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
|
7 |
|
public function is($rule) |
|
|
|
|
41
|
|
|
{ |
42
|
7 |
|
$this->allow_blank = false; |
43
|
7 |
|
$this->reverse = false; |
44
|
7 |
|
return $this->init(func_get_args()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* |
49
|
|
|
* Validate the field is blank. |
50
|
|
|
* |
51
|
|
|
* @return self |
52
|
|
|
* |
53
|
|
|
*/ |
54
|
|
|
public function isBlank() |
55
|
|
|
{ |
56
|
|
|
$this->allow_blank = true; |
57
|
|
|
$this->reverse = false; |
58
|
1 |
|
return $this->init(array()); |
59
|
|
|
} |
60
|
1 |
|
|
61
|
1 |
|
/** |
62
|
1 |
|
* |
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
|
|
|
public function isBlankOr($rule) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$this->allow_blank = true; |
75
|
|
|
$this->reverse = false; |
76
|
3 |
|
return $this->init(func_get_args()); |
77
|
|
|
} |
78
|
3 |
|
|
79
|
3 |
|
/** |
80
|
3 |
|
* |
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
|
|
|
public function isNot($rule) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$this->allow_blank = false; |
93
|
|
|
$this->reverse = true; |
94
|
1 |
|
return $this->init(func_get_args()); |
95
|
|
|
} |
96
|
1 |
|
|
97
|
1 |
|
/** |
98
|
1 |
|
* |
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
|
9 |
|
public function isBlankOrNot($rule) |
|
|
|
|
109
|
|
|
{ |
110
|
9 |
|
$this->allow_blank = true; |
111
|
9 |
|
$this->reverse = true; |
112
|
2 |
|
return $this->init(func_get_args()); |
113
|
2 |
|
} |
114
|
9 |
|
|
115
|
3 |
|
/** |
116
|
3 |
|
* |
117
|
9 |
|
* Returns the default failure message for this rule specification. |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
* |
121
|
|
|
*/ |
122
|
|
|
protected function getDefaultMessage() |
123
|
|
|
{ |
124
|
|
|
$message = $this->field . ' should'; |
125
|
|
|
|
126
|
|
|
if (! $this->rule) { |
127
|
|
|
return $message . ' have been blank'; |
128
|
|
|
} |
129
|
9 |
|
|
130
|
|
|
if ($this->allow_blank) { |
131
|
9 |
|
$message .= ' have been blank or'; |
132
|
3 |
|
} |
133
|
|
|
|
134
|
|
|
if ($this->reverse) { |
135
|
7 |
|
$message .= ' not'; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
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
|
|
|
protected function applyRule($subject) |
151
|
|
|
{ |
152
|
|
|
if (! $this->rule) { |
153
|
|
|
return false; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$field = $this->field; |
157
|
|
|
if (! isset($subject->$field)) { |
158
|
|
|
return false; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if ($this->reverse) { |
162
|
|
|
return ! parent::applyRule($subject); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return parent::applyRule($subject); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.