LocaleProvider   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getLocale() 0 10 3
A resolve() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Nette extension for bckp/translator
7
 * (c) Radovan Kepák
8
 *
9
 * For the full copyright and license information, please view the file license.md that was distributed with this source code.
10
 *
11
 * @author Radovan Kepak <[email protected]>
12
 *  --------------------------------------------------------------------------
13
 */
14
15
namespace Bckp\Translator\Nette;
16
17
use Bckp\Translator\Nette\Resolvers\LocaleResolver;
18
19
final class LocaleProvider
20
{
21
	/**
22
	 * @var LocaleResolver[]
23
	 */
24
	private readonly array $resolvers;
25
26
	private string $locale;
27
28
	/**
29
	 * @param string[] $allowed
30
	 * @api
31
	 */
32
	public function __construct(
33
		public readonly array $allowed,
34
		LocaleResolver ...$resolvers
35
	) {
36
		$this->resolvers = $resolvers;
37
	}
38
39
	/**
40
	 * @api
41
	 */
42
	public function resolve(): string
43
	{
44
		/** @psalm-suppress RedundantPropertyInitializationCheck */
45
		return $this->locale ??= $this->getLocale();
46
	}
47
48
	private function getLocale(): string
49
	{
50
		foreach ($this->resolvers as $resolver) {
51
			$locale = $resolver->resolve($this->allowed);
52
			if ($locale !== null) {
53
				return $locale;
54
			}
55
		}
56
57
		return $this->allowed[0];
58
	}
59
}
60