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-2018 LibreWorks contributors |
19
|
|
|
* @license Apache-2.0 |
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-2018 LibreWorks contributors |
27
|
|
|
* @license Apache-2.0 |
28
|
|
|
*/ |
29
|
|
|
class Validator |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var array<string,\Caridea\Validate\Rule\Set> Associative array of field name to rule set |
33
|
|
|
*/ |
34
|
|
|
protected $ruleset; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Creates a new validator. |
38
|
|
|
* |
39
|
|
|
* @param array<string,\Caridea\Validate\Rule\Set> $ruleset Associative array of field name to rule set |
40
|
|
|
*/ |
41
|
1 |
|
public function __construct(array $ruleset) |
42
|
|
|
{ |
43
|
1 |
|
$this->ruleset = $ruleset; |
44
|
1 |
|
} |
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
|
|
|
*/ |
56
|
1 |
|
protected function access($values, string $field) |
57
|
|
|
{ |
58
|
1 |
|
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
|
|
|
*/ |
68
|
2 |
|
protected function iterate($values) |
69
|
|
|
{ |
70
|
2 |
|
if (!is_object($values) && !is_array($values)) { |
71
|
1 |
|
throw new \InvalidArgumentException("Unable to validate provided object"); |
72
|
|
|
} |
73
|
1 |
|
$errors = []; |
74
|
1 |
|
foreach ($this->ruleset as $field => $rules) { |
75
|
1 |
|
$value = $this->access($values, $field); |
76
|
1 |
|
$error = $rules->apply($value, $values); |
|
|
|
|
77
|
1 |
|
if ($error !== null) { |
78
|
1 |
|
$errors[$field] = count($error) > 1 || count(array_filter(array_keys($error), 'is_string')) > 0 ? |
79
|
1 |
|
$error : current($error); |
80
|
|
|
} |
81
|
|
|
} |
82
|
1 |
|
return $errors; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Validates the provided value, returning a result object. |
87
|
|
|
* |
88
|
|
|
* @param object|array $values An object or associative array to validate |
89
|
|
|
* @return \Caridea\Validate\Result The validation results |
90
|
|
|
* @throws \InvalidArgumentException if `$values` is null |
91
|
|
|
*/ |
92
|
1 |
|
public function validate($values): Result |
93
|
|
|
{ |
94
|
1 |
|
return new Result($this->iterate($values)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Validates the provided value, throwing an exception upon failure. |
99
|
|
|
* |
100
|
|
|
* @param object|array $values An object or associative array to validate |
101
|
|
|
* @throws \Caridea\Validate\Exception\Invalid if validation fails |
102
|
|
|
* @throws \InvalidArgumentException if `$values` is null |
103
|
|
|
*/ |
104
|
2 |
|
public function assert($values) |
105
|
|
|
{ |
106
|
2 |
|
$errors = $this->iterate($values); |
107
|
2 |
|
if (!empty($errors)) { |
108
|
1 |
|
throw new Exception\Invalid($errors); |
109
|
|
|
} |
110
|
1 |
|
} |
111
|
|
|
} |
112
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.