88use DateTimeInterface ;
99use Nette \Forms \Controls \BaseControl ;
1010use Nette \Forms \Form ;
11- use Nette \Utils \Arrays ;
1211use Nette \Utils \Html ;
1312use function class_exists ;
1413use function count ;
@@ -34,7 +33,7 @@ abstract class AbstractDateTimeInput extends BaseControl
3433
3534 protected string $ htmlClass = '' ;
3635
37- protected string |object $ invalidValueMessage = 'Invalid format ' ;
36+ protected string |\ Stringable $ invalidValueMessage = 'Invalid format ' ;
3837
3938 /**
4039 * @param string|string[] $format
@@ -52,21 +51,17 @@ public function __construct(?string $label = null, $format = null)
5251 * @param string|string[] $format
5352 * @return static
5453 */
55- public function setFormat ($ format )
54+ public function setFormat (string | array $ format ): static
5655 {
57- if (is_string ($ format )) {
58- $ format = [$ format ];
59- }
56+ $ formats = is_array ($ format ) ? $ format : [$ format ];
6057
61- if (!is_array ($ format ) || /** @phpstan-ignore-line */
62- Arrays::some ($ format , function ($ item ) {
63- return !is_string ($ item );
64- })
65- ) {
66- throw new InvalidArgumentException ('Format must be either string or array of strings. ' );
58+ foreach ($ formats as $ item ) {
59+ if (!is_string ($ item )) {
60+ throw new InvalidArgumentException ('Format must be either string or array of strings. ' );
61+ }
6762 }
6863
69- $ this ->parser ->setFormats ($ format );
64+ $ this ->parser ->setFormats ($ formats );
7065 return $ this ;
7166 }
7267
@@ -75,28 +70,25 @@ public function setFormat($format)
7570 * @phpstan-param class-string|callable(DateTimeInterface): mixed $type
7671 * @return static
7772 */
78- public function setValueType ($ type )
73+ public function setValueType (string | callable $ type ): static
7974 {
8075 $ this ->valueTransformation = $ this ->createValueTransformation ($ type );
8176 return $ this ;
8277 }
8378
8479 /**
8580 * @template T
86- * @param string|callable $type
8781 * @phpstan-param class-string<T>|callable(DateTimeInterface): ?T $type
8882 * @return callable(DateTimeInterface): ?T
8983 */
90- protected function createValueTransformation ($ type )
84+ protected function createValueTransformation (string | callable $ type ): callable
9185 {
9286 if (is_callable ($ type )) {
9387 return $ type ;
94- } elseif (is_string ($ type )) { /** @phpstan-ignore-line */
95- if (!class_exists ($ type ) || class_implements ($ type ) === false || !in_array ('DateTimeInterface ' , class_implements ($ type ), true )) {
96- throw new InvalidArgumentException ('Value type must be existing class implementing \DateTimeInterface ' );
97- }
98- } else {
99- throw new InvalidArgumentException ('Value type can be only string with class name or or callback. ' );
88+ }
89+
90+ if (!class_exists ($ type ) || class_implements ($ type ) === false || !in_array ('DateTimeInterface ' , class_implements ($ type ), true )) {
91+ throw new InvalidArgumentException ('Value type must be existing class implementing \DateTimeInterface ' );
10092 }
10193
10294 if ($ type === 'Nette\Utils\DateTime ' || (class_parents ($ type ) !== false && in_array ('Nette\Utils\DateTime ' , class_parents ($ type ), true ))) {
@@ -111,10 +103,7 @@ protected function createValueTransformation($type)
111103 }
112104 }
113105
114- /**
115- * @return mixed
116- */
117- protected function transformValue (?DateTimeImmutable $ value )
106+ protected function transformValue (?DateTimeImmutable $ value ): mixed
118107 {
119108 if ($ value === null ) {
120109 return null ;
@@ -130,7 +119,7 @@ protected function transformValue(?DateTimeImmutable $value)
130119 /**
131120 * @return static
132121 */
133- public function addFormat (string $ format )
122+ public function addFormat (string $ format ): static
134123 {
135124 $ this ->parser ->addFormat ($ format );
136125 return $ this ;
@@ -190,26 +179,17 @@ public function setDefaultValue($value)
190179 return parent ::setDefaultValue ($ value );
191180 }
192181
193- /**
194- * @return mixed
195- */
196- public function getValue ()
182+ public function getValue (): mixed
197183 {
198184 return $ this ->transformValue ($ this ->getValueAsDateTimeImmutable ());
199185 }
200186
201- /**
202- * @return mixed
203- */
204- public function getRawValue ()
187+ public function getRawValue (): mixed
205188 {
206189 return parent ::getValue ();
207190 }
208191
209- /**
210- * @return ?DateTimeImmutable
211- */
212- protected function getValueAsDateTimeImmutable ()
192+ protected function getValueAsDateTimeImmutable (): ?DateTimeImmutable
213193 {
214194 if ($ this ->value === null || $ this ->value === '' ) {
215195 return null ;
@@ -226,12 +206,10 @@ protected function getValueAsDateTimeImmutable()
226206
227207 /**
228208 * @template T
229- * @param string|callable $type
230209 * @phpstan-param class-string<T>|callable(DateTimeInterface): ?T $type
231- * @return mixed
232210 * @phpstan-return ?T
233211 */
234- public function getValueAs ($ type )
212+ public function getValueAs (string | callable $ type ): mixed
235213 {
236214 $ value = $ this ->getValueAsDateTimeImmutable ();
237215 if ($ value === null ) {
@@ -243,29 +221,26 @@ public function getValueAs($type)
243221 }
244222
245223 /** @phpstan-var ?T */
224+
246225 return $ value ;
247226 }
248227
249228 /**
250- * @param string|object $message
251229 * @return static
252230 */
253- public function setInvalidValueMessage ($ message )
231+ public function setInvalidValueMessage (string | \ Stringable $ message ): static
254232 {
255233 $ this ->invalidValueMessage = $ message ;
256234 return $ this ;
257235 }
258236
259237 /**
260- * @param callable|string $validator
261- * @param string|object $message
262- * @param mixed $arg
263238 * @return static
264239 */
265- public function addRule ($ validator , $ message = null , $ arg = null )
240+ public function addRule (callable | string $ validator , string | \ Stringable | null $ message = null , mixed $ arg = null ): static
266241 {
267242 switch ($ validator ) {
268- case Form::MIN :
243+ case Form::Min :
269244 if (!$ arg instanceof DateTimeInterface) {
270245 throw new InvalidArgumentException ('Rule parameter expected to be \DateTimeInterface ' );
271246 }
@@ -275,7 +250,7 @@ public function addRule($validator, $message = null, $arg = null)
275250 $ validator = static ::class . '::validateMin ' ;
276251 break ;
277252
278- case Form::MAX :
253+ case Form::Max :
279254 if (!$ arg instanceof DateTimeInterface) {
280255 throw new InvalidArgumentException ('Rule parameter expected to be \DateTimeInterface ' );
281256 }
@@ -285,7 +260,7 @@ public function addRule($validator, $message = null, $arg = null)
285260 $ validator = static ::class . '::validateMax ' ;
286261 break ;
287262
288- case Form::RANGE :
263+ case Form::Range :
289264 if (!is_array ($ arg ) || !$ arg [0 ] instanceof DateTimeInterface || !$ arg [1 ] instanceof DateTimeInterface) {
290265 throw new InvalidArgumentException ('Rule parameter expected to be 2 item array [min, max] of \DateTimeInterface ' );
291266 }
@@ -311,7 +286,6 @@ public function getMin(): ?DateTimeInterface
311286 return $ this ->min ;
312287 }
313288
314-
315289 public function getMax (): ?DateTimeInterface
316290 {
317291 return $ this ->max ;
0 commit comments