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 | 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 | ), |
||
| 37 | 36 | new Not(new EndsWith('-')) |
|
| 38 | ); |
||
| 39 | 36 | } |
|
| 40 | |||
| 41 | 36 | public function tldCheck($do = true) |
|
| 42 | { |
||
| 43 | 36 | if ($do === true) { |
|
| 44 | 36 | $this->tld = new Tld(); |
|
| 45 | } else { |
||
| 46 | 4 | $this->tld = new AllOf( |
|
| 47 | 4 | new Not( |
|
| 48 | 4 | new StartsWith('-') |
|
| 49 | ), |
||
| 50 | 4 | new NoWhitespace(), |
|
| 51 | 4 | new Length(2, null) |
|
| 52 | ); |
||
| 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 | 18 | return false; |
|
| 63 | } |
||
| 64 | } |
||
| 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 | 18 | return false; |
|
| 74 | } |
||
| 75 | } |
||
| 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 | } |
||
| 86 | |||
| 87 | 18 | if (count($parts = explode('.', $input)) >= 2) { |
|
| 88 | 16 | $this->collectAssertException($e, $this->tld, array_pop($parts)); |
|
| 89 | } |
||
| 90 | |||
| 91 | 18 | foreach ($parts as $p) { |
|
| 92 | 18 | $this->collectAssertException($e, $this->otherParts, $p); |
|
| 93 | } |
||
| 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) |
|
| 110 | |||
| 111 | 18 | public function check($input) |
|
| 112 | { |
||
| 113 | 18 | foreach ($this->checks as $chk) { |
|
| 114 | 18 | $chk->check($input); |
|
| 127 | } |
||
| 128 |