|
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
|
|
|
use Exception; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Chain |
|
15
|
|
|
* |
|
16
|
|
|
* @package Particle\Filter |
|
17
|
|
|
*/ |
|
18
|
|
|
class Chain |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var FilterRule[] |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $rules; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Execute all filters in the chain |
|
27
|
|
|
* |
|
28
|
|
|
* @param bool $isNotEmpty |
|
29
|
|
|
* @param mixed $value |
|
30
|
|
|
* @param array|null $filterData |
|
31
|
|
|
* @return FilterResult |
|
32
|
|
|
* @throws Exception |
|
33
|
|
|
*/ |
|
34
|
163 |
|
public function filter($isNotEmpty, $value = null, $filterData = null) |
|
35
|
|
|
{ |
|
36
|
|
|
/** @var FilterRule $rule */ |
|
37
|
163 |
|
foreach ($this->rules as $rule) { |
|
38
|
163 |
|
$rule->setFilterData($filterData); |
|
39
|
163 |
|
if ($isNotEmpty || $rule->allowedNotSet()) { |
|
40
|
160 |
|
$filterResult = $rule->filter($value); |
|
41
|
|
|
|
|
42
|
160 |
|
if (!$filterResult instanceof FilterResult) { |
|
43
|
|
|
throw new Exception( |
|
44
|
|
|
'A FilterResult object must be returned by the FilterRule->filter function.' |
|
45
|
|
|
. ' got another value from ' . get_class($rule) |
|
46
|
|
|
); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
160 |
|
$isNotEmpty = $filterResult->isNotEmpty(); |
|
50
|
160 |
|
$value = $filterResult->getFilteredValue(); |
|
51
|
160 |
|
} |
|
52
|
163 |
|
} |
|
53
|
|
|
|
|
54
|
163 |
|
return new FilterResult($isNotEmpty, $value); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Add a new rule to the chain |
|
59
|
|
|
* |
|
60
|
|
|
* @param FilterRule $rule |
|
61
|
|
|
* @param string|null $encodingFormat |
|
62
|
|
|
* @return $this |
|
63
|
|
|
*/ |
|
64
|
163 |
|
public function addRule(FilterRule $rule, $encodingFormat) |
|
65
|
|
|
{ |
|
66
|
163 |
|
$rule->setEncodingFormat($encodingFormat); |
|
67
|
|
|
|
|
68
|
163 |
|
$this->rules[] = $rule; |
|
69
|
|
|
|
|
70
|
163 |
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|