We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| 1 | <?php |
||
| 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 AnyOf( |
|
| 32 | 30 | new Not( |
|
| 33 | 30 | new Contains('--') |
|
| 34 | ), |
||
| 35 | 30 | new AllOf( |
|
| 36 | 30 | new StartsWith('xn--'), |
|
| 37 | 30 | new Callback(function ($str) { |
|
| 38 | 18 | return mb_substr_count($str, '--') == 1; |
|
| 39 | 30 | }) |
|
| 40 | ) |
||
| 41 | ) |
||
| 42 | ); |
||
| 43 | 30 | } |
|
| 44 | |||
| 45 | 30 | public function tldCheck($do = true) |
|
| 46 | { |
||
| 47 | 30 | if ($do === true) { |
|
| 48 | 30 | $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 | 30 | return true; |
|
| 60 | } |
||
| 61 | |||
| 62 | 12 | public function validate($input) |
|
| 63 | { |
||
| 64 | 12 | foreach ($this->checks as $chk) { |
|
| 65 | 12 | if (!$chk->validate($input)) { |
|
| 66 | 12 | return false; |
|
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | 12 | if (count($parts = explode('.', $input)) < 2 |
|
| 71 | 12 | || !$this->tld->validate(array_pop($parts))) { |
|
| 72 | return false; |
||
| 73 | } |
||
| 74 | |||
| 75 | 12 | foreach ($parts as $p) { |
|
| 76 | 12 | if (!$this->otherParts->validate($p)) { |
|
| 77 | 12 | return false; |
|
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | 12 | return true; |
|
| 82 | } |
||
| 83 | |||
| 84 | 15 | public function assert($input) |
|
| 85 | { |
||
| 86 | 15 | $e = []; |
|
| 87 | 15 | foreach ($this->checks as $chk) { |
|
| 88 | 15 | $this->collectAssertException($e, $chk, $input); |
|
| 89 | } |
||
| 90 | |||
| 91 | 15 | if (count($parts = explode('.', $input)) >= 2) { |
|
| 92 | 13 | $this->collectAssertException($e, $this->tld, array_pop($parts)); |
|
| 93 | } |
||
| 94 | |||
| 95 | 15 | foreach ($parts as $p) { |
|
| 96 | 15 | $this->collectAssertException($e, $this->otherParts, $p); |
|
| 97 | } |
||
| 98 | |||
| 99 | 15 | if (count($e)) { |
|
| 100 | 9 | throw $this->reportError($input)->setRelated($e); |
|
| 101 | } |
||
| 102 | |||
| 103 | 6 | return true; |
|
| 104 | } |
||
| 105 | |||
| 106 | 15 | protected function collectAssertException(&$exceptions, $validator, $input) |
|
| 114 | |||
| 115 | 15 | public function check($input) |
|
| 116 | { |
||
| 117 | 15 | foreach ($this->checks as $chk) { |
|
| 118 | 15 | $chk->check($input); |
|
| 131 | } |
||
| 132 |