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\Rule; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* A set of several Rule objects. |
25
|
|
|
* |
26
|
|
|
* @copyright 2015-2018 LibreWorks contributors |
27
|
|
|
* @license Apache-2.0 |
28
|
|
|
*/ |
29
|
|
|
class Set implements \Caridea\Validate\Rule, \IteratorAggregate, \Countable |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var \Caridea\Validate\Rule[] The rules |
33
|
|
|
*/ |
34
|
|
|
private $rules = []; |
35
|
|
|
/** |
36
|
|
|
* @var string Optional error code to return |
37
|
|
|
*/ |
38
|
|
|
private $error; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Creates a new Set. |
42
|
|
|
* |
43
|
|
|
* @param \Caridea\Validate\Rule[] $rules The rules to add |
44
|
|
|
* @param string|null $error Optional error code to return |
45
|
|
|
* @throws \InvalidArgumentException if `$rules` contains invalid types |
46
|
|
|
*/ |
47
|
8 |
|
public function __construct(array $rules = [], ?string $error = null) |
48
|
|
|
{ |
49
|
8 |
|
$this->addAll($rules); |
50
|
8 |
|
$this->error = $error; |
51
|
8 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Adds one or more `Rule`s into this `Set`. |
55
|
|
|
* |
56
|
|
|
* @param \Caridea\Validate\Rule ...$rules The rules to add |
57
|
|
|
* @return $this Provides a fluent interface |
58
|
|
|
*/ |
59
|
6 |
|
public function add(\Caridea\Validate\Rule ...$rules): self |
60
|
|
|
{ |
61
|
6 |
|
$this->rules = array_merge($this->rules, $rules); |
62
|
6 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Adds several `Rule`s into this `Set`. |
67
|
|
|
* |
68
|
|
|
* @param \Caridea\Validate\Rule[] $rules The rules to add |
69
|
|
|
* @return $this Provides a fluent interface |
70
|
|
|
* @throws \InvalidArgumentException if `$rules` contains invalid types |
71
|
|
|
*/ |
72
|
5 |
|
public function addAll(array $rules): self |
73
|
|
|
{ |
74
|
|
|
try { |
75
|
5 |
|
return $this->add(...$rules); |
76
|
1 |
|
} catch (\TypeError $e) { |
77
|
1 |
|
throw new \InvalidArgumentException('Only Rule objects are allowed', 0, $e); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Adds the entries from another `Set` into this one. |
83
|
|
|
* |
84
|
|
|
* @param \Caridea\Validate\Rule\Set $rules The rules to add |
85
|
|
|
* @return $this Provides a fluent interface |
86
|
|
|
*/ |
87
|
1 |
|
public function merge(Set $rules): self |
88
|
|
|
{ |
89
|
1 |
|
$this->rules = array_merge($this->rules, $rules->rules); |
90
|
1 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Gets the error code, or `null` |
95
|
|
|
* |
96
|
|
|
* @return string|null The error code or `null` |
97
|
|
|
*/ |
98
|
1 |
|
public function getError(): ?string |
99
|
|
|
{ |
100
|
1 |
|
return $this->error; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Sets the error code. |
105
|
|
|
* |
106
|
|
|
* @param string|null $error The error code or `null` |
107
|
|
|
* @return $this provides a fluent interface |
108
|
|
|
*/ |
109
|
1 |
|
public function setError(?string $error): self |
110
|
|
|
{ |
111
|
1 |
|
$this->error = $error; |
112
|
1 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Count elements of an object |
117
|
|
|
* |
118
|
|
|
* @return int Custom count as an integer |
119
|
|
|
*/ |
120
|
1 |
|
public function count(): int |
121
|
|
|
{ |
122
|
1 |
|
return count($this->rules); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Retrieve an external iterator |
127
|
|
|
* |
128
|
|
|
* @return \Traversable An instance of an object implementing Iterator or Traversable |
129
|
|
|
*/ |
130
|
1 |
|
public function getIterator(): \Traversable |
131
|
|
|
{ |
132
|
1 |
|
return new \ArrayIterator($this->rules); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritDoc} |
137
|
|
|
*/ |
138
|
2 |
|
public function apply($value, $data = []): ?array |
139
|
|
|
{ |
140
|
2 |
|
$errors = []; |
141
|
2 |
|
$empty = $value === null || $value === ''; |
142
|
2 |
|
foreach ($this->rules as $rule) { |
143
|
2 |
|
$error = (!$empty || $rule instanceof Blank) ? |
144
|
2 |
|
$rule->apply($value, $data) : null; |
145
|
2 |
|
if ($error !== null) { |
146
|
2 |
|
$errors = $error; |
147
|
2 |
|
break; |
148
|
|
|
} |
149
|
|
|
} |
150
|
2 |
|
return empty($errors) ? null : ($this->error !== null ? [$this->error] : $errors); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|