Skip to content

Commit 9da8334

Browse files
author
EmilienCo
committed
Global rules: rename + fix #1
1 parent d332690 commit 9da8334

3 files changed

Lines changed: 74 additions & 48 deletions

File tree

‎README.md‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,20 @@ $validator->rule(new \Validation\Required);
9393

9494
#### For multiple fields
9595

96-
A single rule can be applied to multiple fields.<br>
96+
A same rule can be applied to multiple fields. It is called a **global rule**.<br>
9797
It is useful to avoid repeating the same rule, for example in the case of several fields are required.
9898

99-
These “global rules” are applied to each field until calling `breakRule` function.<br>
99+
A global rule will be applied to each field until calling `endGlobalRule` function.<br>
100100

101101
There are two type of rules: 'and' and 'or'.
102102

103103
##### 'and' rule
104104

105-
An 'and' rule is applied to each field added after declaring it.
105+
An 'and' rule is applied to each field added after declaring it.<br>
106+
It is the default type of rule.
106107

107108
```php
108-
$validator->ruleUntilBreak($rule);
109+
$validator->globalRule($rule);
109110
```
110111

111112
##### 'or' rule
@@ -114,18 +115,18 @@ An 'or' rule will pass if at least one of the fields added after declaring it pa
114115
It is typically used with `\Validation\Required`, when at least one of X fields must be filled (for example, at least the user landline or mobile phone number).
115116

116117
```php
117-
$validator->ruleUntilBreak($rule, Validator::OPERATOR_OR, $message);
118+
$validator->globalRule($rule, Validator::OPERATOR_OR, $message);
118119
```
119120

120121
`$message` is a required property. It is the user-facing message that will be returned if none of the fields pass the rule.
121122

122123

123124
##### Break
124125

125-
To break the latest global rule declared:
126+
To end the latest global rule declared:
126127

127128
```php
128-
$validator->breakRule();
129+
$validator->endGlobalRule();
129130
```
130131

