Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
26 / 26 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| RegEx | |
100.00% |
26 / 26 |
|
100.00% |
5 / 5 |
15 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getNumberToSatisfy | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setNumberToSatisfy | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| evaluate | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
9 | |||
| generateDefaultMessage | |
100.00% |
2 / 2 |
|
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 | * RegEx 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 RegEx extends AbstractValidator |
| 28 | { |
| 29 | |
| 30 | /** |
| 31 | * Number of regex's that need to be satisgied |
| 32 | * @var int |
| 33 | */ |
| 34 | protected $numberToSatisfy = null; |
| 35 | |
| 36 | /** |
| 37 | * Constructor |
| 38 | * |
| 39 | * Instantiate the validator object |
| 40 | * |
| 41 | * @param mixed $value |
| 42 | * @param ?string $message |
| 43 | * @param ?int $numberToSatisfy |
| 44 | */ |
| 45 | public function __construct(mixed $value = null, ?string $message = null, ?int $numberToSatisfy = null) |
| 46 | { |
| 47 | parent::__construct($value, $message); |
| 48 | |
| 49 | if ($numberToSatisfy !== null) { |
| 50 | $this->setNumberToSatisfy($numberToSatisfy); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Get the number to satisfy |
| 56 | * |
| 57 | * @return int|null |
| 58 | */ |
| 59 | public function getNumberToSatisfy(): int|null |
| 60 | { |
| 61 | return $this->numberToSatisfy; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Set the number to satisfy |
| 66 | * |
| 67 | * @param int $numberToSatisfy |
| 68 | * @return static |
| 69 | */ |
| 70 | public function setNumberToSatisfy(int $numberToSatisfy): static |
| 71 | { |
| 72 | $this->numberToSatisfy = $numberToSatisfy; |
| 73 | return $this; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Method to evaluate the validator |
| 78 | * |
| 79 | * @param mixed $input |
| 80 | * @return bool |
| 81 | */ |
| 82 | public function evaluate(mixed $input = null): bool |
| 83 | { |
| 84 | // Set the input, if passed |
| 85 | if ($input !== null) { |
| 86 | $this->input = $input; |
| 87 | } |
| 88 | |
| 89 | // Set the default message |
| 90 | if (!$this->hasMessage()) { |
| 91 | $this->generateDefaultMessage(); |
| 92 | } |
| 93 | |
| 94 | $inputValue = ($this->hasKeyField()) ? $this->getKeyFieldValue() : $this->input; |
| 95 | |
| 96 | if (is_array($this->value)) { |
| 97 | if ((int)$this->numberToSatisfy == 0) { |
| 98 | $result = true; |
| 99 | foreach ($this->value as $value) { |
| 100 | if (!preg_match($value, (string)$inputValue)) { |
| 101 | $result = false; |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | return $result; |
| 106 | } else { |
| 107 | $satisfied = 0; |
| 108 | foreach ($this->value as $value) { |
| 109 | $satisfied += (int)preg_match($value, (string)$inputValue); |
| 110 | } |
| 111 | return ($satisfied >= (int)$this->numberToSatisfy); |
| 112 | } |
| 113 | } else { |
| 114 | return (bool)(preg_match($this->value, (string)$inputValue)); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Generate default message |
| 120 | |
| 121 | * @param mixed $name |
| 122 | * @param mixed $value |
| 123 | * @return string |
| 124 | */ |
| 125 | public function generateDefaultMessage(mixed $name = null, mixed $value = null): string |
| 126 | { |
| 127 | $this->message = "The " . (($name !== null) ? "'" . $name . "'" : "input") . " format is not correct."; |
| 128 | return $this->message; |
| 129 | } |
| 130 | |
| 131 | } |