Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
84.62% |
11 / 13 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| Hostname | |
84.62% |
11 / 13 |
|
0.00% |
0 / 1 |
11.44 | |
0.00% |
0 / 1 |
| __construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| isValid | |
84.62% |
11 / 13 |
|
0.00% |
0 / 1 |
10.36 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Validation\Network |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Validation\Network; |
| 16 | |
| 17 | use phpOMS\Validation\ValidatorAbstract; |
| 18 | |
| 19 | /** |
| 20 | * Validate hostname. |
| 21 | * |
| 22 | * @package phpOMS\Validation\Network |
| 23 | * @license OMS License 2.0 |
| 24 | * @link https://jingga.app |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | abstract class Hostname extends ValidatorAbstract |
| 28 | { |
| 29 | /** |
| 30 | * Constructor. |
| 31 | * |
| 32 | * @since 1.0.0 |
| 33 | * @codeCoverageIgnore |
| 34 | */ |
| 35 | private function __construct() |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * {@inheritdoc} |
| 41 | * |
| 42 | * A IPv6 string MUST be in [...] to be sucessfully validated |
| 43 | */ |
| 44 | public static function isValid(mixed $value, array $constraints = null) : bool |
| 45 | { |
| 46 | //return \filter_var(\gethostbyname($value), \FILTER_VALIDATE_IP) !== false; |
| 47 | |
| 48 | if (!\is_string($value)) { |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | if (empty($value) |
| 53 | || \strlen($value) > 256 |
| 54 | || !\preg_match('/^([a-zA-Z\d.-]*|\[[a-fA-F\d:]+\])$/', $value) |
| 55 | ) { |
| 56 | return false; |
| 57 | } elseif (\strlen($value) > 2 && \substr($value, 0, 1) === '[' && \substr($value, -1, 1) === ']') { |
| 58 | return \filter_var(\substr($value, 1, -1), \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6) !== false; |
| 59 | } elseif (\is_numeric(\str_replace('.', '', $value))) { |
| 60 | return \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4) !== false; |
| 61 | } elseif (\filter_var('http://' . $value, \FILTER_VALIDATE_URL) !== false) { |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | return false; |
| 66 | } |
| 67 | } |