|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Locale; |
|
4
|
|
|
|
|
5
|
|
|
use Thinktomorrow\Locale\Values\Config; |
|
6
|
|
|
use Thinktomorrow\Locale\Values\Root; |
|
7
|
|
|
|
|
8
|
|
|
final class ScopeCollection |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var Config |
|
12
|
|
|
*/ |
|
13
|
|
|
private $config; |
|
14
|
|
|
|
|
15
|
76 |
|
private function __construct(Config $config) |
|
16
|
|
|
{ |
|
17
|
76 |
|
$this->config = $config; |
|
18
|
76 |
|
} |
|
19
|
|
|
|
|
20
|
17 |
|
public static function fromArray(array $config) |
|
21
|
|
|
{ |
|
22
|
17 |
|
return new static(Config::from($config)); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
65 |
|
public static function fromConfig(Config $config) |
|
26
|
|
|
{ |
|
27
|
65 |
|
return new static($config); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
74 |
|
public function findByRoot(string $root): Scope |
|
31
|
|
|
{ |
|
32
|
74 |
|
return $this->findByKey($this->guessKeyFromRoot($root)); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $locale |
|
37
|
|
|
* |
|
38
|
|
|
* @return null|Scope |
|
39
|
|
|
*/ |
|
40
|
11 |
|
public function findCanonical(string $locale): ?Scope |
|
41
|
|
|
{ |
|
42
|
11 |
|
$canonicals = $this->config->get('canonicals'); |
|
43
|
|
|
|
|
44
|
11 |
|
if (!isset($canonicals[$locale])) { |
|
45
|
2 |
|
return null; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
10 |
|
$scope = $this->findByRoot($canonicals[$locale]); |
|
49
|
|
|
|
|
50
|
10 |
|
return $scope->setCustomRoot(Root::fromString($canonicals[$locale])); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
74 |
|
private function guessKeyFromRoot(string $value): string |
|
54
|
|
|
{ |
|
55
|
74 |
|
foreach ($this->config->get('locales') as $scopeKey => $locales) { |
|
56
|
74 |
|
$pattern = preg_quote($scopeKey, '#'); |
|
57
|
|
|
|
|
58
|
|
|
/* |
|
59
|
|
|
* The host pattern allows for an asterix which stands for a |
|
60
|
|
|
* wildcard of characters when matching the scope keys. |
|
61
|
|
|
* The default '*' scope will match anything |
|
62
|
|
|
*/ |
|
63
|
74 |
|
if (false !== strpos($pattern, '*')) { |
|
64
|
30 |
|
$pattern = str_replace('\*', '(.+)', $pattern); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
74 |
|
if (preg_match("#^(https?://)?(www\.)?$pattern/?$#", $value)) { |
|
68
|
74 |
|
return $scopeKey; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// In case value remains empty, we return the default routing. |
|
73
|
1 |
|
return '*'; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// We limit the available locales to the current domain space, including the default ones |
|
77
|
74 |
|
private function findByKey(string $scopeKey): Scope |
|
78
|
|
|
{ |
|
79
|
74 |
|
$locales = $this->config->get('locales'); |
|
80
|
|
|
|
|
81
|
74 |
|
$locales = array_merge($locales['*'], $locales[$scopeKey]); |
|
82
|
|
|
|
|
83
|
|
|
// We flip our values so in case of duplicate locales, the default one |
|
84
|
|
|
// is omitted and the one from the specific scope is preserved. |
|
85
|
74 |
|
return new Scope(array_flip(array_flip($locales))); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|