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\Spec; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* |
13
|
|
|
* A "validate" rule specification. |
14
|
|
|
* |
15
|
|
|
* @package Aura.Filter |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
class ValidateSpec extends Spec |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* |
22
|
|
|
* Reverse the rule, so that a "pass" is treated as a "fail". |
23
|
|
|
* |
24
|
|
|
* @var bool |
25
|
|
|
* |
26
|
|
|
*/ |
27
|
|
|
protected $reverse = false; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* |
31
|
|
|
* Validate the field matches this rule (blank not allowed). |
32
|
|
|
* |
33
|
|
|
* @param string $rule The rule name. |
34
|
|
|
* |
35
|
|
|
* @param ...$args Arguments for the rule. |
36
|
|
|
* |
37
|
|
|
* @return self |
38
|
|
|
* |
39
|
|
|
*/ |
40
|
9 |
|
public function is($rule) |
|
|
|
|
41
|
|
|
{ |
42
|
9 |
|
$this->allow_blank = false; |
43
|
9 |
|
$this->reverse = false; |
44
|
9 |
|
return $this->init(func_get_args()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* |
49
|
|
|
* Validate the field is blank. |
50
|
|
|
* |
51
|
|
|
* @return self |
52
|
|
|
* |
53
|
|
|
*/ |
54
|
1 |
|
public function isBlank() |
55
|
|
|
{ |
56
|
1 |
|
$this->allow_blank = true; |
57
|
1 |
|
$this->reverse = false; |
58
|
1 |
|
return $this->init(array()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* |
63
|
|
|
* Validate the field matches this rule (blank allowed). |
64
|
|
|
* |
65
|
|
|
* @param string $rule The rule name. |
66
|
|
|
* |
67
|
|
|
* @param ...$args Arguments for the rule. |
68
|
|
|
* |
69
|
|
|
* @return self |
70
|
|
|
* |
71
|
|
|
*/ |
72
|
1 |
|
public function isBlankOr($rule) |
|
|
|
|
73
|
|
|
{ |
74
|
1 |
|
$this->allow_blank = true; |
75
|
1 |
|
$this->reverse = false; |
76
|
1 |
|
return $this->init(func_get_args()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* |
81
|
|
|
* Validate the field does not match this rule (blank not allowed). |
82
|
|
|
* |
83
|
|
|
* @param string $rule The rule name. |
84
|
|
|
* |
85
|
|
|
* @param ...$args Arguments for the rule. |
86
|
|
|
* |
87
|
|
|
* @return self |
88
|
|
|
* |
89
|
|
|
*/ |
90
|
2 |
|
public function isNot($rule) |
|
|
|
|
91
|
|
|
{ |
92
|
2 |
|
$this->allow_blank = false; |
93
|
2 |
|
$this->reverse = true; |
94
|
2 |
|
return $this->init(func_get_args()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* |
99
|
|
|
* Validate the field does not match this rule (blank allowed). |
100
|
|
|
* |
101
|
|
|
* @param string $rule The rule name. |
102
|
|
|
* |
103
|
|
|
* @param ...$args Arguments for the rule. |
104
|
|
|
* |
105
|
|
|
* @return self |
106
|
|
|
* |
107
|
|
|
*/ |
108
|
1 |
|
public function isBlankOrNot($rule) |
|
|
|
|
109
|
|
|
{ |
110
|
1 |
|
$this->allow_blank = true; |
111
|
1 |
|
$this->reverse = true; |
112
|
1 |
|
return $this->init(func_get_args()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* |
117
|
|
|
* Returns the default failure message for this rule specification. |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
* |
121
|
|
|
*/ |
122
|
12 |
|
protected function getDefaultMessage() |
123
|
|
|
{ |
124
|
12 |
|
$message = $this->field . ' should'; |
125
|
|
|
|
126
|
12 |
|
if (! $this->rule) { |
127
|
1 |
|
return $message . ' have been blank'; |
128
|
|
|
} |
129
|
|
|
|
130
|
11 |
|
if ($this->allow_blank) { |
131
|
2 |
|
$message .= ' have been blank or'; |
132
|
2 |
|
} |
133
|
|
|
|
134
|
11 |
|
if ($this->reverse) { |
135
|
2 |
|
$message .= ' not'; |
136
|
2 |
|
} |
137
|
|
|
|
138
|
11 |
|
return "{$message} have validated as " . parent::getDefaultMessage(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* |
143
|
|
|
* Check if the subject field passes the rule specification. |
144
|
|
|
* |
145
|
|
|
* @param mixed $subject The filter subject. |
146
|
|
|
* |
147
|
|
|
* @return bool |
148
|
|
|
* |
149
|
|
|
*/ |
150
|
12 |
|
protected function applyRule($subject) |
151
|
|
|
{ |
152
|
12 |
|
if (! $this->rule) { |
153
|
1 |
|
return false; |
154
|
|
|
} |
155
|
|
|
|
156
|
11 |
|
if (! $this->fieldExists($subject)) { |
157
|
2 |
|
return false; |
158
|
|
|
} |
159
|
|
|
|
160
|
10 |
|
if ($this->reverse) { |
161
|
2 |
|
return ! parent::applyRule($subject); |
162
|
|
|
} |
163
|
|
|
|
164
|
8 |
|
return parent::applyRule($subject); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* |
169
|
|
|
* Does the field exist in the subject, even if it is null? |
170
|
|
|
* |
171
|
|
|
* @param mixed $subject The filter subject. |
172
|
|
|
* |
173
|
|
|
* @return bool |
174
|
|
|
* |
175
|
|
|
*/ |
176
|
11 |
|
protected function fieldExists($subject) |
177
|
|
|
{ |
178
|
11 |
|
$field = $this->field; |
179
|
11 |
|
if (isset($subject->$field)) { |
180
|
10 |
|
return true; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
// still, the property might exist and be null. using property_exists() |
184
|
|
|
// presumes that we have a non-magic-method object, which may not be the |
185
|
|
|
// case, so we have this hackish approach. |
186
|
|
|
|
187
|
|
|
// first, turn off error reporting entirely. |
188
|
2 |
|
$level = error_reporting(0); |
189
|
|
|
|
190
|
|
|
// now put error_get_last() into known state by addressing a nonexistent |
191
|
|
|
// variable with an unlikely name. |
192
|
2 |
|
$fake = __FILE__ . ':' . __CLASS__; |
193
|
2 |
|
$value = $$fake; |
|
|
|
|
194
|
|
|
|
195
|
|
|
// now get the value of the field and turn error reporting back on |
196
|
2 |
|
$value = $subject->$field; |
|
|
|
|
197
|
2 |
|
error_reporting($level); |
198
|
|
|
|
199
|
|
|
// if the last error was on $field, then $field is nonexistent. |
200
|
2 |
|
$error = error_get_last(); |
201
|
2 |
|
$property = substr($error['message'], -1 * strlen($field) - 1); |
202
|
2 |
|
return $property !== "\$$field"; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.