|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
/** |
|
4
|
|
|
* /src/Service/Localization.php |
|
5
|
|
|
* |
|
6
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace App\Service; |
|
10
|
|
|
|
|
11
|
|
|
use App\Enum\Language; |
|
12
|
|
|
use App\Enum\Locale; |
|
13
|
|
|
use Closure; |
|
14
|
|
|
use DateTimeImmutable; |
|
15
|
|
|
use DateTimeZone; |
|
16
|
|
|
use Psr\Log\LoggerInterface; |
|
17
|
|
|
use Symfony\Contracts\Cache\CacheInterface; |
|
18
|
|
|
use Symfony\Contracts\Cache\ItemInterface; |
|
19
|
|
|
use Throwable; |
|
20
|
|
|
use function explode; |
|
21
|
|
|
use function floor; |
|
22
|
|
|
use function str_replace; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class Localization |
|
26
|
|
|
* |
|
27
|
|
|
* @package App\Service |
|
28
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class Localization |
|
31
|
|
|
{ |
|
32
|
|
|
final public const DEFAULT_TIMEZONE = 'Europe/Helsinki'; |
|
33
|
|
|
|
|
34
|
51 |
|
public function __construct( |
|
35
|
|
|
private readonly CacheInterface $appCacheApcu, |
|
36
|
|
|
private readonly LoggerInterface $logger, |
|
37
|
|
|
) { |
|
38
|
51 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return array<int, string> |
|
42
|
|
|
*/ |
|
43
|
2 |
|
public function getLanguages(): array |
|
44
|
|
|
{ |
|
45
|
2 |
|
return Language::getValues(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return array<int, string> |
|
50
|
|
|
*/ |
|
51
|
2 |
|
public function getLocales(): array |
|
52
|
|
|
{ |
|
53
|
2 |
|
return Locale::getValues(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return array<int, array{timezone: string, identifier: string, offset: string, value: string}> |
|
58
|
|
|
*/ |
|
59
|
47 |
|
public function getTimezones(): array |
|
60
|
|
|
{ |
|
61
|
47 |
|
$output = []; |
|
62
|
|
|
|
|
63
|
|
|
try { |
|
64
|
|
|
/** @var array<int, array{timezone: string, identifier: string, offset: string, value: string}> $output */ |
|
65
|
47 |
|
$output = $this->appCacheApcu->get('application_timezone', $this->getClosure()); |
|
66
|
1 |
|
} catch (Throwable $exception) { |
|
67
|
1 |
|
$this->logger->error($exception->getMessage(), $exception->getTrace()); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
47 |
|
return $output; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @throws Throwable |
|
75
|
|
|
* |
|
76
|
|
|
* @return array<int, array{timezone: string, identifier: string, offset: string, value: string}> |
|
77
|
|
|
*/ |
|
78
|
46 |
|
public function getFormattedTimezones(): array |
|
79
|
|
|
{ |
|
80
|
46 |
|
$output = []; |
|
81
|
|
|
|
|
82
|
46 |
|
$identifiers = DateTimeZone::listIdentifiers(); |
|
83
|
|
|
|
|
84
|
46 |
|
foreach ($identifiers as $identifier) { |
|
85
|
46 |
|
$dateTimeZone = new DateTimeZone($identifier); |
|
86
|
|
|
|
|
87
|
46 |
|
$dateTime = new DateTimeImmutable(timezone: $dateTimeZone); |
|
88
|
|
|
|
|
89
|
46 |
|
$hours = floor($dateTimeZone->getOffset($dateTime) / 3600); |
|
90
|
46 |
|
$minutes = floor(($dateTimeZone->getOffset($dateTime) - ($hours * 3600)) / 60); |
|
91
|
|
|
|
|
92
|
46 |
|
$hours = 'GMT' . ($hours < 0 ? $hours : '+' . $hours); |
|
93
|
46 |
|
$minutes = ($minutes > 0 ? $minutes : '0' . $minutes); |
|
94
|
|
|
|
|
95
|
46 |
|
$output[] = [ |
|
96
|
46 |
|
'timezone' => explode('/', $identifier)[0], |
|
97
|
46 |
|
'identifier' => $identifier, |
|
98
|
46 |
|
'offset' => $hours . ':' . $minutes, |
|
99
|
46 |
|
'value' => str_replace('_', ' ', $identifier), |
|
100
|
46 |
|
]; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
46 |
|
return $output; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
47 |
|
private function getClosure(): Closure |
|
107
|
|
|
{ |
|
108
|
47 |
|
return function (ItemInterface $item): array { |
|
109
|
|
|
// One year |
|
110
|
46 |
|
$item->expiresAfter(31_536_000); |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
46 |
|
return $this->getFormattedTimezones(); |
|
113
|
47 |
|
}; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|