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
|
|
|
/** |
12
|
|
|
* |
13
|
|
|
* A static proxy to a ValueFilter instance. |
14
|
|
|
* |
15
|
|
|
* @package Aura.Filter |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
abstract class AbstractStaticFilter |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* |
22
|
|
|
* The proxied filter instance. |
23
|
|
|
* |
24
|
|
|
* @var ValueFilter |
25
|
|
|
* |
26
|
|
|
*/ |
27
|
|
|
protected static $instance; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* |
31
|
|
|
* Prevents construction of the static proxy as an instance. |
32
|
|
|
* |
33
|
|
|
* @codeCoverageIgnore |
34
|
|
|
* |
35
|
|
|
*/ |
36
|
|
|
private function __construct() |
37
|
|
|
{ |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* |
42
|
|
|
* Sets the proxied filter instance. |
43
|
|
|
* |
44
|
|
|
* @param ValueFilter $instance The proxied filter instance. |
45
|
|
|
* |
46
|
|
|
* @return null |
47
|
|
|
* |
48
|
|
|
*/ |
49
|
2 |
|
public static function setInstance(ValueFilter $instance) |
50
|
|
|
{ |
51
|
2 |
|
if (static::$instance) { |
52
|
1 |
|
$class = get_called_class(); |
53
|
1 |
|
throw new Exception("{$class}::\$instance is already set."); |
54
|
|
|
} |
55
|
2 |
|
static::$instance = $instance; |
56
|
2 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* |
60
|
|
|
* Validates a value using a rule. |
61
|
|
|
* |
62
|
|
|
* @param mixed $value The value to validate. |
63
|
|
|
* |
64
|
|
|
* @param string $rule The rule name. |
65
|
|
|
* |
66
|
|
|
* @param ...$args Arguments to pass to the rule. |
67
|
|
|
* |
68
|
|
|
* @return bool True on success, false on failure. |
69
|
|
|
* |
70
|
|
|
*/ |
71
|
2 |
|
public static function validate($value, $rule) |
|
|
|
|
72
|
|
|
{ |
73
|
2 |
|
if (! static::$instance) { |
74
|
1 |
|
$class = get_called_class(); |
75
|
1 |
|
throw new Exception("{$class}::\$instance not set."); |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
$args = func_get_args(); |
79
|
1 |
|
return call_user_func_array( |
80
|
1 |
|
array(static::$instance, 'validate'), |
81
|
1 |
|
$args |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* |
87
|
|
|
* Sanitizes a value in place using a rule. |
88
|
|
|
* |
89
|
|
|
* @param mixed $value The value to sanitize. |
90
|
|
|
* |
91
|
|
|
* @param string $rule The rule name. |
92
|
|
|
* |
93
|
|
|
* @param ...$args Arguments to pass to the rule. |
94
|
|
|
* |
95
|
|
|
* @return bool True on success, false on failure. |
96
|
|
|
* |
97
|
|
|
*/ |
98
|
2 |
|
public static function sanitize(&$value, $rule) |
|
|
|
|
99
|
|
|
{ |
100
|
2 |
|
if (! static::$instance) { |
101
|
1 |
|
$class = get_called_class(); |
102
|
1 |
|
throw new Exception("{$class}::\$instance not set."); |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
$args = func_get_args(); |
106
|
1 |
|
$args[0] = &$value; |
107
|
1 |
|
return call_user_func_array( |
108
|
1 |
|
array(static::$instance, 'sanitize'), |
109
|
1 |
|
$args |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.