1 | <?php |
||
20 | class NotEmpty extends Rule |
||
21 | { |
||
22 | use StringifyCallbackTrait; |
||
23 | |||
24 | /** |
||
25 | * The error code for when a value is empty while this is not allowed. |
||
26 | */ |
||
27 | const EMPTY_VALUE = 'NotEmpty::EMPTY_VALUE'; |
||
28 | |||
29 | /** |
||
30 | * The templates for the possible messages this validator can return. |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $messageTemplates = [ |
||
35 | self::EMPTY_VALUE => '{{ name }} must not be empty', |
||
36 | ]; |
||
37 | |||
38 | /** |
||
39 | * Denotes whether or not the chain should be stopped after this rule. |
||
40 | * |
||
41 | * @var bool |
||
42 | */ |
||
43 | protected $shouldBreak; |
||
44 | |||
45 | /** |
||
46 | * Indicates if the value can be empty. |
||
47 | * |
||
48 | * @var bool |
||
49 | */ |
||
50 | protected $allowEmpty; |
||
51 | |||
52 | /** |
||
53 | * Optionally contains a callable to overwrite the allow empty requirement on time of validation. |
||
54 | * |
||
55 | * @var callable |
||
56 | */ |
||
57 | protected $allowEmptyCallback; |
||
58 | |||
59 | /** |
||
60 | * Contains the input container. |
||
61 | * |
||
62 | * @var Container |
||
63 | */ |
||
64 | protected $input; |
||
65 | |||
66 | /** |
||
67 | * Construct the NotEmpty validator. |
||
68 | * |
||
69 | * @param bool $allowEmpty |
||
70 | */ |
||
71 | 153 | public function __construct($allowEmpty) |
|
75 | |||
76 | /** |
||
77 | * @return bool |
||
78 | */ |
||
79 | 143 | public function shouldBreakChain() |
|
83 | |||
84 | /** |
||
85 | * Ensures a certain key has a value. |
||
86 | * |
||
87 | * @param mixed $value |
||
88 | * @return bool |
||
89 | */ |
||
90 | 143 | public function validate($value) |
|
99 | |||
100 | /** |
||
101 | * Determines whether or not value $value is to be considered "empty". |
||
102 | * |
||
103 | * @param mixed $value |
||
104 | * @return bool |
||
105 | */ |
||
106 | 143 | protected function isEmpty($value) |
|
118 | |||
119 | /** |
||
120 | * @inheritdoc |
||
121 | * |
||
122 | * @param string $key |
||
123 | * @param Container $input |
||
124 | * @return bool |
||
125 | */ |
||
126 | 143 | public function isValid($key, Container $input) |
|
132 | |||
133 | /** |
||
134 | * Set a callable or boolean value to potentially alter the allow empty requirement at the time of validation. |
||
135 | * |
||
136 | * This may be incredibly useful for conditional validation. |
||
137 | * |
||
138 | * @param callable|bool $allowEmpty |
||
139 | * @return $this |
||
140 | */ |
||
141 | 4 | public function setAllowEmpty($allowEmpty) |
|
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | 7 | protected function getMessageParameters() |
|
159 | |||
160 | /** |
||
161 | * Overwrite the allow empty requirement after instantiation of this rule. |
||
162 | * |
||
163 | * @param bool $allowEmpty |
||
164 | * @return $this |
||
165 | */ |
||
166 | 2 | protected function overwriteAllowEmpty($allowEmpty) |
|
171 | |||
172 | /** |
||
173 | * Set the callback to execute to determine whether or not the rule should allow empty. |
||
174 | * |
||
175 | * @param callable $allowEmptyCallback |
||
176 | * @return $this |
||
177 | */ |
||
178 | 2 | protected function setAllowEmptyCallback(callable $allowEmptyCallback) |
|
183 | |||
184 | /** |
||
185 | * Determines whether or not the value may be empty. |
||
186 | * |
||
187 | * @param Container $input |
||
188 | * @return bool |
||
189 | */ |
||
190 | 7 | protected function allowEmpty(Container $input) |
|
197 | } |
||
198 |