/
Variable.php
334 lines (298 loc) · 8.87 KB
/
Variable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
namespace Pv;
abstract class Variable
{
/**
* Object validation methods
* @var array
*/
protected $validate = array();
/**
* Object's current value
* @var mixed
*/
protected $value = null;
/**
* Negate the evaluation or not
* @var boolean
*/
protected $negate = false;
public function __construct($value,$type=null)
{
$this->value = $value;
$type = ($type == null) ? 'none' : $type;
$this->setupValidation($type);
}
/**
* Set up the validation objects for the variable
*
* @param mixed $type Either string/array of validation options
*
* @return null
*/
private function setupValidation($type)
{
if (!is_array($type)) {
$type = array($type);
}
foreach ($type as $index => $t) {
if ($t instanceof \Closure) {
$valid = $t($this->value);
} else {
$valid = $this->buildValidationObject($t, $this->value);
}
if (strtolower($t) == 'none' || $valid->isAllowedType(get_class($this)) == true) {
$this->appendValidation($index, $valid);
}
}
}
/**
* Create the validation object based on the string
*
* @param string $validationString Validation string definition
* @param mixed $value Value to assign to validator
*
* @return \Pv\Validate $valid Validation object
*/
public function buildValidationObject($validationString, $value)
{
$negate = false;
// see if we have a "not:" to negate
if (strpos($validationString, 'not:') !== false) {
$validationString = str_replace('not:', '', $validationString);
$negate = true;
}
// see if we have parameters to pass
$addlParams = array();
if (strpos($validationString, '[') !== false) {
preg_match('#(.+?)\[(.+?)\]#', $validationString, $params);
$validationString = $params[1];
$addlParams = explode(',', $params[2]);
}
$validation = "\Pv\Validate\\".ucwords(strtolower($validationString));
$valid = new $validation($value);
if (!empty($addlParams)) {
$valid->setParams($addlParams);
}
if ($negate == true) {
$valid->negate();
}
return $valid;
}
/**
* Append the validation to the validation set
*
* @param string $index Index (name) for the validation
* @param mixed $valid A Validation object (extends \Pv\Validate)
*
* @return null
*/
public function appendValidation($index, $valid)
{
// get the current validation and append
$currentValidation = $this->getValidation();
$newValidation = array_merge(
$currentValidation,
array($index => $valid)
);
$this->validate = $newValidation;
}
/**
* Execute the validation on the variable's current value
*
* @param mixed $index Eithe name or ID of validtor to execute
*
* @return boolean $pass Pass/fail result from validation
*/
public function validate($index=null)
{
$pass = true;
// see if we're just trying to run one validator
if ($index !== null && isset($this->validate[$index])) {
$this->execValidation($this->validate[$index], $index);
} else {
foreach ($this->validate as $index => $validate) {
$this->execValidation($validate, $index);
}
}
return $pass;
}
/**
* Execute a single validation without adding it to the object
*
* @param string $validationString Validation definition
*
* @return null
*/
public function single($validationString)
{
$validate = $this->buildValidationObject($validationString, $this->value);
$this->execValidatorObject($validate, null, false);
return $this;
}
/**
* Execute the validator
*
* @param object $validate Validator object (either \Pv\Validate\Validate or a \Closure)
* @param string $index Validator index (in validation set)
*
* @throws Exception Validation failure
*
* @return null
*/
public function execValidation($validate, $index)
{
if (is_bool($validate)) {
$this->execValidatorClosure($validate, $index);
} else {
$this->execValidatorObject($validate, $index);
}
}
/**
* Validate a normal \Pv\Validate\Validate object
*
* @param \Pv\Validate\Validate $validate Validation object
* @param mixed $index Index of the validator
* @param boolean $return Return the validation back to the object
*
* @return null
*/
private function execValidatorObject($validate, $index, $return=true)
{
$ret = $validate->run();
$check = false;
if ($validate->isNegated() == true) {
$check = true;
}
if ($ret == $check) {
$pass = false;
if ($return == true) {
$this->validate[$index]->fail();
}
$msg = 'Failure on validation '.get_class($validate);
if ($validate->isNegated()) {
$msg .= ' (negated)';
}
throw new ValidationException($msg);
} else {
if ($return == true) {
$this->validate[$index]->pass();
}
}
}
/**
* Execute a validation closure
*
* @param boolean $validate Result of validation closure execution
* @param mixed $index Index of the validator in the set
*
* @return null
*/
private function execValidatorClosure($validate, $index)
{
if ($validate == false) {
throw new ValidationException(
'Failure on validation Closure at index '.$index
);
}
}
/**
* Get the current variable's value
*
* @return mixed Variable value
*/
public function getValue()
{
return $this->value;
}
/**
* Set the value for the variable
*
* @param mixed $value Variable value
*
* @return boolean If value was set correctly
*/
public function setValue($value)
{
return $this->value = $value;
}
/**
* Return the current set of validation methods
*
* @return array Set of validation objects
*/
public function getValidation()
{
return $this->validate;
}
/**
* Add additional validation options to the variable
*
* @param mixed $types Either a string/array of types to add & options
* @param string $name Optional name for the validator
*
* @return null;
*/
public function addValidation($types, $name=null)
{
if (!is_array($types)) {
$types = ($name !== null)
? array($name => $types) : array($types);
}
$this->setupValidation($types);
}
/**
* Remove validation from an object
*
* @param mixed $index Integer/string key for the validation to remove
*
* @return boolean Pass/fail on removal
*/
public function removeValidation($index)
{
if (isset($this->validate[$index])) {
unset($this->validate[$index]);
return true;
} else {
return false;
}
}
/**
* Convert the object to the given type
*
* @param string $type Type to convert to
*
* @return an object of the new type
*/
public function convert($type)
{
// see if we have an object of that type
$toType = ucwords(strtolower($type));
$file = __DIR__.'/P'.$type.'.php';
if (is_file($file)) {
// see if we have the conversion method
$method = 'to'.ucwords(strtolower($type));
if (method_exists($this, $method)) {
// try to make an object
$objectType = '\Pv\P'.$type;
$obj = new $objectType($this->$method());
// filter through the validations on the object and be sure
// they can stick around
foreach ($this->getValidation() as $index => $valid) {
// can it go on the new object?
if ($valid->isAllowedType('P'.$type) == true){
$obj->appendValidation($index, $valid);
}
}
return $obj;
} else {
throw new \Pv\ConversionException(
'Cannot convert to type "'.$type.'"'
);
}
} else {
throw new \Pv\ConversionException('Invalid conversion type "'.$type.'"');
}
}
}
?>