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