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