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
|
|
|
* Applies the rule specification to a subject. |
32
|
|
|
* |
33
|
|
|
* @param mixed $subject The filter subject. |
34
|
|
|
* |
35
|
|
|
* @return bool True on success, false on failure. |
36
|
|
|
* |
37
|
|
|
*/ |
38
|
13 |
|
public function __invoke($subject) |
39
|
|
|
{ |
40
|
13 |
|
if ($this->subjectFieldIsBlank($subject)) { |
41
|
8 |
|
return $this->allow_blank; |
42
|
|
|
} |
43
|
|
|
|
44
|
11 |
|
if (! $this->rule) { |
45
|
2 |
|
return $this->reverse; |
46
|
|
|
} |
47
|
|
|
|
48
|
9 |
|
if ($this->reverse) { |
49
|
2 |
|
return ! parent::__invoke($subject); |
50
|
|
|
} |
51
|
|
|
|
52
|
7 |
|
return parent::__invoke($subject); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* |
57
|
|
|
* Validate the field matches this rule (blank not allowed). |
58
|
|
|
* |
59
|
|
|
* @param string $rule The rule name. |
60
|
|
|
* |
61
|
|
|
* @param ...$args Arguments for the rule. |
62
|
|
|
* |
63
|
|
|
* @return self |
64
|
|
|
* |
65
|
|
|
*/ |
66
|
9 |
|
public function is($rule) |
|
|
|
|
67
|
|
|
{ |
68
|
9 |
|
$this->allow_blank = false; |
69
|
9 |
|
$this->reverse = false; |
70
|
9 |
|
return $this->init(func_get_args()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* |
75
|
|
|
* Validate the field is blank. |
76
|
|
|
* |
77
|
|
|
* @return self |
78
|
|
|
* |
79
|
|
|
*/ |
80
|
1 |
|
public function isBlank() |
81
|
|
|
{ |
82
|
1 |
|
$this->allow_blank = true; |
83
|
1 |
|
$this->reverse = false; |
84
|
1 |
|
return $this->init(array()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* |
89
|
|
|
* Validate the field matches this rule (blank allowed). |
90
|
|
|
* |
91
|
|
|
* @param string $rule The rule name. |
92
|
|
|
* |
93
|
|
|
* @param ...$args Arguments for the rule. |
94
|
|
|
* |
95
|
|
|
* @return self |
96
|
|
|
* |
97
|
|
|
*/ |
98
|
1 |
|
public function isBlankOr($rule) |
|
|
|
|
99
|
|
|
{ |
100
|
1 |
|
$this->allow_blank = true; |
101
|
1 |
|
$this->reverse = false; |
102
|
1 |
|
return $this->init(func_get_args()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* |
107
|
|
|
* Validate the field does not match this rule (blank not allowed). |
108
|
|
|
* |
109
|
|
|
* @param string $rule The rule name. |
110
|
|
|
* |
111
|
|
|
* @param ...$args Arguments for the rule. |
112
|
|
|
* |
113
|
|
|
* @return self |
114
|
|
|
* |
115
|
|
|
*/ |
116
|
2 |
|
public function isNot($rule) |
|
|
|
|
117
|
|
|
{ |
118
|
2 |
|
$this->allow_blank = false; |
119
|
2 |
|
$this->reverse = true; |
120
|
2 |
|
return $this->init(func_get_args()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* |
125
|
|
|
* Validate the field is not blank. |
126
|
|
|
* |
127
|
|
|
* @return self |
128
|
|
|
* |
129
|
|
|
*/ |
130
|
2 |
|
public function isNotBlank() |
131
|
|
|
{ |
132
|
2 |
|
$this->allow_blank = false; |
133
|
2 |
|
$this->reverse = true; |
134
|
2 |
|
return $this->init(array()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* |
139
|
|
|
* Validate the field does not match this rule (blank allowed). |
140
|
|
|
* |
141
|
|
|
* @param string $rule The rule name. |
142
|
|
|
* |
143
|
|
|
* @param ...$args Arguments for the rule. |
144
|
|
|
* |
145
|
|
|
* @return self |
146
|
|
|
* |
147
|
|
|
*/ |
148
|
1 |
|
public function isBlankOrNot($rule) |
|
|
|
|
149
|
|
|
{ |
150
|
1 |
|
$this->allow_blank = true; |
151
|
1 |
|
$this->reverse = true; |
152
|
1 |
|
return $this->init(func_get_args()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* |
157
|
|
|
* Returns the default failure message for this rule specification. |
158
|
|
|
* |
159
|
|
|
* @return string |
160
|
|
|
* |
161
|
|
|
*/ |
162
|
13 |
|
protected function getDefaultMessage() |
163
|
|
|
{ |
164
|
13 |
|
$message = $this->field . ' should'; |
165
|
|
|
|
166
|
13 |
|
if (! $this->rule) { |
167
|
|
|
return $message |
168
|
3 |
|
. (($this->reverse) ? ' not' : '') |
169
|
3 |
|
. ' have been blank'; |
170
|
|
|
} |
171
|
|
|
|
172
|
11 |
|
if ($this->allow_blank) { |
173
|
2 |
|
$message .= ' have been blank or'; |
174
|
2 |
|
} |
175
|
|
|
|
176
|
11 |
|
if ($this->reverse) { |
177
|
2 |
|
$message .= ' not'; |
178
|
2 |
|
} |
179
|
|
|
|
180
|
11 |
|
return "{$message} have validated as " . parent::getDefaultMessage(); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.