Completed
Push — master ( 2fcf3b...8f00bb )
by Rick
04:45 queued 02:17
created

FilterResource::cut()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
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-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 168
    public function __construct(Filter $filter, $keys = null)
32
    {
33 168
        $this->filter = $filter;
34 168
        $this->keys = $keys;
35 168
    }
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
     */
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 defaults a given value if the data key was not provided
94
     *
95
     * @param mixed $defaultValue
96
     * @return $this
97
     */
98 13
    public function defaults($defaultValue)
99
    {
100 13
        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 2
    public function each(callable $callable)
110
    {
111 2
        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
     * @return $this
120
     */
121 5
    public function encode($toEncodingFormat = null, $fromEncodingFormat = null)
122
    {
123 5
        return $this->addRule(new FilterRule\Encode($toEncodingFormat, $fromEncodingFormat));
124
    }
125
126
    /**
127
     * Returns rule that results a casted float
128
     *
129
     * @return $this
130
     */
131 12
    public function float()
132
    {
133 12
        return $this->addRule(new FilterRule\CastFloat);
134
    }
135
136
    /**
137
     * Returns rule that results a casted int
138
     *
139
     * @return $this
140
     */
141 11
    public function int()
142
    {
143 11
        return $this->addRule(new FilterRule\CastInt);
144
    }
145
146
    /**
147
     * Returns rule that results all letters of a value
148
     *
149
     * @return $this
150
     */
151 3
    public function letters()
152
    {
153 3
        return $this->addRule(new FilterRule\Letters);
154
    }
155
156
    /**
157
     * Returns rule that results a lower-cased value
158
     *
159
     * @return $this
160
     */
161 17
    public function lower()
162
    {
163 17
        return $this->addRule(new FilterRule\Lower);
164
    }
165
166
    /**
167
     * Returns rule that formats numbers
168
     *
169
     * @param int $decimals
170
     * @param string $decimalPoint
171
     * @param string $thousandSeparator
172
     * @return $this
173
     */
174 12
    public function numberFormat($decimals, $decimalPoint, $thousandSeparator)
175
    {
176 12
        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
     */
184 3
    public function numbers()
185
    {
186 3
        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 4
    public function prepend($prepend)
196
    {
197 4
        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
     * @return $this
206
     */
207 2
    public function regexReplace($searchRegex, $replace)
208
    {
209 2
        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
     * @return $this
216
     */
217 5
    public function remove()
218
    {
219 5
        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 7
    public function removeNull()
228
    {
229 7
        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
     * @return $this
238
     */
239 5
    public function replace($search, $replace)
240
    {
241 5
        return $this->addRule(new FilterRule\Replace($search, $replace));
242
    }
243
244
    /**
245
     * Returns rule that results a casted string
246
     *
247
     * @return $this
248
     */
249 7
    public function string()
250
    {
251 7
        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
     * @return $this
259
     */
260 4
    public function stripHtml($excludeTags = null)
261
    {
262 4
        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
     * @return $this
270
     */
271 21
    public function trim($characters = null)
272
    {
273 21
        return $this->addRule(new FilterRule\Trim($characters));
274
    }
275
276
    /**
277
     * Results rule that returns an upper-cased value
278
     *
279
     * @return $this
280
     */
281 7
    public function upper()
282
    {
283 7
        return $this->addRule(new FilterRule\Upper);
284
    }
285
286
    /**
287
     * Returns rule that results a value starting with a upper-cased character
288
     *
289
     * @return $this
290
     */
291 14
    public function upperFirst()
292
    {
293 14
        return $this->addRule(new FilterRule\UpperFirst);
294
    }
295
296
    /**
297
     * Add a new rule to the chain
298
     *
299
     * @param FilterRule $rule
300
     * @return $this
301
     */
302 167
    protected function addRule(FilterRule $rule)
303
    {
304 167
        if ($this->keys === null) {
305 4
            $this->filter->addFilterRule($rule);
306 4
        }
307
308 167
        if (is_array($this->keys)) {
309 6
            foreach ($this->keys as $key) {
310 6
                $this->filter->addFilterRule($rule, $key);
311 6
            }
312 6
        } else {
313 162
            $this->filter->addFilterRule($rule, $this->keys);
314
        }
315
316 167
        return $this;
317
    }
318
}
319