We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 11 |
Total Lines | 47 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
21 | trait CanValidateDateTime |
||
22 | { |
||
23 | /** |
||
24 | * Finds whether a value is a valid date/time in a specific format. |
||
25 | * |
||
26 | * @param string $format |
||
27 | * @param string $value |
||
28 | * |
||
29 | * @return bool |
||
30 | */ |
||
31 | 76 | private function isDateTime(string $format, string $value): bool |
|
32 | { |
||
33 | $exceptionalFormats = [ |
||
34 | 76 | 'c' => 'Y-m-d\TH:i:sP', |
|
35 | 'r' => 'D, d M Y H:i:s O', |
||
36 | ]; |
||
37 | |||
38 | 76 | $info = date_parse_from_format($exceptionalFormats[$format] ?? $format, $value); |
|
39 | |||
40 | 76 | if (!$this->isDateTimeParsable($info)) { |
|
41 | 30 | return false; |
|
42 | } |
||
43 | |||
44 | 49 | if ($this->isDateFormat($format)) { |
|
45 | 30 | return $this->isDateInformation($info); |
|
46 | } |
||
47 | |||
48 | 19 | return true; |
|
49 | } |
||
50 | |||
51 | 76 | private function isDateTimeParsable(array $info) |
|
52 | { |
||
53 | 76 | return 0 === $info['error_count'] && 0 === $info['warning_count']; |
|
54 | } |
||
55 | |||
56 | 49 | private function isDateFormat(string $format): bool |
|
57 | { |
||
58 | 49 | return preg_match('/[djSFmMnYy]/', $format) > 0; |
|
59 | } |
||
60 | |||
61 | 30 | private function isDateInformation(array $info) |
|
68 | } |
||
69 | } |
||
70 |