Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| IsSubnetOf | |
100.00% |
11 / 11 |
|
100.00% |
2 / 2 |
7 | |
100.00% |
1 / 1 |
| evaluate | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
| generateDefaultMessage | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <nick@popphp.org> |
| 8 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Validator; |
| 16 | |
| 17 | /** |
| 18 | * Is subnet of validator class |
| 19 | * |
| 20 | * @category Pop |
| 21 | * @package Pop\Validator |
| 22 | * @author Nick Sagona, III <nick@popphp.org> |
| 23 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 24 | * @license https://www.popphp.org/license New BSD License |
| 25 | * @version 5.0.0 |
| 26 | */ |
| 27 | class IsSubnetOf extends AbstractValidator |
| 28 | { |
| 29 | |
| 30 | /** |
| 31 | * Method to evaluate the validator |
| 32 | * |
| 33 | * @param mixed $input |
| 34 | * @throws Exception |
| 35 | * @return bool |
| 36 | */ |
| 37 | public function evaluate(mixed $input = null): bool |
| 38 | { |
| 39 | // Check to make sure the input is a valid Ipv4 address. |
| 40 | if (!(new Ipv4())->evaluate($input)) { |
| 41 | throw new Exception('The IP address must be a valid IPv4 address.'); |
| 42 | } |
| 43 | |
| 44 | // Set the input, if passed |
| 45 | if ($input !== null) { |
| 46 | $this->input = $input; |
| 47 | } |
| 48 | |
| 49 | // Set the default message |
| 50 | if (!$this->hasMessage()) { |
| 51 | $this->generateDefaultMessage(); |
| 52 | } |
| 53 | |
| 54 | $inputValue = ($this->hasKeyField()) ? $this->getKeyFieldValue() : $this->input; |
| 55 | |
| 56 | return (substr((string)$inputValue, 0, strrpos((string)$inputValue, '.')) == $this->value); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Generate default message |
| 61 | |
| 62 | * @param mixed $name |
| 63 | * @param mixed $value |
| 64 | * @return string |
| 65 | */ |
| 66 | public function generateDefaultMessage(mixed $name = null, mixed $value = null): string |
| 67 | { |
| 68 | $this->message = "The " . (($name !== null) ? "'" . $name . "'" : "input") . |
| 69 | " must be part of the subnet '" . ($value ?? $this->value) . "'."; |
| 70 | |
| 71 | return $this->message; |
| 72 | } |
| 73 | |
| 74 | } |