1 | <?php |
||
21 | class TranslatorHelpers implements HelpersInterface |
||
22 | { |
||
23 | /** |
||
24 | * Store the translator service. |
||
25 | * |
||
26 | * @var Translator|null |
||
27 | */ |
||
28 | private $translator; |
||
29 | |||
30 | /** |
||
31 | * Store the given number to use to find the indice of the message (Mustache tag node). |
||
32 | * |
||
33 | * This can be a variable name in the context stack or an integer. |
||
34 | * |
||
35 | * Floats are not supported due to their decimal symbol conflicting |
||
36 | * with Mustache's dot-notation. |
||
37 | * |
||
38 | * @var string|integer|null |
||
39 | */ |
||
40 | private $number; |
||
41 | |||
42 | /** |
||
43 | * Store the given locale (Mustache tag node). |
||
44 | * |
||
45 | * This must be an available locale on the Translator. |
||
46 | * |
||
47 | * @var string|null |
||
48 | */ |
||
49 | private $locale; |
||
50 | |||
51 | /** |
||
52 | * Store the given domain for the message (Mustache tag node). |
||
53 | * |
||
54 | * This must be an available domain on the Translator. |
||
55 | * |
||
56 | * @var string|null |
||
57 | */ |
||
58 | private $domain; |
||
59 | |||
60 | /** |
||
61 | * @param array $data Class Dependencies. |
||
62 | */ |
||
63 | public function __construct(array $data = null) |
||
69 | |||
70 | /** |
||
71 | * Set the translator service. |
||
72 | * |
||
73 | * @param Translator $translator The Translator service. |
||
74 | * @return void |
||
75 | */ |
||
76 | protected function setTranslator(Translator $translator): void |
||
80 | |||
81 | /** |
||
82 | * Retrieve the helpers. |
||
83 | * |
||
84 | * @return array |
||
85 | */ |
||
86 | public function toArray(): array |
||
92 | |||
93 | /** |
||
94 | * Clear macros. |
||
95 | * |
||
96 | * @return void |
||
97 | */ |
||
98 | protected function reset(): void |
||
104 | |||
105 | /** |
||
106 | * Magic: Render the Mustache section. |
||
107 | * |
||
108 | * @param string $text The translation key. |
||
109 | * @param LambdaHelper|null $helper For rendering strings in the current context. |
||
110 | * @return string |
||
111 | */ |
||
112 | public function __invoke(string $text, LambdaHelper $helper = null): string |
||
131 | |||
132 | /** |
||
133 | * Magic: Determine if a property is set and is not NULL. |
||
134 | * |
||
135 | * Required by Mustache. |
||
136 | * |
||
137 | * @param string $macro A domain, locale, or number. |
||
138 | * @return boolean |
||
139 | */ |
||
140 | public function __isset(string $macro): bool |
||
144 | |||
145 | /** |
||
146 | * Magic: Process domain, locale, and number. |
||
147 | * |
||
148 | * Required by Mustache. |
||
149 | * |
||
150 | * @param string $macro A domain, locale, or number. |
||
151 | * @throws LogicException If the macro is unresolved. |
||
152 | * @return mixed |
||
153 | */ |
||
154 | public function __get(string $macro) |
||
177 | } |
||
178 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: