1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Caridea |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
7
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
8
|
|
|
* the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
15
|
|
|
* License for the specific language governing permissions and limitations under |
16
|
|
|
* the License. |
17
|
|
|
* |
18
|
|
|
* @copyright 2015-2016 LibreWorks contributors |
19
|
|
|
* @license http://opensource.org/licenses/Apache-2.0 Apache 2.0 License |
20
|
|
|
*/ |
21
|
|
|
namespace Caridea\Validate; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* An immutable set of rules for a set of fields that can validate data. |
25
|
|
|
* |
26
|
|
|
* @copyright 2015-2016 LibreWorks contributors |
27
|
|
|
* @license http://opensource.org/licenses/Apache-2.0 Apache 2.0 License |
28
|
|
|
*/ |
29
|
|
|
class Validator |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var array Associative array of field name to list of rules |
33
|
|
|
*/ |
34
|
|
|
protected $ruleset; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Creates a new validator. |
38
|
|
|
* |
39
|
|
|
* @param array $ruleset Associative array of field name to list of rules |
40
|
1 |
|
*/ |
41
|
|
|
public function __construct(array $ruleset) |
42
|
1 |
|
{ |
43
|
1 |
|
$this->ruleset = $ruleset; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Gets a field from the values. |
48
|
|
|
* |
49
|
|
|
* This can be overridden to access by other means (e.g. object properties, |
50
|
|
|
* getter methods). |
51
|
|
|
* |
52
|
|
|
* @param mixed $values The values |
53
|
|
|
* @param string $field The field to access |
54
|
|
|
* @return mixed The accessed value |
55
|
1 |
|
*/ |
56
|
|
|
protected function access($values, string $field) |
57
|
1 |
|
{ |
58
|
|
|
return isset($values[$field]) ? $values[$field] : null; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Iterates over the ruleset and collects any error codes. |
63
|
|
|
* |
64
|
|
|
* @param object|array $values An object or associative array to validate |
65
|
|
|
* @return array Associative array of field name to error |
66
|
|
|
* @throws \InvalidArgumentException if `$values` is null |
67
|
2 |
|
*/ |
68
|
|
|
protected function iterate($values) |
69
|
2 |
|
{ |
70
|
1 |
|
if (!is_object($values) && !is_array($values)) { |
71
|
|
|
throw new \InvalidArgumentException("Unable to validate provided object"); |
72
|
1 |
|
} |
73
|
1 |
|
$errors = []; |
74
|
1 |
|
foreach ($this->ruleset as $field => $rules) { |
75
|
1 |
|
$value = $this->access($values, $field); |
76
|
1 |
|
$empty = $value === null || $value === ''; |
77
|
1 |
|
foreach ($rules as $rule) { |
78
|
1 |
|
$error = (!$empty || $rule instanceof Rule\Blank) ? |
79
|
1 |
|
$rule->apply($value, $values) : null; |
80
|
1 |
|
if (is_array($error) && count($error) > 0) { |
81
|
1 |
|
$errors[$field] = count($error) > 1 ? $error : current($error); |
82
|
|
|
break; |
83
|
1 |
|
} elseif ($error !== null) { |
84
|
1 |
|
$errors[$field] = $error; |
85
|
1 |
|
break; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
return $errors; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Validates the provided value, returning a result object. |
94
|
|
|
* |
95
|
1 |
|
* @param object|array $values An object or associative array to validate |
96
|
|
|
* @return \Caridea\Validate\Result The validation results |
97
|
1 |
|
* @throws \InvalidArgumentException if `$values` is null |
98
|
|
|
*/ |
99
|
|
|
public function validate($values): Result |
100
|
|
|
{ |
101
|
|
|
return new Result($this->iterate($values)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Validates the provided value, throwing an exception upon failure. |
106
|
|
|
* |
107
|
2 |
|
* @param object|array $values An object or associative array to validate |
108
|
|
|
* @throws \Caridea\Validate\Exception\Invalid if validation fails |
109
|
2 |
|
* @throws \InvalidArgumentException if `$values` is null |
110
|
2 |
|
*/ |
111
|
1 |
|
public function assert($values) |
112
|
|
|
{ |
113
|
1 |
|
$errors = $this->iterate($values); |
114
|
|
|
if (!empty($errors)) { |
115
|
|
|
throw new Exception\Invalid($errors); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|