1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Configuration\Behavior; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; |
6
|
|
|
use Symfony\Component\OptionsResolver\Options; |
7
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
8
|
|
|
|
9
|
|
|
trait TranslationConfigurationTrait |
10
|
|
|
{ |
11
|
|
|
public abstract function get($name); |
12
|
|
|
|
13
|
|
|
protected function setTranslationOptions(OptionsResolver $resolver, string $pattern = 'lag.{admin}.{key}', string $catalog = 'messages') |
14
|
|
|
{ |
15
|
|
|
$resolver |
16
|
|
|
->setDefaults([ |
17
|
|
|
'translation' => function (OptionsResolver $subResolver) use ($pattern, $catalog) { |
18
|
|
|
$subResolver |
19
|
|
|
->setDefaults([ |
20
|
|
|
'enabled' => true, |
21
|
|
|
'pattern' => $pattern, |
22
|
|
|
'catalog' => $catalog, |
23
|
|
|
]) |
24
|
|
|
; |
25
|
|
|
}, |
26
|
|
|
]) |
27
|
|
|
->setAllowedTypes('translation', 'array') |
28
|
|
|
->setNormalizer('translation', function (Options $options, $value) { |
29
|
|
|
if (!array_key_exists('enabled', $value)) { |
30
|
|
|
throw new InvalidOptionsException('Admin translation enabled parameter should be defined'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if (!is_bool($value['enabled'])) { |
34
|
|
|
throw new InvalidOptionsException('Admin translation enabled parameter should be a boolean'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if (!array_key_exists('pattern', $value)) { |
38
|
|
|
$value['pattern'] = '{admin}.{key}'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if ($value['enabled'] && false === strstr($value['pattern'], '{key}')) { |
42
|
|
|
throw new InvalidOptionsException('Admin translation pattern should contains the {key} placeholder, given "'.$value['pattern'].'"'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $value; |
46
|
|
|
}) |
47
|
|
|
; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function isTranslationEnabled(): bool |
51
|
|
|
{ |
52
|
|
|
return true === $this->get('translation')['enabled']; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getTranslationPattern(): string |
56
|
|
|
{ |
57
|
|
|
return $this->get('translation')['pattern']; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getTranslationCatalog(): string |
61
|
|
|
{ |
62
|
|
|
return $this->get('translation')['catalog']; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
|