131132
### Running the validation
@@ -170,20 +171,20 @@ The following examples validates a simple user information form.
170171
```php
171172
$validator = new Validator;
172173

173-
$validator->ruleuntilbreak(new \Validation\Required)
174+
$validator->globalRule(new \Validation\Required)
174175

175-
->ruleuntilbreak(new \Validation\LengthGreaterThan(3))
176+
->globalRule(new \Validation\LengthGreaterThan(3))
176177
->field('firstname')
177178
->field('lastname')
178-
->breakRule()
179+
->endGlobalRule()
179180

180181
->field('email')
181182
->rule(new \Validation\Email)
182183

183184
->field('dob')
184185
->rule(new \Validation\Dob)
185186

186-
->breakRule();
187+
->endGlobalRule();
187188

188189
if( ! $validator->run($_POST))
189190
print_r($validator->getErrors());

‎sample/full.php‎

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,43 +34,43 @@ public static function isUsernameTaken($username)
3434

3535
$validator = new Validator;
3636

37-
$validator->ruleuntilbreak(new \Validation\Required())
37+
$validator->globalRule(new \Validation\Required())
3838

39-
->ruleuntilbreak(new \Validation\LengthGreaterThan(3))
39+
->globalRule(new \Validation\LengthGreaterThan(3))
4040

41-
->field('firstname')
41+
->field('firstname')
4242

43-
->field('lastname')
43+
->field('lastname')
4444

45-
->field('username')
46-
->rule('User::isUsernameTaken')
45+
->field('username')
46+
->rule('User::isUsernameTaken')
4747

48-
->breakRule()
48+
->endGlobalRule()
4949

50-
->field('email')
51-
->rule(new \Validation\Email)
50+
->field('email')
51+
->rule(new \Validation\Email)
5252

53-
->field('dob')
54-
->rule(new \Validation\Date)
53+
->field('dob')
54+
->rule(new \Validation\Date)
5555

56-
->field('agreement')
57-
->rule(function($value)
58-
{
59-
if($value !== 'I AGREE')
60-
throw new Exception('Please write “I AGREE” if you agree with the rules');
61-
})
56+
->field('agreement')
57+
->rule(function($value)
58+
{
59+
if($value !== 'I AGREE')
60+
throw new Exception('Please write “I AGREE” if you agree with the rules');
61+
})
6262

63-
->field('age')
64-
->rule('ctype_digit')
63+
->field('age')
64+
->rule('ctype_digit')
6565

66-
->breakRule()
66+
->endGlobalRule()
6767

68-
->ruleuntilbreak(new \Validation\Required, Validator::OPERATOR_OR, 'Please indicate your phone number')
68+
->globalRule(new \Validation\Required, Validator::OPERATOR_OR, 'Please indicate your phone number')
6969

7070
->field('landline_number')
7171
->field('mobile_number')
7272

73-
->breakRule();
73+
->endGlobalRule();
7474

7575
if( ! $validator->run($data))
7676
print_r($validator->getErrors());

‎src/validator.php‎

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ class Validator
2222
protected $_global_errors = [ ];
2323

2424
// Hold temporary 'and' and 'or' rules
25-
protected $_temp_and = [ ];
26-
protected $_temp_or = [ ];
25+
protected $_temp_or = [ ];
26+
protected $_temp_and = [ ];
2727

2828
// Hold final 'or' rules
2929
protected $_or = [ ];
3030

31+
// Hold the stack of 'and' and 'or' to break in the right order
32+
private $__and_or_stack = [ ];
33+
3134
/**
3235
* Declare a field
3336
*
@@ -64,7 +67,7 @@ public function __call($name, $arguments)
6467
try
6568
{
6669
if( ! $this->_current_field)
67-
throw new Exception('Please declare a field before calling '.$name.', or use `ruleUntilBreak`', E_ERROR);
70+
throw new Exception('Please declare a field before calling '.$name.', or use `globalRule`', E_ERROR);
6871

6972
$field = $this->_fields[$this->_current_field];
7073

@@ -80,7 +83,7 @@ public function __call($name, $arguments)
8083

8184
/**
8285
* Declare a global rule to be applied to multiple field.
83-
* A global rule is disabled with `breakRule` function.
86+
* A global rule is disabled with `endGlobalRule` function.
8487
*
8588
* - An 'and' rule is applied to each field added after declaring it.
8689
*
@@ -93,8 +96,10 @@ public function __call($name, $arguments)
9396
* @param string $message Error message (in the case of 'or' rule)
9497
* @return \Validator
9598
*/
96-
public function ruleUntilBreak($rule, $operator = self::OPERATOR_AND, $message = null)
99+
public function globalRule($rule, $operator = self::OPERATOR_AND, $message = null)
97100
{
101+
$this->__and_or_stack[] = $operator;
102+
98103
switch($operator)
99104
{
100105
case self::OPERATOR_AND:
@@ -115,24 +120,44 @@ public function ruleUntilBreak($rule, $operator = self::OPERATOR_AND, $message =
115120
];
116121

117122
break;
123+
124+
default:
125+
126+
throw new Exception('Unknown operator \''.$operator.'\' for global rule');
118127
}
119128

120129
return $this;
121130
}
122131

123132
/**
124-
* Break the latest global rule declared.
133+
* Disable the latest global rule declared.
125134
*
126135
* @return \Validator
127136
*/
128-
public function breakRule()
137+
public function endGlobalRule()
129138
{
130-
// Merge temporary 'or' rules
131-
if($this->_temp_or)
132-
$this->_or[] = array_pop($this->_temp_or);
139+
switch(array_pop($this->__and_or_stack))
140+
{
141+
case self::OPERATOR_AND:
142+
143+
// Remove latest pushed 'and' rule
144+
array_pop($this->_temp_and);
133145

134-
// Remove latest pushed 'and' rule
135-
array_pop($this->_temp_and);
146+
break;
147+
148+
case self::OPERATOR_OR:
149+
150+
// Merge temporary 'or' rules
151+
if($this->_temp_or)
152+
$this->_or[] = array_pop($this->_temp_or);
153+
154+
break;
155+
156+
default:
157+
158+
// No more rule in the stack
159+
throw new Exception('Called `endGlobalRule` when no global rule remaining');
160+
}
136161

137162
return $this;
138163
}
@@ -194,7 +219,7 @@ public function run(array $values)
194219

195220
$this->_has_error = false;
196221

197-
$this->_breakAllRules();
222+
$this->_endAllGlobalRules();
198223

199224
// First, run all 'or' validation rules
200225
foreach($this->_or as $or)
@@ -298,11 +323,11 @@ public function setHasError($has_error)
298323
}
299324

300325
/**
301-
* Break all global rules declared till now.
326+
* Close and disable all the remaining global rules.
302327
*
303328
* @return \Validator
304329
*/
305-
protected function _breakAllRules()
330+
protected function _endAllGlobalRules()
306331
{
307332
// Merge temporary 'or' rules
308333
if($this->_temp_or)

0 commit comments

Comments
 (0)