/
AbstractRedirector.php
89 lines (73 loc) · 2.51 KB
/
AbstractRedirector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace I18nBundle\Adapter\Redirector;
use Symfony\Component\OptionsResolver\OptionsResolver;
abstract class AbstractRedirector implements RedirectorInterface
{
protected array $config = [];
protected ?string $name;
protected array $decision = [];
public function getConfig(): array
{
return $this->config;
}
public function setConfig(array $config): void
{
$configResolver = $this->getConfigResolver();
if (null === $configResolver) {
if (!empty($config)) {
throw new \Exception(sprintf('redirector adapter "%s" has a config, but no config resolver was provided.', $this->getName()));
}
} else {
$this->config = $configResolver->resolve($config);
}
}
public function getName(): string
{
return $this->name;
}
public function setName($name): void
{
$this->name = $name;
}
public function setDecision(array $decision): void
{
$this->decision = $this->getDecisionResolver()->resolve($decision);
}
public function getDecision(): array
{
return $this->getDecisionResolver()->resolve($this->decision);
}
public function lastRedirectorWasSuccessful(RedirectorBag $redirectorBag): bool
{
$lastDecisionBag = $redirectorBag->getLastValidRedirectorDecision();
if (!is_null($lastDecisionBag) && $lastDecisionBag['decision']['valid'] === true) {
$this->setDecision(['valid' => false]);
return true;
}
return false;
}
protected function getConfigResolver(): ?OptionsResolver
{
return null;
}
private function getDecisionResolver(): OptionsResolver
{
$resolver = new OptionsResolver();
$resolver->setDefaults([
'redirectorOptions' => [],
'valid' => null,
'locale' => null,
'country' => null,
'language' => null,
'url' => null,
]);
$resolver->setRequired(['locale', 'url']);
$resolver->setAllowedTypes('valid', 'boolean');
$resolver->setAllowedTypes('locale', ['null', 'string']);
$resolver->setAllowedTypes('url', ['null', 'string']);
$resolver->setAllowedTypes('language', ['null', 'string']);
$resolver->setAllowedTypes('country', ['null', 'string']);
$resolver->setAllowedTypes('redirectorOptions', ['array']);
return $resolver;
}
}