Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
25 / 25 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| RegEx | |
100.00% |
25 / 25 |
|
100.00% |
5 / 5 |
14 | |
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% |
17 / 17 |
|
100.00% |
1 / 1 |
8 | |||
| generateDefaultMessage | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Pop PHP Framework (http://www.popphp.org/) |
| 4 | * |
| 5 | * @link https://github.com/popphp/popphp-framework |
| 6 | * @author Nick Sagona, III <dev@noladev.com> |
| 7 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. |
| 8 | * @license http://www.popphp.org/license New BSD License |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * @namespace |
| 13 | */ |
| 14 | namespace Pop\Validator; |
| 15 | |
| 16 | /** |
| 17 | * RegEx validator class |
| 18 | * |
| 19 | * @category Pop |
| 20 | * @package Pop\Validator |
| 21 | * @author Nick Sagona, III <dev@noladev.com> |
| 22 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. |
| 23 | * @license http://www.popphp.org/license New BSD License |
| 24 | * @version 4.5.0 |
| 25 | */ |
| 26 | class RegEx extends AbstractValidator |
| 27 | { |
| 28 | |
| 29 | /** |
| 30 | * Number of regex's that need to be satisgied |
| 31 | * @var int |
| 32 | */ |
| 33 | protected $numberToSatisfy = null; |
| 34 | |
| 35 | /** |
| 36 | * Constructor |
| 37 | * |
| 38 | * Instantiate the validator object |
| 39 | * |
| 40 | * @param mixed $value |
| 41 | * @param ?string $message |
| 42 | * @param ?int $numberToSatisfy |
| 43 | */ |
| 44 | public function __construct(mixed $value = null, ?string $message = null, ?int $numberToSatisfy = null) |
| 45 | { |
| 46 | parent::__construct($value, $message); |
| 47 | |
| 48 | if ($numberToSatisfy !== null) { |
| 49 | $this->setNumberToSatisfy($numberToSatisfy); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get the number to satisfy |
| 55 | * |
| 56 | * @return int|null |
| 57 | */ |
| 58 | public function getNumberToSatisfy(): int|null |
| 59 | { |
| 60 | return $this->numberToSatisfy; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Set the number to satisfy |
| 65 | * |
| 66 | * @param int $numberToSatisfy |
| 67 | * @return RegEx |
| 68 | */ |
| 69 | public function setNumberToSatisfy(int $numberToSatisfy): Regex |
| 70 | { |
| 71 | $this->numberToSatisfy = $numberToSatisfy; |
| 72 | return $this; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Method to evaluate the validator |
| 77 | * |
| 78 | * @param mixed $input |
| 79 | * @return bool |
| 80 | */ |
| 81 | public function evaluate(mixed $input = null): bool |
| 82 | { |
| 83 | // Set the input, if passed |
| 84 | if ($input !== null) { |
| 85 | $this->input = $input; |
| 86 | } |
| 87 | |
| 88 | // Set the default message |
| 89 | if (!$this->hasMessage()) { |
| 90 | $this->generateDefaultMessage(); |
| 91 | } |
| 92 | |
| 93 | if (is_array($this->value)) { |
| 94 | if ((int)$this->numberToSatisfy == 0) { |
| 95 | $result = true; |
| 96 | foreach ($this->value as $value) { |
| 97 | if (!preg_match($value, $this->input)) { |
| 98 | $result = false; |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | return $result; |
| 103 | } else { |
| 104 | $satisfied = 0; |
| 105 | foreach ($this->value as $value) { |
| 106 | $satisfied += (int)preg_match($value, $this->input); |
| 107 | } |
| 108 | return ($satisfied >= (int)$this->numberToSatisfy); |
| 109 | } |
| 110 | } else { |
| 111 | return (bool)(preg_match($this->value, $this->input)); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Generate default message |
| 117 | |
| 118 | * @param mixed $name |
| 119 | * @param mixed $value |
| 120 | * @return string |
| 121 | */ |
| 122 | public function generateDefaultMessage(mixed $name = null, mixed $value = null): string |
| 123 | { |
| 124 | $this->message = "The " . (($name !== null) ? "'" . $name . "'" : "value") . " format is not correct."; |
| 125 | return $this->message; |
| 126 | } |
| 127 | |
| 128 | } |