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\ValidationException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Alexandre Gomes Gaigalas <[email protected]> |
21
|
|
|
* @author Henrique Moody <[email protected]> |
22
|
|
|
* @author Mehmet Tolga Avcioglu <[email protected]> |
23
|
|
|
* @author Nick Lombard <[email protected]> |
24
|
|
|
* @author Róbert Nagy <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class Domain extends AbstractComposite |
27
|
|
|
{ |
28
|
|
|
protected $tld; |
29
|
|
|
protected $checks = []; |
30
|
|
|
protected $otherParts; |
31
|
|
|
|
32
|
36 |
|
public function __construct($tldCheck = true) |
33
|
|
|
{ |
34
|
36 |
|
$this->checks[] = new NoWhitespace(); |
35
|
36 |
|
$this->checks[] = new Contains('.'); |
36
|
36 |
|
$this->checks[] = new Length(3, null); |
37
|
36 |
|
$this->tldCheck($tldCheck); |
38
|
36 |
|
$this->otherParts = new AllOf( |
39
|
36 |
|
new Alnum('-'), |
40
|
36 |
|
new Not(new StartsWith('-')), |
41
|
36 |
|
new AnyOf( |
42
|
36 |
|
new Not(new Contains('--')), |
43
|
|
|
new Callback(function ($str) { |
44
|
24 |
|
return 1 == mb_substr_count($str, '--'); |
45
|
36 |
|
}) |
46
|
|
|
), |
47
|
36 |
|
new Not(new EndsWith('-')) |
48
|
|
|
); |
49
|
|
|
|
50
|
36 |
|
parent::__construct(); |
51
|
36 |
|
} |
52
|
|
|
|
53
|
36 |
|
public function tldCheck($do = true) |
54
|
|
|
{ |
55
|
36 |
|
if (true === $do) { |
56
|
36 |
|
$this->tld = new Tld(); |
57
|
|
|
} else { |
58
|
4 |
|
$this->tld = new AllOf( |
59
|
4 |
|
new Not( |
60
|
4 |
|
new StartsWith('-') |
61
|
|
|
), |
62
|
4 |
|
new NoWhitespace(), |
63
|
4 |
|
new Length(2, null) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
36 |
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
18 |
|
public function validate($input): bool |
71
|
|
|
{ |
72
|
18 |
|
foreach ($this->checks as $chk) { |
73
|
18 |
|
if (!$chk->validate($input)) { |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
18 |
|
if (count($parts = explode('.', (string) $input)) < 2 |
79
|
18 |
|
|| !$this->tld->validate(array_pop($parts))) { |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
18 |
|
foreach ($parts as $p) { |
84
|
18 |
|
if (!$this->otherParts->validate($p)) { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
18 |
|
return true; |
90
|
|
|
} |
91
|
|
|
|
92
|
18 |
|
public function assert($input): void |
93
|
|
|
{ |
94
|
18 |
|
$exceptions = []; |
95
|
18 |
|
foreach ($this->checks as $chk) { |
96
|
18 |
|
$this->collectAssertException($exceptions, $chk, $input); |
97
|
|
|
} |
98
|
|
|
|
99
|
18 |
|
if (count($parts = explode('.', (string) $input)) >= 2) { |
100
|
16 |
|
$this->collectAssertException($exceptions, $this->tld, array_pop($parts)); |
101
|
|
|
} |
102
|
|
|
|
103
|
18 |
|
foreach ($parts as $p) { |
104
|
18 |
|
$this->collectAssertException($exceptions, $this->otherParts, $p); |
105
|
|
|
} |
106
|
|
|
|
107
|
18 |
|
if (count($exceptions)) { |
108
|
|
|
/** @var DomainException $domainException */ |
109
|
9 |
|
$domainException = $this->reportError($input); |
110
|
9 |
|
$domainException->addChildren($exceptions); |
111
|
|
|
|
112
|
9 |
|
throw $domainException; |
113
|
|
|
} |
114
|
9 |
|
} |
115
|
|
|
|
116
|
18 |
|
protected function collectAssertException(&$exceptions, $validator, $input): void |
117
|
|
|
{ |
118
|
|
|
try { |
119
|
18 |
|
$validator->assert($input); |
120
|
9 |
|
} catch (ValidationException $e) { |
121
|
9 |
|
$exceptions[] = $e; |
122
|
|
|
} |
123
|
18 |
|
} |
124
|
|
|
|
125
|
18 |
|
public function check($input): void |
126
|
|
|
{ |
127
|
18 |
|
foreach ($this->checks as $chk) { |
128
|
18 |
|
$chk->check($input); |
129
|
|
|
} |
130
|
|
|
|
131
|
16 |
|
if (count($parts = explode('.', $input)) >= 2) { |
132
|
16 |
|
$this->tld->check(array_pop($parts)); |
133
|
|
|
} |
134
|
|
|
|
135
|
12 |
|
foreach ($parts as $p) { |
136
|
12 |
|
$this->otherParts->check($p); |
137
|
|
|
} |
138
|
9 |
|
} |
139
|
|
|
} |
140
|
|
|
|