1 | <?php |
||
10 | class LanguageResolver |
||
11 | { |
||
12 | /** |
||
13 | * Values typically provided by configuration. |
||
14 | * |
||
15 | * These will need to change when configuration (scope) changes mid flight using setters below. |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | private $configLanguages; |
||
20 | |||
21 | /** @var bool */ |
||
22 | private $defaultUseAlwaysAvailable; |
||
23 | |||
24 | /** @var bool */ |
||
25 | private $defaultShowAllTranslations; |
||
26 | |||
27 | /** |
||
28 | * Values typically provided by user context, will need to be set depending on your own custom logic using setter. |
||
29 | * |
||
30 | * E.g. Backend UI might expose a language selector for the whole backend that should be reflected on both |
||
31 | * UI strings as well as default languages to prioritize for repository objects. |
||
32 | * |
||
33 | * If set, this will have priority over configured languages. |
||
34 | * |
||
35 | * @var string|null |
||
36 | */ |
||
37 | private $contextLanguage; |
||
38 | |||
39 | public function __construct( |
||
48 | |||
49 | /** |
||
50 | * For use by event listening to config resolver scope changes (or other event changing configured languages). |
||
51 | * |
||
52 | * @param array $configLanguages |
||
53 | */ |
||
54 | public function setConfigLanguages(array $configLanguages): void |
||
58 | |||
59 | /** |
||
60 | * For use by custom events / logic setting language for all retrieved objects from repository. |
||
61 | * |
||
62 | * User language will, if set, will have prepended before configured languages. But in cases PHP API consumer |
||
63 | * specifies languages to retrieve repository objects in it will instead be appended as a fallback. |
||
64 | * |
||
65 | * If set, this will have priority over configured languages. |
||
66 | * |
||
67 | * @param string|null $contextLanguage |
||
68 | */ |
||
69 | public function setContextLanguage(?string $contextLanguage): void |
||
73 | |||
74 | /** |
||
75 | * Get prioritized languages taking into account forced-, context- and lastly configured-languages. |
||
76 | * |
||
77 | * @param array|null $forcedLanguages Optional, typically arguments provided to API, will be used first if set. |
||
78 | * |
||
79 | * @return array |
||
80 | */ |
||
81 | public function getPrioritizedLanguages(?array $forcedLanguages): array |
||
101 | |||
102 | /** |
||
103 | * For use by event listening to config resolver scope changes (or other event changing configured languages). |
||
104 | * |
||
105 | * @param bool $defaultUseAlwaysAvailable |
||
106 | */ |
||
107 | public function setDefaultUseAlwaysAvailable(bool $defaultUseAlwaysAvailable): void |
||
111 | |||
112 | /** |
||
113 | * Get currently set UseAlwaysAvailable. |
||
114 | * |
||
115 | * @param bool|null $forcedUseAlwaysAvailable Optional, if set will be used instead of configured value, |
||
116 | * typically arguments provided to API. |
||
117 | * |
||
118 | * @return bool |
||
119 | */ |
||
120 | public function getUseAlwaysAvailable(?bool $forcedUseAlwaysAvailable = null): bool |
||
128 | |||
129 | /** |
||
130 | * For use by event listening to config resolver scope changes (or other event changing configured languages). |
||
131 | * |
||
132 | * @param bool $defaultShowAllTranslations |
||
133 | */ |
||
134 | public function setShowAllTranslations(bool $defaultShowAllTranslations): void |
||
138 | |||
139 | /** |
||
140 | * Get currently set showAllTranslations. |
||
141 | * |
||
142 | * @param bool|null $forcedShowAllTranslations Optional, if set will be used instead of configured value, |
||
143 | * typically arguments provided to API. |
||
144 | * |
||
145 | * @return bool |
||
146 | */ |
||
147 | public function getShowAllTranslations(?bool $forcedShowAllTranslations = null): bool |
||
155 | } |
||
156 |