Completed
Pull Request — master (#47)
by
unknown
02:40
created

FilterResource::slug()   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 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
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 176
    public function __construct(Filter $filter, $keys = null)
32
    {
33 176
        $this->filter = $filter;
34 176
        $this->keys = $keys;
35 176
    }
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
     * Results that returns a value slugged
246
     *
247
     * @param string|null $fieldToSlugFrom
248
     * @return $this
249
     */
250 8
    public function slug($fieldToSlugFrom = null)
251
    {
252 8
        return $this->addRule(new FilterRule\Slug($fieldToSlugFrom));
253
    }
254
255
    /**
256
     * Returns rule that results a casted string
257
     *
258
     * @return $this
259
     */
260 7
    public function string()
261
    {
262 7
        return $this->addRule(new FilterRule\CastString);
263
    }
264
265
    /**
266
     * Results rule that results a html-stripped value
267
     *
268
     * @param null|string $excludeTags
269
     * @return $this
270
     */
271 4
    public function stripHtml($excludeTags = null)
272
    {
273 4
        return $this->addRule(new FilterRule\StripHtml($excludeTags));
274
    }
275
276
    /**
277
     * Returns rule that results a trimmed value
278
     *
279
     * @param string|null $characters
280
     * @return $this
281
     */
282 21
    public function trim($characters = null)
283
    {
284 21
        return $this->addRule(new FilterRule\Trim($characters));
285
    }
286
287
    /**
288
     * Results rule that returns an upper-cased value
289
     *
290
     * @return $this
291
     */
292 7
    public function upper()
293
    {
294 7
        return $this->addRule(new FilterRule\Upper);
295
    }
296
297
    /**
298
     * Returns rule that results a value starting with a upper-cased character
299
     *
300
     * @return $this
301
     */
302 14
    public function upperFirst()
303
    {
304 14
        return $this->addRule(new FilterRule\UpperFirst);
305
    }
306
307
    /**
308
     * Add a new rule to the chain
309
     *
310
     * @param FilterRule $rule
311
     * @return $this
312
     */
313 175
    protected function addRule(FilterRule $rule)
314
    {
315 175
        if ($this->keys === null) {
316 4
            $this->filter->addFilterRule($rule);
317 4
        }
318
319 175
        if (is_array($this->keys)) {
320 6
            foreach ($this->keys as $key) {
321 6
                $this->filter->addFilterRule($rule, $key);
322 6
            }
323 6
        } else {
324 170
            $this->filter->addFilterRule($rule, $this->keys);
325
        }
326
327 175
        return $this;
328
    }
329
}
330