Completed
Pull Request — master (#58)
by Surgie
04:18
created

FilterResource::decodeJSON()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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
eloc 2
nc 1
nop 3
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 188
    public function __construct(Filter $filter, $keys = null)
32
    {
33 188
        $this->filter = $filter;
34 188
        $this->keys = $keys;
35 188
    }
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 4
    public function cut($start, $length = null)
88
    {
89 4
        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
96
     * @param int $depth Decode recursion dept
97
     * @param int $options Bitmask of JSON decode options
98
     * @see http://php.net/manual/en/function.json-decode.php More information about the parameters
99
     * @return $this
100
     */
101 11
    public function decodeJSON($assoc = false, $depth = 512, $options = 0)
102
    {
103 11
        return $this->addRule(new FilterRule\DecodeJSON($assoc, $depth, $options));
104
    }
105
106
    /**
107
     * Returns rule that defaults a given value if the data key was not provided
108
     *
109
     * @param mixed $defaultValue
110
     * @return $this
111
     */
112 13
    public function defaults($defaultValue)
113
    {
114 13
        return $this->addRule(new FilterRule\Defaults($defaultValue));
115
    }
116
117
    /**
118
     * Returns rule that can filter repeated nested arrays
119
     *
120
     * @param callable $callable
121
     * @return $this
122
     */
123 2
    public function each(callable $callable)
124
    {
125 2
        return $this->addRule(new FilterRule\Each($callable));
126
    }
127
128
    /**
129
     * Returns rule that returns an value in a specific encoding format
130
     *
131
     * @param string|null $toEncodingFormat
132
     * @param string|null $fromEncodingFormat
133
     * @return $this
134
     */
135 5
    public function encode($toEncodingFormat = null, $fromEncodingFormat = null)
136
    {
137 5
        return $this->addRule(new FilterRule\Encode($toEncodingFormat, $fromEncodingFormat));
138
    }
139
140
    /**
141
     * Returns rule that results a casted float
142
     *
143
     * @return $this
144
     */
145 12
    public function float()
146
    {
147 12
        return $this->addRule(new FilterRule\CastFloat);
148
    }
149
150
    /**
151
     * Returns rule that results a casted int
152
     *
153
     * @return $this
154
     */
155 11
    public function int()
156
    {
157 11
        return $this->addRule(new FilterRule\CastInt);
158
    }
159
160
    /**
161
     * Returns rule that results all letters of a value
162
     *
163
     * @return $this
164
     */
165 3
    public function letters()
166
    {
167 3
        return $this->addRule(new FilterRule\Letters);
168
    }
169
170
    /**
171
     * Returns rule that results a lower-cased value
172
     *
173
     * @return $this
174
     */
175 17
    public function lower()
176
    {
177 17
        return $this->addRule(new FilterRule\Lower);
178
    }
179
180
    /**
181
     * Returns rule that formats numbers
182
     *
183
     * @param int $decimals
184
     * @param string $decimalPoint
185
     * @param string $thousandSeparator
186
     * @return $this
187
     */
188 12
    public function numberFormat($decimals, $decimalPoint, $thousandSeparator)
189
    {
190 12
        return $this->addRule(new FilterRule\NumberFormat($decimals, $decimalPoint, $thousandSeparator));
191
    }
192
193
    /**
194
     * Returns rule that results all numbers of a value
195
     *
196
     * @return $this
197
     */
198 3
    public function numbers()
199
    {
200 3
        return $this->addRule(new FilterRule\Numbers);
201
    }
202
203
    /**
204
     * Results rule that returns the value prepended with a given value
205
     *
206
     * @param string $prepend
207
     * @return $this
208
     */
209 4
    public function prepend($prepend)
210
    {
211 4
        return $this->addRule(new FilterRule\Prepend($prepend));
212
    }
213
214
    /**
215
     * Results rule that returns a value with replacements by a regex
216
     *
217
     * @param string $searchRegex
218
     * @param string $replace
219
     * @return $this
220
     */
221 2
    public function regexReplace($searchRegex, $replace)
222
    {
223 2
        return $this->addRule(new FilterRule\RegexReplace($searchRegex, $replace));
224
    }
225
226
    /**
227
     * Results rule that returns an empty result so it can be removed
228
     *
229
     * @return $this
230
     */
231 5
    public function remove()
232
    {
233 5
        return $this->addRule(new FilterRule\Remove);
234
    }
235
236
    /**
237
     * Results rule that returns an empty result when the value is null so it can be removed
238
     *
239
     * @return $this
240
     */
241 7
    public function removeNull()
242
    {
243 7
        return $this->addRule(new FilterRule\RemoveNull);
244
    }
245
246
    /**
247
     * Results rule that returns a value with replacements
248
     *
249
     * @param mixed $search
250
     * @param mixed $replace
251
     * @return $this
252
     */
253 5
    public function replace($search, $replace)
254
    {
255 5
        return $this->addRule(new FilterRule\Replace($search, $replace));
256
    }
257
258
    /**
259
     * Results that returns a value slugged
260
     *
261
     * @param string|null $fieldToSlugFrom
262
     * @return $this
263
     */
264 9
    public function slug($fieldToSlugFrom = null)
265
    {
266 9
        return $this->addRule(new FilterRule\Slug($fieldToSlugFrom));
267
    }
268
269
    /**
270
     * Returns rule that results a casted string
271
     *
272
     * @return $this
273
     */
274 7
    public function string()
275
    {
276 7
        return $this->addRule(new FilterRule\CastString);
277
    }
278
279
    /**
280
     * Results rule that results a html-stripped value
281
     *
282
     * @param null|string $excludeTags
283
     * @return $this
284
     */
285 4
    public function stripHtml($excludeTags = null)
286
    {
287 4
        return $this->addRule(new FilterRule\StripHtml($excludeTags));
288
    }
289
290
    /**
291
     * Returns rule that results a trimmed value
292
     *
293
     * @param string|null $characters
294
     * @return $this
295
     */
296 21
    public function trim($characters = null)
297
    {
298 21
        return $this->addRule(new FilterRule\Trim($characters));
299
    }
300
301
    /**
302
     * Results rule that returns an upper-cased value
303
     *
304
     * @return $this
305
     */
306 7
    public function upper()
307
    {
308 7
        return $this->addRule(new FilterRule\Upper);
309
    }
310
311
    /**
312
     * Returns rule that results a value starting with a upper-cased character
313
     *
314
     * @return $this
315
     */
316 14
    public function upperFirst()
317
    {
318 14
        return $this->addRule(new FilterRule\UpperFirst);
319
    }
320
321
    /**
322
     * Add a new rule to the chain
323
     *
324
     * @param FilterRule $rule
325
     * @return $this
326
     */
327 187
    protected function addRule(FilterRule $rule)
328
    {
329 187
        if ($this->keys === null) {
330 4
            $this->filter->addFilterRule($rule);
331 4
        }
332
333 187
        if (is_array($this->keys)) {
334 6
            foreach ($this->keys as $key) {
335 6
                $this->filter->addFilterRule($rule, $key);
336 6
            }
337 6
        } else {
338 182
            $this->filter->addFilterRule($rule, $this->keys);
339
        }
340
341 187
        return $this;
342
    }
343
}
344