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