|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* This file is part of Aura for PHP. |
|
5
|
|
|
* |
|
6
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace Aura\Filter; |
|
10
|
|
|
|
|
11
|
|
|
use Aura\Filter\Locator\Locator; |
|
12
|
|
|
use Aura\Filter\Locator\SanitizeLocator; |
|
13
|
|
|
use Aura\Filter\Locator\ValidateLocator; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* |
|
17
|
|
|
* A filter for individual values. |
|
18
|
|
|
* |
|
19
|
|
|
* @package Aura.Filter |
|
20
|
|
|
* |
|
21
|
|
|
*/ |
|
22
|
|
|
class ValueFilter |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* |
|
26
|
|
|
* A pesudo-subject to hold the value being filtered. |
|
27
|
|
|
* |
|
28
|
|
|
* @var object |
|
29
|
|
|
* |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $subject; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* |
|
35
|
|
|
* Constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param ValidateLocator $validate_locator A locator for "validate" rules. |
|
38
|
|
|
* |
|
39
|
|
|
* @param SanitizeLocator $sanitize_locator A locator for "sanitize" rules. |
|
40
|
|
|
* |
|
41
|
|
|
* @return self |
|
|
|
|
|
|
42
|
|
|
* |
|
43
|
|
|
*/ |
|
44
|
4 |
|
public function __construct( |
|
45
|
|
|
ValidateLocator $validate_locator, |
|
46
|
|
|
SanitizeLocator $sanitize_locator |
|
47
|
|
|
) { |
|
48
|
4 |
|
$this->validate_locator = $validate_locator; |
|
|
|
|
|
|
49
|
4 |
|
$this->sanitize_locator = $sanitize_locator; |
|
|
|
|
|
|
50
|
4 |
|
$this->subject = (object) array('value' => null); |
|
51
|
4 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* |
|
55
|
|
|
* Validates a value using a rule. |
|
56
|
|
|
* |
|
57
|
|
|
* @param mixed $value The value to validate. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $rule The rule name. |
|
60
|
|
|
* |
|
61
|
|
|
* @param ...$args Arguments to pass to the rule. |
|
62
|
|
|
* |
|
63
|
|
|
* @return bool True on success, false on failure. |
|
64
|
|
|
* |
|
65
|
|
|
*/ |
|
66
|
2 |
|
public function validate($value, $rule) |
|
|
|
|
|
|
67
|
|
|
{ |
|
68
|
2 |
|
$this->subject->value = $value; |
|
69
|
2 |
|
return $this->apply($this->validate_locator, func_get_args()); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* |
|
74
|
|
|
* Sanitizes a value in place using a rule. |
|
75
|
|
|
* |
|
76
|
|
|
* @param mixed $value The value to sanitize. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $rule The rule name. |
|
79
|
|
|
* |
|
80
|
|
|
* @param ...$args Arguments to pass to the rule. |
|
81
|
|
|
* |
|
82
|
|
|
* @return bool True on success, false on failure. |
|
83
|
|
|
* |
|
84
|
|
|
*/ |
|
85
|
2 |
|
public function sanitize(&$value, $rule) |
|
|
|
|
|
|
86
|
|
|
{ |
|
87
|
2 |
|
$this->subject->value =& $value; |
|
88
|
2 |
|
return $this->apply($this->sanitize_locator, func_get_args()); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* |
|
93
|
|
|
* Applies a rule. |
|
94
|
|
|
* |
|
95
|
|
|
* @param Locator $locator A rule locator. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $args Arugments for the rule. |
|
98
|
|
|
* |
|
99
|
|
|
* @return bool True on success, false on failure. |
|
100
|
|
|
* |
|
101
|
|
|
*/ |
|
102
|
2 |
|
protected function apply(Locator $locator, $args) |
|
103
|
|
|
{ |
|
104
|
2 |
|
array_shift($args); // remove $value |
|
105
|
2 |
|
$rule = array_shift($args); // remove $rule |
|
106
|
2 |
|
$rule = $locator->get($rule); // create rule object |
|
107
|
|
|
|
|
108
|
2 |
|
array_unshift($args, 'value'); // add field name on $this->subject |
|
109
|
2 |
|
array_unshift($args, $this->subject); // add $this->subject |
|
110
|
2 |
|
return call_user_func_array($rule, $args); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.