|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package Fuel\Validation |
|
4
|
|
|
* @version 2.0 |
|
5
|
|
|
* @author Fuel Development Team |
|
6
|
|
|
* @license MIT License |
|
7
|
|
|
* @copyright 2010 - 2013 Fuel Development Team |
|
8
|
|
|
* @link http://fuelphp.com |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Fuel\Validation\RuleProvider; |
|
12
|
|
|
|
|
13
|
|
|
use ArrayAccess; |
|
14
|
|
|
use Fuel\Validation\ValidationAwareInterface; |
|
15
|
|
|
use Fuel\Validation\Validator; |
|
16
|
|
|
use InvalidArgumentException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Allows sets of validation rules to be generated from an array structure |
|
20
|
|
|
* |
|
21
|
|
|
* @package Fuel\Validation\RuleProvider |
|
22
|
|
|
* @author Fuel Development Team |
|
23
|
|
|
* @since 2.0 |
|
24
|
|
|
*/ |
|
25
|
|
|
class FromArray implements ValidationAwareInterface |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $data; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The key of label |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $labelKey = 'label'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* The key of rules |
|
42
|
|
|
* |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $ruleKey = 'rules'; |
|
46
|
|
|
|
|
47
|
3 |
|
public function __construct($labelKey = null, $ruleKey = 'rules') |
|
48
|
|
|
{ |
|
49
|
3 |
|
if ($labelKey !== true) |
|
50
|
|
|
{ |
|
51
|
3 |
|
$this->labelKey = $labelKey; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
3 |
|
$this->ruleKey = $ruleKey; |
|
55
|
3 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Sets the array that will be used to generate |
|
59
|
|
|
* |
|
60
|
|
|
* @param array $data |
|
61
|
|
|
* |
|
62
|
|
|
* @return $this |
|
63
|
|
|
* |
|
64
|
|
|
* @throws InvalidArgumentException |
|
65
|
|
|
* |
|
66
|
|
|
* @since 2.0 |
|
67
|
|
|
*/ |
|
68
|
2 |
|
public function setData($data) |
|
69
|
|
|
{ |
|
70
|
2 |
|
if ( ! is_array($data) && ! $data instanceof ArrayAccess) |
|
71
|
|
|
{ |
|
72
|
|
|
throw new InvalidArgumentException('VAL-008: $data must be an array or implement the ArrayAccess interface.'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
2 |
|
$this->data = $data; |
|
76
|
|
|
|
|
77
|
2 |
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return array |
|
82
|
|
|
* |
|
83
|
|
|
* @since 2.0 |
|
84
|
|
|
*/ |
|
85
|
3 |
|
public function getData() |
|
86
|
|
|
{ |
|
87
|
3 |
|
return $this->data; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Should populate the given validator with the needed rules. |
|
92
|
|
|
* |
|
93
|
|
|
* @param Validator $validator |
|
94
|
|
|
* |
|
95
|
|
|
* @return Validator |
|
96
|
|
|
* |
|
97
|
|
|
* @throws InvalidArgumentException |
|
98
|
|
|
* |
|
99
|
|
|
* @since 2.0 |
|
100
|
|
|
*/ |
|
101
|
3 |
|
public function populateValidator(Validator $validator) |
|
102
|
|
|
{ |
|
103
|
3 |
|
$data = $this->getData(); |
|
104
|
|
|
|
|
105
|
3 |
|
if ($data === null) |
|
106
|
|
|
{ |
|
107
|
1 |
|
throw new InvalidArgumentException('VAL-005: No data specified. Please call setData() first.'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
// Loop through and add all the rules |
|
111
|
2 |
|
foreach ($data as $field => $rules) |
|
112
|
|
|
{ |
|
113
|
2 |
|
$this->addFieldRules($field, $rules, $validator); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
2 |
|
return $validator; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Processes the given field and rules to add them to the validator. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $field Name of the field to add rules to |
|
123
|
|
|
* @param array $rules Array of any rules to be added to the field |
|
124
|
|
|
* @param Validator $validator Validator object to apply rules to |
|
125
|
|
|
* |
|
126
|
|
|
* @since 2.0 |
|
127
|
|
|
*/ |
|
128
|
2 |
|
protected function addFieldRules($field, $rules, Validator $validator) |
|
129
|
|
|
{ |
|
130
|
2 |
|
$label = null; |
|
131
|
|
|
|
|
132
|
2 |
|
if ( ! empty($this->labelKey)) |
|
133
|
|
|
{ |
|
134
|
1 |
|
if (array_key_exists($this->labelKey, $rules)) |
|
135
|
|
|
{ |
|
136
|
1 |
|
$label = $rules[$this->labelKey]; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
1 |
|
if (array_key_exists($this->ruleKey, $rules)) |
|
140
|
|
|
{ |
|
141
|
1 |
|
$rules = $rules[$this->ruleKey]; |
|
142
|
|
|
} |
|
143
|
|
|
else |
|
144
|
|
|
{ |
|
145
|
|
|
$rules = array(); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
2 |
|
$validator->addField($field, $label); |
|
150
|
|
|
|
|
151
|
|
|
// Add each of the rules |
|
152
|
2 |
|
foreach ($rules as $ruleName => $params) |
|
153
|
|
|
{ |
|
154
|
2 |
|
$this->addFieldRule($field, $ruleName, $params, $validator); |
|
155
|
|
|
} |
|
156
|
2 |
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Adds an individual rule for the given field to the given validator. |
|
160
|
|
|
* If the $ruleName is numeric the function will assume that $params is the rule name and that there are no |
|
161
|
|
|
* parameters. |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $fieldName |
|
164
|
|
|
* @param string|int $ruleName |
|
165
|
|
|
* @param mixed $params |
|
166
|
|
|
* @param Validator $validator |
|
167
|
|
|
* |
|
168
|
|
|
* @since 2.0 |
|
169
|
|
|
*/ |
|
170
|
2 |
|
protected function addFieldRule($fieldName, $ruleName, $params, Validator $validator) |
|
171
|
|
|
{ |
|
172
|
|
|
// If $ruleName is numeric assume that $params is the name and there are no actual parameters |
|
173
|
2 |
|
if (is_numeric($ruleName)) |
|
174
|
|
|
{ |
|
175
|
2 |
|
$ruleName = $params; |
|
176
|
2 |
|
$params = []; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
// Create and add the rule |
|
180
|
2 |
|
$ruleInstance = $validator->createRuleInstance($ruleName, $params); |
|
181
|
2 |
|
$validator->addRule($fieldName, $ruleInstance); |
|
182
|
2 |
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @return string |
|
186
|
|
|
*/ |
|
187
|
|
|
public function getLabelKey() |
|
188
|
|
|
{ |
|
189
|
|
|
return $this->labelKey; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @return string |
|
194
|
|
|
*/ |
|
195
|
|
|
public function getRuleKey() |
|
196
|
|
|
{ |
|
197
|
|
|
return $this->ruleKey; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
} |
|
201
|
|
|
|