Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
21 / 21 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| Format | |
100.00% |
21 / 21 |
|
100.00% |
5 / 5 |
14 | |
100.00% |
1 / 1 |
| number | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| currency | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| dateTime | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
8 | |||
| relative | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| isDA | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Helpers; |
| 4 | |
| 5 | use Carbon\Carbon; |
| 6 | |
| 7 | class Format |
| 8 | { |
| 9 | public static function number(int|float $value, int $decimals = 0): string |
| 10 | { |
| 11 | if (app()->getLocale() === 'da') { |
| 12 | return number_format($value, $decimals, ',', '.'); |
| 13 | } |
| 14 | |
| 15 | return number_format($value, $decimals, '.', ','); |
| 16 | } |
| 17 | |
| 18 | public static function currency(float $value, int $decimals = 2): string |
| 19 | { |
| 20 | return '$' . self::number($value, $decimals); |
| 21 | } |
| 22 | |
| 23 | public static function dateTime(?Carbon $dt, string $format = 'short'): string |
| 24 | { |
| 25 | if (! $dt) { |
| 26 | return '-'; |
| 27 | } |
| 28 | |
| 29 | return match ($format) { |
| 30 | 'time' => $dt->format('H:i:s'), |
| 31 | 'short' => self::isDA() |
| 32 | ? $dt->format('d/m/Y H:i:s') |
| 33 | : $dt->format('Y-m-d H:i:s'), |
| 34 | 'date_time_short' => self::isDA() |
| 35 | ? $dt->format('d/m H:i') |
| 36 | : $dt->format('m/d H:i'), |
| 37 | default => $dt->format($format), |
| 38 | }; |
| 39 | } |
| 40 | |
| 41 | public static function relative(?Carbon $dt): string |
| 42 | { |
| 43 | if (! $dt) { |
| 44 | return '-'; |
| 45 | } |
| 46 | |
| 47 | Carbon::setLocale(app()->getLocale()); |
| 48 | |
| 49 | return $dt->diffForHumans(); |
| 50 | } |
| 51 | |
| 52 | private static function isDA(): bool |
| 53 | { |
| 54 | return app()->getLocale() === 'da'; |
| 55 | } |
| 56 | } |