Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
66.67% |
10 / 15 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
DateTimeFormatHelper | |
66.67% |
10 / 15 |
|
33.33% |
1 / 3 |
7.33 | |
0.00% |
0 / 1 |
formatDateArray | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
getInternalDateFromISO | |
50.00% |
3 / 6 |
|
0.00% |
0 / 1 |
4.12 | |||
getInternalDateFromTimestamp | |
50.00% |
2 / 4 |
|
0.00% |
0 / 1 |
2.50 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace BO\Zmscitizenapi\Helper; |
6 | |
7 | class DateTimeFormatHelper |
8 | { |
9 | private static function formatDateArray(\DateTime $date): array |
10 | { |
11 | return [ |
12 | 'day' => (int) $date->format('d'), |
13 | 'month' => (int) $date->format('m'), |
14 | 'year' => (int) $date->format('Y'), |
15 | ]; |
16 | } |
17 | |
18 | public static function getInternalDateFromISO(string $dateString): array |
19 | { |
20 | try { |
21 | if (!is_string($dateString)) { |
22 | throw new \InvalidArgumentException('Date string must be a string'); |
23 | } |
24 | $date = new \DateTime($dateString); |
25 | return self::formatDateArray($date); |
26 | } catch (\Exception $e) { |
27 | throw new \InvalidArgumentException('Invalid ISO date format: ' . $e->getMessage()); |
28 | } |
29 | } |
30 | |
31 | public static function getInternalDateFromTimestamp(int $timestamp): array |
32 | { |
33 | try { |
34 | $date = (new \DateTime())->setTimestamp($timestamp); |
35 | return self::formatDateArray($date); |
36 | } catch (\Exception $e) { |
37 | throw new \InvalidArgumentException('Invalid timestamp: ' . $e->getMessage()); |
38 | } |
39 | } |
40 | } |