Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
77.78% |
7 / 9 |
CRAP | |
91.43% |
64 / 70 |
Date | |
0.00% |
0 / 1 |
|
77.78% |
7 / 9 |
27.46 | |
91.43% |
64 / 70 |
format | |
0.00% |
0 / 1 |
8.10 | |
88.46% |
23 / 26 |
|||
cleanDate | |
100.00% |
1 / 1 |
1 | |
100.00% |
6 / 6 |
|||
getExplode | |
100.00% |
1 / 1 |
3 | |
100.00% |
4 / 4 |
|||
buildDateFromFourTeenDigital | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
buildDateFromTwelveDigital | |
100.00% |
1 / 1 |
3 | |
100.00% |
8 / 8 |
|||
buildDateFromTeenDigital | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
buildDateFromEightDigit | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
buildDateFromSixDigit | |
0.00% |
0 / 1 |
5.68 | |
70.00% |
7 / 10 |
|||
convertDate | |
100.00% |
1 / 1 |
3 | |
100.00% |
4 / 4 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\DictionaryFormatter\Formatters; |
4 | |
5 | use Illuminate\Support\Facades\Log; |
6 | use MongoDB\BSON\UTCDateTime; |
7 | use DateTime; |
8 | use Exception; |
9 | use Qmp\Laravel\DictionaryFormatter\Facades\DictionaryFormatter; |
10 | |
11 | class Date |
12 | { |
13 | |
14 | /** |
15 | * @var string |
16 | */ |
17 | protected static $raw; |
18 | |
19 | /** |
20 | * @var array |
21 | */ |
22 | protected static $months = [ |
23 | '01' => 'january|janvier', |
24 | '02' => 'february|fevrier', |
25 | '03' => 'march|mars', |
26 | '04' => 'april|avril', |
27 | '05' => 'may|mai', |
28 | '06' => 'june|juin', |
29 | '07' => 'july|juillet', |
30 | '08' => 'august|aout', |
31 | '09' => 'september|septembre', |
32 | '10' => 'october|octobre', |
33 | '11' => 'november|novembre', |
34 | '12' => 'december|decembre' |
35 | ]; |
36 | |
37 | /** |
38 | * @param string $value |
39 | * @param string $format |
40 | * @param string|null $in |
41 | * @return UTCDateTime|string |
42 | */ |
43 | public static function format(string $value, string $format = 'Y-m-d H:i:s', string $in = null) |
44 | { |
45 | try { |
46 | |
47 | self::$raw = $value; |
48 | |
49 | if($in) { |
50 | return self::convertDate(DateTime::createFromFormat($in, $value), $format); |
51 | } |
52 | |
53 | self::cleanDate($value); |
54 | |
55 | $explode = self::getExplode($value); |
56 | $value = implode('', $explode); |
57 | |
58 | switch (strlen($value)) { |
59 | case 14: |
60 | self::buildDateFromFourTeenDigital($value); |
61 | break; |
62 | case 12: |
63 | self::buildDateFromTwelveDigital($value, $explode); |
64 | break; |
65 | case 10: |
66 | self::buildDateFromTeenDigital($value); |
67 | break; |
68 | case 8; |
69 | self::buildDateFromEightDigit($value); |
70 | break; |
71 | case 6; |
72 | self::buildDateFromSixDigit($value); |
73 | break; |
74 | } |
75 | |
76 | return self::convertDate(new DateTime($value), $format); |
77 | |
78 | } catch (Exception $e) { |
79 | Log::debug('Unable to format date:' . var_export(['message' => $e->getMessage(), 'line' => $e->getLine(), 'file' => $e->getFile()], true)); |
80 | return self::$raw; |
81 | } |
82 | } |
83 | |
84 | /** |
85 | * @param string $date |
86 | */ |
87 | protected static function cleanDate(string &$date) |
88 | { |
89 | $date = DictionaryFormatter::stripaccents($date); |
90 | |
91 | collect(self::$months)->each(function ($month, $number) use (&$date) { |
92 | $date = preg_replace('/'.strtolower($month).'/i', $number, strtolower($date)); |
93 | }); |
94 | |
95 | $date = preg_replace('#\s+#',' ',trim(preg_replace('#[^0-9]#',' ',$date))); |
96 | } |
97 | |
98 | /** |
99 | * @param string $value |
100 | * @return false|string[] |
101 | */ |
102 | protected static function getExplode(string $value) |
103 | { |
104 | $explode = explode(' ',$value); |
105 | |
106 | foreach ($explode as $key => $number) { |
107 | $explode[$key] = intval($number) < 10 ? '0' . intval($number) : $number; |
108 | } |
109 | |
110 | return $explode; |
111 | } |
112 | |
113 | /** |
114 | * @param string $date |
115 | */ |
116 | protected static function buildDateFromFourTeenDigital(string &$date): void |
117 | { |
118 | preg_match('/([0-9]{8})([0-9]{2})([0-9]{2})([0-9]{2})/', $date, $matches); |
119 | |
120 | self::buildDateFromEightDigit($matches[1]); |
121 | |
122 | $date = "$matches[1] $matches[2]:$matches[3]:$matches[4]"; |
123 | |
124 | } |
125 | |
126 | /** |
127 | * @param string $date |
128 | * @param array $explode |
129 | */ |
130 | protected static function buildDateFromTwelveDigital(string &$date, array $explode): void |
131 | { |
132 | $day = implode('', array_slice($explode,0,3)); |
133 | |
134 | if(count($explode) == 6) { |
135 | self::buildDateFromSixDigit($day); |
136 | $date = "$day $explode[3]:$explode[4]:$explode[5]"; |
137 | } |
138 | |
139 | if(count($explode) == 5) { |
140 | self::buildDateFromEightDigit($day); |
141 | $date = "$day $explode[3]:$explode[4]:00"; |
142 | } |
143 | } |
144 | |
145 | /** |
146 | * @param string $date |
147 | */ |
148 | protected static function buildDateFromTeenDigital(string &$date): void |
149 | { |
150 | preg_match('/([0-9]{6})([0-9]{2})([0-9]{2})/', $date, $matches); |
151 | |
152 | self::buildDateFromSixDigit($matches[1]); |
153 | |
154 | $date = "$matches[1] $matches[2]:$matches[3]:00"; |
155 | } |
156 | |
157 | /** |
158 | * @param string $date |
159 | */ |
160 | protected static function buildDateFromEightDigit(string &$date): void |
161 | { |
162 | if(substr($date,4,4)>1900) { |
163 | $date = preg_replace('#([0-9]{2})([0-9]{2})([0-9]{4})#','$3-$2-$1',$date); |
164 | } else { |
165 | $date = preg_replace('#([0-9]{4})([0-9]{2})([0-9]{2})#','$1-$2-$3',$date); |
166 | } |
167 | } |
168 | |
169 | /** |
170 | * @param string $date |
171 | */ |
172 | protected static function buildDateFromSixDigit(string &$date): void |
173 | { |
174 | if(substr($date,4,2)>31) { |
175 | $date = preg_replace('#([0-9]{2})([0-9]{2})([0-9]{2})#','19$3-$2-$1',$date); |
176 | } elseif(substr($date,0,2)>31) { |
177 | $date = preg_replace('#([0-9]{2})([0-9]{2})([0-9]{2})#','19$1-$2-$3',$date); |
178 | } elseif(substr($date,4,2)>date('y')) { |
179 | $date =preg_replace('#([0-9]{2})([0-9]{2})([0-9]{2})#','19$3-$2-$1',$date); |
180 | } elseif(substr($date,0,2)>date('y')) { |
181 | $date = preg_replace('#([0-9]{2})([0-9]{2})([0-9]{2})#','19$1-$2-$3',$date); |
182 | } else { |
183 | $date = preg_replace('#([0-9]{2})([0-9]{2})([0-9]{2})#','20$3-$2-$1',$date); |
184 | } |
185 | } |
186 | |
187 | /** |
188 | * @param DateTime $date |
189 | * @param string $format |
190 | * @return UTCDateTime|string |
191 | * @throws Exception |
192 | */ |
193 | protected static function convertDate(DateTime $date, string $format) |
194 | { |
195 | switch ($format) { |
196 | case 'mongo': |
197 | return new UTCDateTime($date); |
198 | default: |
199 | return $date->format($format); |
200 | } |
201 | } |
202 | } |