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
|
|
|
* A container for validation rules. |
25
|
|
|
*/ |
26
|
|
|
class Registry |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var array<string,callable> Associative array of definition name to function callback |
30
|
|
|
*/ |
31
|
|
|
private $definitions = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array<string,callable> Associative array of definition name to function callback |
35
|
|
|
*/ |
36
|
|
|
private static $defaultDefinitions = [ |
37
|
|
|
'required' => ['Caridea\Validate\Rule\Blank', 'required'], |
38
|
|
|
'not_empty' => ['Caridea\Validate\Rule\Blank', 'notEmpty'], |
39
|
|
|
'not_empty_list' => ['Caridea\Validate\Rule\Blank', 'notEmptyList'], |
40
|
|
|
'one_of' => ['Caridea\Validate\Rule\Compare', 'oneOf'], |
41
|
|
|
'min_length' => ['Caridea\Validate\Rule\Length', 'min'], |
42
|
|
|
'max_length' => ['Caridea\Validate\Rule\Length', 'max'], |
43
|
|
|
'length_equal' => ['Caridea\Validate\Rule\Length', 'equal'], |
44
|
|
|
'length_between' => ['Caridea\Validate\Rule\Length', 'between'], |
45
|
|
|
'like' => ['Caridea\Validate\Rule\Match', 'like'], |
46
|
|
|
'integer' => ['Caridea\Validate\Rule\Compare', 'integer'], |
47
|
|
|
'positive_integer' => ['Caridea\Validate\Rule\Compare', 'positiveInteger'], |
48
|
|
|
'decimal' => ['Caridea\Validate\Rule\Compare', 'decimal'], |
49
|
|
|
'positive_decimal' => ['Caridea\Validate\Rule\Compare', 'positiveDecimal'], |
50
|
|
|
'min_number' => ['Caridea\Validate\Rule\Compare', 'min'], |
51
|
|
|
'max_number' => ['Caridea\Validate\Rule\Compare', 'max'], |
52
|
|
|
'number_between' => ['Caridea\Validate\Rule\Compare', 'between'], |
53
|
|
|
'email' => ['Caridea\Validate\Rule\Match', 'email'], |
54
|
|
|
'iso_date' => ['Caridea\Validate\Rule\Match', 'isoDate'], |
55
|
|
|
'url' => ['Caridea\Validate\Rule\Match', 'url'], |
56
|
|
|
'timezone' => ['Caridea\Validate\Rule\Timezone', 'timezone'], |
57
|
|
|
'equal_to_field' => ['Caridea\Validate\Rule\Compare', 'equalToField'], |
58
|
|
|
'nested_object' => ['Caridea\Validate\Rule\Nested', 'nestedObject'], |
59
|
|
|
'list_of' => ['Caridea\Validate\Rule\Nested', 'listOf'], |
60
|
|
|
'list_of_objects' => ['Caridea\Validate\Rule\Nested', 'listOfObjects'], |
61
|
|
|
'list_of_different_objects' => ['Caridea\Validate\Rule\Nested', 'listOfDifferentObjects'], |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Creates a new Validation rule registry. |
66
|
|
|
*/ |
67
|
4 |
|
public function __construct() |
68
|
|
|
{ |
69
|
4 |
|
$this->definitions = array_merge([], self::$defaultDefinitions); |
|
|
|
|
70
|
4 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Registers rule definitions. |
74
|
|
|
* |
75
|
|
|
* ```php |
76
|
|
|
* $registry = new \Caridea\Validate\Registry(); |
77
|
|
|
* $registry->register([ |
78
|
|
|
* 'adult' => ['My\Validate\AgeRule', 'adult'], |
79
|
|
|
* 'credit_card' => function(){return new CreditCardRule();}, |
80
|
|
|
* 'something' => 'my_function_that_can_be_called' |
81
|
|
|
* ]); |
82
|
|
|
* ``` |
83
|
|
|
* |
84
|
|
|
* @param array<string,callable> $definitions Associative array of definition name to function callback |
85
|
|
|
* @return $this provides a fluent interface |
86
|
|
|
*/ |
87
|
2 |
|
public function register(array $definitions): self |
88
|
|
|
{ |
89
|
2 |
|
foreach ($definitions as $name => $callback) { |
90
|
2 |
|
if (!is_callable($callback)) { |
91
|
1 |
|
throw new \InvalidArgumentException('Values passed to register must be callable'); |
92
|
|
|
} |
93
|
1 |
|
$this->definitions[$name] = $callback; |
94
|
|
|
} |
95
|
1 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Constructs a validation rule. |
100
|
|
|
* |
101
|
|
|
* @param string $name A string name |
102
|
|
|
* @param mixed $arg Optional constructor argument, or an array of arguments |
103
|
|
|
* @return \Caridea\Validate\Rule The instantiated rule |
104
|
|
|
* @throws \InvalidArgumentException if the rule name is not registered |
105
|
|
|
* @throws \UnexpectedValueException if the factory returns a non-Rule |
106
|
|
|
*/ |
107
|
3 |
|
public function factory(string $name, $arg = null): Rule |
108
|
|
|
{ |
109
|
3 |
|
if (!array_key_exists($name, $this->definitions)) { |
110
|
1 |
|
throw new \InvalidArgumentException("No rule registered with name: $name"); |
111
|
|
|
} |
112
|
2 |
|
$vrule = is_array($arg) ? |
113
|
2 |
|
call_user_func_array($this->definitions[$name], $arg) : |
114
|
2 |
|
call_user_func($this->definitions[$name], $arg); |
115
|
2 |
|
if (!$vrule instanceof Rule) { |
116
|
1 |
|
throw new \UnexpectedValueException('Definitions must return Rule objects'); |
117
|
|
|
} |
118
|
1 |
|
return $vrule; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Creates a new Builder using this Repository. |
123
|
|
|
* |
124
|
|
|
* @return \Caridea\Validate\Builder The builder |
125
|
|
|
*/ |
126
|
1 |
|
public function builder(): Builder |
127
|
|
|
{ |
128
|
1 |
|
return new Builder($this); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..