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