FilterResource::upper()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Particle.
4
 *
5
 * @link      http://github.com/particle-php for the canonical source repository
6
 * @copyright Copyright (c) 2005-2016 Particle (http://particle-php.com)
7
 * @license   https://github.com/particle-php/Filter/blob/master/LICENSE New BSD License
8
 */
9
namespace Particle\Filter;
10
11
/**
12
 * Class FilterResource
13
 * @package Particle\Filter
14
 */
15
class FilterResource
16
{
17
    /**
18
     * @var Filter
19
     */
20
    protected $filter;
21
22
    /**
23
     * @var null|string|string[]
24
     */
25
    protected $keys;
26
27
    /**
28
     * @param Filter $filter
29
     * @param null|string|string[] $keys
30
     */
31 192
    public function __construct(Filter $filter, $keys = null)
32
    {
33 192
        $this->filter = $filter;
34 192
        $this->keys = $keys;
35 192
    }
36
37
    /**
38
     * Results rule that returns alphabetic numeric characters from the value
39
     *
40
     * @return $this
41
     */
42 4
    public function alnum()
43
    {
44 4
        return $this->addRule(new FilterRule\AlNum);
45
    }
46
47
    /**
48
     * Results rule that returns the value appended with a given value
49
     *
50
     * @param string $append
51
     * @return $this
52
     */
53 4
    public function append($append)
54
    {
55 4
        return $this->addRule(new FilterRule\Append($append));
56
    }
57
58
    /**
59
     * Results rule that returns a casted boolean
60
     *
61
     * @return $this
62
     */
63 16
    public function bool()
64
    {
65 16
        return $this->addRule(new FilterRule\CastBool);
66
    }
67
68
    /**
69
     * Returns rule that returns a value modified by a callable closure
70
     *
71
     * @param callable $callable
72
     * @param bool $allowNotSet
73
     * @return $this
74
     */
75 6
    public function callback(callable $callable, $allowNotSet = false)
76
    {
77 6
        return $this->addRule(new FilterRule\Callback($callable, $allowNotSet));
78
    }
79
80
    /**
81
     * Results rule that returns the cut value
82
     *
83
     * @param int      $start
84
     * @param int|null $length
85
     * @return $this
86
     */
87 5
    public function cut($start, $length = null)
88
    {
89 5
        return $this->addRule(new FilterRule\Cut($start, $length));
90
    }
91
92
    /**
93
     * Returns rule that decodes JSON code of a given value
94
     *
95
     * @param bool $assoc When `true`, decoded objects will be converted into associative arrays (the default value is
96
     *     set to `true` because both Filter and Validator can't deal with objects)
97
     * @param int $depth Decode recursion dept
98
     * @param int $options Bitmask of JSON decode options
99
     * @see http://php.net/manual/en/function.json-decode.php More information about the parameters
100
     * @return $this
101
     */
102 11
    public function decodeJSON($assoc = true, $depth = 512, $options = 0)
103
    {
104 11
        return $this->addRule(new FilterRule\DecodeJSON($assoc, $depth, $options));
105
    }
106
107
    /**
108
     * Returns rule that defaults a given value if the data key was not provided
109
     *
110
     * @param mixed $defaultValue
111
     * @return $this
112
     */
113 13
    public function defaults($defaultValue)
114
    {
115 13
        return $this->addRule(new FilterRule\Defaults($defaultValue));
116
    }
117
118
    /**
119
     * Returns rule that can filter repeated nested arrays
120
     *
121
     * @param callable $callable
122
     * @return $this
123
     */
124 2
    public function each(callable $callable)
125
    {
126 2
        return $this->addRule(new FilterRule\Each($callable));
127
    }
128
129
    /**
130
     * Returns rule that returns an value in a specific encoding format
131
     *
132
     * @param string|null $toEncodingFormat
133
     * @param string|null $fromEncodingFormat
134
     * @return $this
135
     */
136 5
    public function encode($toEncodingFormat = null, $fromEncodingFormat = null)
137
    {
138 5
        return $this->addRule(new FilterRule\Encode($toEncodingFormat, $fromEncodingFormat));
139
    }
140
141
    /**
142
     * Returns rule that results a casted float
143
     *
144
     * @return $this
145
     */
146 12
    public function float()
147
    {
148 12
        return $this->addRule(new FilterRule\CastFloat);
149
    }
150
151
    /**
152
     * Returns rule that results a casted int
153
     *
154
     * @return $this
155
     */
156 11
    public function int()
157
    {
158 11
        return $this->addRule(new FilterRule\CastInt);
159
    }
160
161
    /**
162
     * Returns rule that results all letters of a value
163
     *
164
     * @return $this
165
     */
166 3
    public function letters()
167
    {
168 3
        return $this->addRule(new FilterRule\Letters);
169
    }
170
171
    /**
172
     * Returns rule that results a lower-cased value
173
     *
174
     * @return $this
175
     */
176 17
    public function lower()
177
    {
178 17
        return $this->addRule(new FilterRule\Lower);
179
    }
180
181
    /**
182
     * Returns rule that formats numbers
183
     *
184
     * @param int $decimals
185
     * @param string $decimalPoint
186
     * @param string $thousandSeparator
187
     * @return $this
188
     */
189 12
    public function numberFormat($decimals, $decimalPoint, $thousandSeparator)
190
    {
191 12
        return $this->addRule(new FilterRule\NumberFormat($decimals, $decimalPoint, $thousandSeparator));
192
    }
193
194
    /**
195
     * Returns rule that results all numbers of a value
196
     *
197
     * @return $this
198
     */
199 3
    public function numbers()
200
    {
201 3
        return $this->addRule(new FilterRule\Numbers);
202
    }
203
204
    /**
205
     * Results rule that returns the value prepended with a given value
206
     *
207
     * @param string $prepend
208
     * @return $this
209
     */
210 4
    public function prepend($prepend)
211
    {
212 4
        return $this->addRule(new FilterRule\Prepend($prepend));
213
    }
214
215
    /**
216
     * Results rule that returns a value with replacements by a regex
217
     *
218
     * @param string $searchRegex
219
     * @param string $replace
220
     * @return $this
221
     */
222 2
    public function regexReplace($searchRegex, $replace)
223
    {
224 2
        return $this->addRule(new FilterRule\RegexReplace($searchRegex, $replace));
225
    }
226
227
    /**
228
     * Results rule that returns an empty result so it can be removed
229
     *
230
     * @return $this
231
     */
232 5
    public function remove()
233
    {
234 5
        return $this->addRule(new FilterRule\Remove);
235
    }
236
237
    /**
238
     * Results rule that returns an empty result when the value is null so it can be removed
239
     *
240
     * @return $this
241
     */
242 7
    public function removeNull()
243
    {
244 7
        return $this->addRule(new FilterRule\RemoveNull);
245
    }
246
247
    /**
248
     * Results rule that returns a value with replacements
249
     *
250
     * @param mixed $search
251
     * @param mixed $replace
252
     * @return $this
253
     */
254 5
    public function replace($search, $replace)
255
    {
256 5
        return $this->addRule(new FilterRule\Replace($search, $replace));
257
    }
258
259
    /**
260
     * Results that returns a value slugged
261
     *
262
     * @param string|null $fieldToSlugFrom
263
     * @return $this
264
     */
265 11
    public function slug($fieldToSlugFrom = null)
266
    {
267 11
        return $this->addRule(new FilterRule\Slug($fieldToSlugFrom));
268
    }
269
270
    /**
271
     * Returns rule that results a casted string
272
     *
273
     * @return $this
274
     */
275 7
    public function string()
276
    {
277 7
        return $this->addRule(new FilterRule\CastString);
278
    }
279
280
    /**
281
     * Results rule that results a html-stripped value
282
     *
283
     * @param null|string $excludeTags
284
     * @return $this
285
     */
286 4
    public function stripHtml($excludeTags = null)
287
    {
288 4
        return $this->addRule(new FilterRule\StripHtml($excludeTags));
289
    }
290
291
    /**
292
     * Returns rule that results a trimmed value
293
     *
294
     * @param string|null $characters
295
     * @return $this
296
     */
297 22
    public function trim($characters = null)
298
    {
299 22
        return $this->addRule(new FilterRule\Trim($characters));
300
    }
301
302
    /**
303
     * Results rule that returns an upper-cased value
304
     *
305
     * @return $this
306
     */
307 7
    public function upper()
308
    {
309 7
        return $this->addRule(new FilterRule\Upper);
310
    }
311
312
    /**
313
     * Returns rule that results a value starting with a upper-cased character
314
     *
315
     * @return $this
316
     */
317 14
    public function upperFirst()
318
    {
319 14
        return $this->addRule(new FilterRule\UpperFirst);
320
    }
321
322
    /**
323
     * Add a new rule to the chain
324
     *
325
     * @param FilterRule $rule
326
     * @return $this
327
     */
328 191
    protected function addRule(FilterRule $rule)
329
    {
330 191
        if ($this->keys === null) {
331 4
            $this->filter->addFilterRule($rule);
332 4
        }
333
334 191
        if (is_array($this->keys)) {
335 6
            foreach ($this->keys as $key) {
336 6
                $this->filter->addFilterRule($rule, $key);
337 6
            }
338 6
        } else {
339 186
            $this->filter->addFilterRule($rule, $this->keys);
340
        }
341
342 191
        return $this;
343
    }
344
}
345