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