1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Validation\Rules\Converters; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2020 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use DateTimeImmutable; |
22
|
|
|
use DateTimeInterface; |
23
|
|
|
use Limoncello\Validation\Contracts\Errors\ErrorCodes; |
24
|
|
|
use Limoncello\Validation\Contracts\Execution\ContextInterface; |
25
|
|
|
use Limoncello\Validation\I18n\Messages; |
26
|
|
|
use Limoncello\Validation\Rules\ExecuteRule; |
27
|
|
|
use function assert; |
28
|
|
|
use function is_string; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @package Limoncello\Validation |
32
|
|
|
*/ |
33
|
|
|
final class StringToDateTime extends ExecuteRule |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Property key. |
37
|
|
|
*/ |
38
|
|
|
const PROPERTY_FORMAT = self::PROPERTY_LAST + 1; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $format |
42
|
|
|
*/ |
43
|
3 |
|
public function __construct(string $format) |
44
|
|
|
{ |
45
|
3 |
|
assert(!empty($format)); |
46
|
|
|
|
47
|
3 |
|
parent::__construct([ |
48
|
3 |
|
self::PROPERTY_FORMAT => $format, |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param mixed $value |
54
|
|
|
* @param ContextInterface $context |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
* |
58
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
59
|
|
|
*/ |
60
|
2 |
|
public static function execute($value, ContextInterface $context): array |
61
|
|
|
{ |
62
|
2 |
|
$format = $context->getProperties()->getProperty(self::PROPERTY_FORMAT); |
63
|
2 |
|
if (is_string($value) === true && ($parsed = static::parseFromFormat($value, $format)) !== null) { |
|
|
|
|
64
|
1 |
|
return static::createSuccessReply($parsed); |
65
|
2 |
|
} elseif ($value instanceof DateTimeInterface) { |
66
|
1 |
|
return static::createSuccessReply($value); |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
return static::createErrorReply( |
70
|
2 |
|
$context, |
71
|
2 |
|
$value, |
72
|
2 |
|
ErrorCodes::IS_DATE_TIME, |
73
|
2 |
|
Messages::IS_DATE_TIME, |
74
|
2 |
|
[$format] |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $input |
80
|
|
|
* @param string $format |
81
|
|
|
* |
82
|
|
|
* @return DateTimeInterface|null |
|
|
|
|
83
|
|
|
* |
84
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
85
|
|
|
*/ |
86
|
2 |
|
private static function parseFromFormat(string $input, string $format) |
87
|
|
|
{ |
88
|
2 |
|
$parsedOrNull = null; |
89
|
|
|
|
90
|
2 |
|
if (($value = DateTimeImmutable::createFromFormat($format, $input)) !== false) { |
91
|
1 |
|
$parsedOrNull = $value; |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
return $parsedOrNull; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
Late static binding only has effect in subclasses. A
final
class cannot be extended anymore so late static binding cannot occurr. Consider replacingstatic::
withself::
.To learn more about late static binding, please refer to the PHP core documentation.