1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015 SURFnet bv |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
|
|
|
|
20
|
|
|
|
21
|
|
|
namespace Surfnet\StepupSelfService\SelfServiceBundle\Locale; |
22
|
|
|
|
23
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Exception\InvalidArgumentException; |
24
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Locale\PreferredLocaleProvider; |
25
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Fetches the client's preferred locale from the request (specifically from the Accept header), falling back to a |
29
|
|
|
* default locale if no preferred locale can be determined. |
30
|
|
|
*/ |
|
|
|
|
31
|
|
|
final class RequestStackLocaleProvider implements PreferredLocaleProvider |
32
|
|
|
{ |
33
|
|
|
private readonly string $defaultLocale; |
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
|
|
|
|
36
|
|
|
* @var string[] |
37
|
|
|
*/ |
38
|
|
|
private $supportedLocales; |
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
|
|
|
|
41
|
|
|
* @param string $defaultLocale |
|
|
|
|
42
|
|
|
* @param string[] $supportedLocales |
|
|
|
|
43
|
|
|
*/ |
44
|
|
|
public function __construct( |
45
|
|
|
private readonly RequestStack $requestStack, |
46
|
|
|
$defaultLocale, |
47
|
|
|
$supportedLocales |
48
|
|
|
) { |
49
|
|
|
if (!is_string($defaultLocale)) { |
|
|
|
|
50
|
|
|
throw InvalidArgumentException::invalidType('string', 'defaultLocale', $defaultLocale); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
foreach ($supportedLocales as $key => $supportedLocale) { |
54
|
|
|
if (!is_string($supportedLocale)) { |
55
|
|
|
$parameterName = sprintf('supportedLocales[%s]', $key); |
56
|
|
|
throw InvalidArgumentException::invalidType('string', $parameterName, $supportedLocale); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
$this->defaultLocale = $defaultLocale; |
|
|
|
|
60
|
|
|
$this->supportedLocales = $supportedLocales; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function providePreferredLocale(): string |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$preferredLocale = $this->requestStack->getCurrentRequest()->getPreferredLanguage($this->supportedLocales); |
66
|
|
|
|
67
|
|
|
if (!$preferredLocale) { |
68
|
|
|
return $this->defaultLocale; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $preferredLocale; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|