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