Completed
Pull Request — master (#48)
by
unknown
06:37
created

FilterResource::encode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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