Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
14 / 14 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
AbstractValidator | |
100.00% |
14 / 14 |
|
100.00% |
9 / 9 |
10 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getMessage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getInput | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getResults | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasResults | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setValue | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
setMessage | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
setInput | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
evaluate | n/a |
0 / 0 |
n/a |
0 / 0 |
0 |
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 | * 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 | abstract class AbstractValidator implements ValidatorInterface |
27 | { |
28 | |
29 | /** |
30 | * Validator value to test against |
31 | * @var mixed |
32 | */ |
33 | protected mixed $value = null; |
34 | |
35 | /** |
36 | * Input value to test |
37 | * @var mixed |
38 | */ |
39 | protected mixed $input = null; |
40 | |
41 | /** |
42 | * Validator message |
43 | * - The message provided when the validation fails |
44 | * @var ?string |
45 | */ |
46 | protected ?string $message = null; |
47 | |
48 | /** |
49 | * Validator results |
50 | * - Optional results to collect post-validation, would be something that was |
51 | * set by a custom validator in its "evaluate" method |
52 | * @var mixed |
53 | */ |
54 | protected mixed $results = null; |
55 | |
56 | /** |
57 | * Constructor |
58 | * |
59 | * Instantiate the validator object |
60 | * |
61 | * @param mixed $value |
62 | * @param ?string $message |
63 | */ |
64 | public function __construct(mixed $value = null, ?string $message = null) |
65 | { |
66 | $this->setValue($value); |
67 | if ($message !== null) { |
68 | $this->setMessage($message); |
69 | } |
70 | } |
71 | |
72 | /** |
73 | * Get the validator value |
74 | * |
75 | * @return mixed |
76 | */ |
77 | public function getValue(): mixed |
78 | { |
79 | return $this->value; |
80 | } |
81 | |
82 | /** |
83 | * Get the validator default message |
84 | * |
85 | * @return string|null |
86 | */ |
87 | public function getMessage(): string|null |
88 | { |
89 | return $this->message; |
90 | } |
91 | |
92 | /** |
93 | * GEt the validator input |
94 | * |
95 | * @return mixed |
96 | */ |
97 | public function getInput(): mixed |
98 | { |
99 | return $this->input; |
100 | } |
101 | |
102 | /** |
103 | * Get the validator results |
104 | * |
105 | * @return mixed |
106 | */ |
107 | public function getResults(): mixed |
108 | { |
109 | return $this->results; |
110 | } |
111 | |
112 | /** |
113 | * Has validator results |
114 | * |
115 | * @return bool |
116 | */ |
117 | public function hasResults(): bool |
118 | { |
119 | return !empty($this->results); |
120 | } |
121 | |
122 | /** |
123 | * Set the validator value |
124 | * |
125 | * @param mixed $value |
126 | * @return AbstractValidator |
127 | */ |
128 | public function setValue(mixed $value): AbstractValidator |
129 | { |
130 | $this->value = $value; |
131 | return $this; |
132 | } |
133 | |
134 | /** |
135 | * Set the validator condition |
136 | * |
137 | * @param ?string $message |
138 | * @return AbstractValidator |
139 | */ |
140 | public function setMessage(?string $message = null): AbstractValidator |
141 | { |
142 | $this->message = $message; |
143 | return $this; |
144 | } |
145 | |
146 | /** |
147 | * Set the validator input |
148 | * |
149 | * @param mixed $input |
150 | * @return AbstractValidator |
151 | */ |
152 | public function setInput(mixed $input = null): AbstractValidator |
153 | { |
154 | $this->input = $input; |
155 | return $this; |
156 | } |
157 | |
158 | /** |
159 | * Evaluate |
160 | |
161 | * @param mixed $input |
162 | * @return bool |
163 | */ |
164 | abstract public function evaluate(mixed $input = null): bool; |
165 | |
166 | } |