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