Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
23 / 23 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
RegEx | |
100.00% |
23 / 23 |
|
100.00% |
4 / 4 |
12 | |
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 |
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@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
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@nolainteractive.com> |
22 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
23 | * @license http://www.popphp.org/license New BSD License |
24 | * @version 4.0.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->message === null) { |
90 | $this->message = 'The format is not correct.'; |
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 | } |