1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Respect/Validation. |
5
|
|
|
* |
6
|
|
|
* (c) Alexandre Gomes Gaigalas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the "LICENSE.md" |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Respect\Validation\Rules; |
15
|
|
|
|
16
|
|
|
use Respect\Validation\Exceptions\DomainException; |
17
|
|
|
use Respect\Validation\Exceptions\NestedValidationException; |
18
|
|
|
use Respect\Validation\Exceptions\ValidationException; |
19
|
|
|
use Respect\Validation\Validatable; |
20
|
|
|
use function array_merge; |
21
|
|
|
use function array_pop; |
22
|
|
|
use function count; |
23
|
|
|
use function explode; |
24
|
|
|
use function iterator_to_array; |
25
|
|
|
use function mb_substr_count; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Validates whether the input is a valid domain name or not. |
29
|
|
|
* |
30
|
|
|
* @author Alexandre Gomes Gaigalas <[email protected]> |
31
|
|
|
* @author Henrique Moody <[email protected]> |
32
|
|
|
* @author Mehmet Tolga Avcioglu <[email protected]> |
33
|
|
|
* @author Nick Lombard <[email protected]> |
34
|
|
|
* @author Róbert Nagy <[email protected]> |
35
|
|
|
*/ |
36
|
|
|
final class Domain extends AbstractRule |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var Validatable |
40
|
|
|
*/ |
41
|
|
|
private $genericRule; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Validatable |
45
|
|
|
*/ |
46
|
|
|
private $tldRule; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var Validatable |
50
|
|
|
*/ |
51
|
|
|
private $partsRule; |
52
|
|
|
|
53
|
1 |
|
public function __construct(bool $tldCheck = true) |
54
|
|
|
{ |
55
|
1 |
|
$this->genericRule = $this->createGenericRule(); |
56
|
1 |
|
$this->tldRule = $this->createTldRule($tldCheck); |
57
|
1 |
|
$this->partsRule = $this->createPartsRule(); |
58
|
1 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritDoc} |
62
|
|
|
*/ |
63
|
23 |
|
public function assert($input): void |
64
|
|
|
{ |
65
|
23 |
|
$exceptions = []; |
66
|
|
|
|
67
|
23 |
|
$this->collectAssertException($exceptions, $this->genericRule, $input); |
68
|
23 |
|
$this->throwExceptions($exceptions, $input); |
69
|
|
|
|
70
|
17 |
|
$parts = explode('.', (string) $input); |
71
|
17 |
|
if (count($parts) >= 2) { |
72
|
17 |
|
$this->collectAssertException($exceptions, $this->tldRule, array_pop($parts)); |
73
|
|
|
} |
74
|
|
|
|
75
|
17 |
|
foreach ($parts as $part) { |
76
|
17 |
|
$this->collectAssertException($exceptions, $this->partsRule, $part); |
77
|
|
|
} |
78
|
|
|
|
79
|
17 |
|
$this->throwExceptions($exceptions, $input); |
80
|
10 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritDoc} |
84
|
|
|
*/ |
85
|
23 |
|
public function validate($input): bool |
86
|
|
|
{ |
87
|
|
|
try { |
88
|
23 |
|
$this->assert($input); |
89
|
13 |
|
} catch (ValidationException $exception) { |
90
|
13 |
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
10 |
|
return true; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param ValidationException[] $exceptions |
98
|
|
|
* @param mixed $input |
99
|
|
|
*/ |
100
|
23 |
|
private function collectAssertException(array &$exceptions, Validatable $validator, $input): void |
101
|
|
|
{ |
102
|
|
|
try { |
103
|
23 |
|
$validator->assert($input); |
104
|
14 |
|
} catch (NestedValidationException $nestedValidationException) { |
105
|
10 |
|
$exceptions = array_merge( |
106
|
10 |
|
$exceptions, |
107
|
10 |
|
iterator_to_array($nestedValidationException) |
108
|
|
|
); |
109
|
5 |
|
} catch (ValidationException $validationException) { |
110
|
5 |
|
$exceptions[] = $validationException; |
111
|
|
|
} |
112
|
23 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
1 |
|
public function check($input): void |
118
|
|
|
{ |
119
|
|
|
try { |
120
|
1 |
|
$this->assert($input); |
121
|
1 |
|
} catch (NestedValidationException $exception) { |
122
|
|
|
/** @var ValidationException $childException */ |
123
|
1 |
|
foreach ($exception as $childException) { |
124
|
1 |
|
throw $childException; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
throw $exception; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
private function createGenericRule(): Validatable |
132
|
|
|
{ |
133
|
1 |
|
return new AllOf( |
134
|
1 |
|
new StringType(), |
135
|
1 |
|
new NoWhitespace(), |
136
|
1 |
|
new Contains('.'), |
137
|
1 |
|
new Length(3) |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
private function createTldRule(bool $realTldCheck): Validatable |
142
|
|
|
{ |
143
|
1 |
|
if ($realTldCheck) { |
144
|
1 |
|
return new Tld(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return new AllOf( |
148
|
|
|
new Not(new StartsWith('-')), |
149
|
|
|
new NoWhitespace(), |
150
|
|
|
new Length(2) |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
17 |
|
private function createPartsRule(): Validatable |
155
|
|
|
{ |
156
|
1 |
|
return new AllOf( |
157
|
1 |
|
new Alnum('-'), |
158
|
1 |
|
new Not(new StartsWith('-')), |
159
|
1 |
|
new AnyOf( |
160
|
1 |
|
new Not(new Contains('--')), |
161
|
|
|
new Callback(static function ($str) { |
162
|
17 |
|
return mb_substr_count($str, '--') == 1; |
163
|
1 |
|
}) |
164
|
|
|
), |
165
|
1 |
|
new Not(new EndsWith('-')) |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param ValidationException[] $exceptions |
171
|
|
|
* @param mixed $input |
172
|
|
|
*/ |
173
|
23 |
|
private function throwExceptions(array $exceptions, $input): void |
174
|
|
|
{ |
175
|
23 |
|
if (count($exceptions)) { |
176
|
|
|
/** @var DomainException $domainException */ |
177
|
14 |
|
$domainException = $this->reportError($input); |
178
|
14 |
|
$domainException->addChildren($exceptions); |
179
|
|
|
|
180
|
14 |
|
throw $domainException; |
181
|
|
|
} |
182
|
17 |
|
} |
183
|
|
|
} |
184
|
|
|
|