1 | <?php |
||
13 | trait DateTypeTrait |
||
14 | { |
||
15 | /** |
||
16 | * Extract date parts from the given string. |
||
17 | * |
||
18 | * Will return a stdClass with 3 properties: year, month, day. Each property defaults to null. If the string cannot |
||
19 | * be parsed, return null. |
||
20 | * |
||
21 | * @param string $dateString |
||
22 | * @param string $delimiterPattern regex pattern to use when searching for delimiters (excluding the surrounding /'s |
||
23 | * @param bool normalizeYear indicates if a two digit year should get normalized to a 4 digit year |
||
24 | * |
||
25 | * @return null|\stdClass |
||
26 | */ |
||
27 | 63 | protected function extractDateParts(string $dateString, string $delimiterPattern, bool $normalizeYear): ?\stdClass { |
|
47 | |||
48 | /** |
||
49 | * Sanitizes the given dateString to YYYYMMDD. |
||
50 | * |
||
51 | * Returns null if the string cannot be sanitized. |
||
52 | * |
||
53 | * @param string $dateString |
||
54 | * @param string $delimiterPattern regex pattern to use when searching for delimiters (excluding the surrounding /'s |
||
55 | * @param bool normalizeYear indicates if a two digit year should get normalized to a 4 digit year |
||
56 | * |
||
57 | * @return null|string |
||
58 | */ |
||
59 | 63 | private function sanitizeDateString(string $dateString, string $delimiterPattern, bool $normalizeYear): ?string |
|
74 | |||
75 | /** |
||
76 | * Strips delimiters from the given date string and returns the sanitized string. |
||
77 | * |
||
78 | * The returned string will be either |
||
79 | * |
||
80 | * @param string $dateString |
||
81 | * @param string $delimiterPattern regex pattern to use when searching for delimiters (excluding the surrounding /'s |
||
82 | * |
||
83 | * @return null|string |
||
84 | */ |
||
85 | 63 | private function handleDelimiters(string $dateString, string $delimiterPattern): ?string |
|
107 | |||
108 | /** |
||
109 | * Converts YYMMDD to YYYYMMDD. |
||
110 | * |
||
111 | * @param string $dateString |
||
112 | * |
||
113 | * @return null|string |
||
114 | */ |
||
115 | 60 | private function normalizeYear(string $dateString): ?string |
|
126 | } |
||
127 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: