1
1
<?php
2
2
/**
3
- * Zend Framework (http://framework.zend.com/)
4
- *
5
- * @link http://github.com/zendframework/zf2 for the canonical source repository
6
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
3
+ * @link http://github.com/zendframework/zend-view for the canonical source repository
4
+ * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
7
5
* @license http://framework.zend.com/license/new-bsd New BSD License
8
6
*/
9
7
10
8
namespace Zend \View \Helper ;
11
9
12
- use Zend \I18n \Translator \TranslatorInterface as Translator ;
13
- use Zend \I18n \Translator \TranslatorAwareInterface ;
14
10
use Zend \View \Exception ;
15
11
16
12
/**
17
- * Helper for setting and retrieving title element for HTML head
13
+ * Helper for setting and retrieving title element for HTML head.
14
+ *
15
+ * Duck-types against Zend\I18n\Translator\TranslatorAwareInterface.
18
16
*/
19
- class HeadTitle extends Placeholder \Container \AbstractStandalone implements
20
- TranslatorAwareInterface
17
+ class HeadTitle extends Placeholder \Container \AbstractStandalone
21
18
{
19
+ use TranslatorAwareTrait;
20
+
22
21
/**
23
22
* Registry key for placeholder
24
23
*
@@ -33,27 +32,6 @@ class HeadTitle extends Placeholder\Container\AbstractStandalone implements
33
32
*/
34
33
protected $ defaultAttachOrder = null ;
35
34
36
- /**
37
- * Translator (optional)
38
- *
39
- * @var Translator
40
- */
41
- protected $ translator ;
42
-
43
- /**
44
- * Translator text domain (optional)
45
- *
46
- * @var string
47
- */
48
- protected $ translatorTextDomain = 'default ' ;
49
-
50
- /**
51
- * Whether translator should be used
52
- *
53
- * @var bool
54
- */
55
- protected $ translatorEnabled = true ;
56
-
57
35
/**
58
36
* Retrieve placeholder for title element and optionally set state
59
37
*
@@ -109,14 +87,9 @@ public function renderTitle()
109
87
{
110
88
$ items = [];
111
89
112
- if (null !== ($ translator = $ this ->getTranslator ())) {
113
- foreach ($ this as $ item ) {
114
- $ items [] = $ translator ->translate ($ item , $ this ->getTranslatorTextDomain ());
115
- }
116
- } else {
117
- foreach ($ this as $ item ) {
118
- $ items [] = $ item ;
119
- }
90
+ $ itemCallback = $ this ->getTitleItemCallback ();
91
+ foreach ($ this as $ item ) {
92
+ $ items [] = $ itemCallback ($ item );
120
93
}
121
94
122
95
$ separator = $ this ->getSeparator ();
@@ -172,92 +145,28 @@ public function getDefaultAttachOrder()
172
145
return $ this ->defaultAttachOrder ;
173
146
}
174
147
175
- // Translator methods - Good candidate to refactor as a trait with PHP 5.4
176
148
177
149
/**
178
- * Sets translator to use in helper
150
+ * Create and return a callback for normalizing title items.
179
151
*
180
- * @param Translator $translator [optional] translator.
181
- * Default is null, which sets no translator.
182
- * @param string $textDomain [optional] text domain
183
- * Default is null, which skips setTranslatorTextDomain
184
- * @return HeadTitle
185
- */
186
- public function setTranslator (Translator $ translator = null , $ textDomain = null )
187
- {
188
- $ this ->translator = $ translator ;
189
- if (null !== $ textDomain ) {
190
- $ this ->setTranslatorTextDomain ($ textDomain );
191
- }
192
- return $ this ;
193
- }
194
-
195
- /**
196
- * Returns translator used in helper
152
+ * If translation is not enabled, or no translator is present, returns a
153
+ * callable that simply returns the provided item; otherwise, returns a
154
+ * callable that returns a translation of the provided item.
197
155
*
198
- * @return Translator|null
156
+ * @return callable
199
157
*/
200
- public function getTranslator ()
158
+ private function getTitleItemCallback ()
201
159
{
202
- if (! $ this ->isTranslatorEnabled ()) {
203
- return ;
160
+ if (! $ this ->isTranslatorEnabled () || ! $ this ->hasTranslator ()) {
161
+ return function ($ item ) {
162
+ return $ item ;
163
+ };
204
164
}
205
165
206
- return $ this ->translator ;
207
- }
208
-
209
- /**
210
- * Checks if the helper has a translator
211
- *
212
- * @return bool
213
- */
214
- public function hasTranslator ()
215
- {
216
- return (bool ) $ this ->getTranslator ();
217
- }
218
-
219
- /**
220
- * Sets whether translator is enabled and should be used
221
- *
222
- * @param bool $enabled [optional] whether translator should be used.
223
- * Default is true.
224
- * @return HeadTitle
225
- */
226
- public function setTranslatorEnabled ($ enabled = true )
227
- {
228
- $ this ->translatorEnabled = (bool ) $ enabled ;
229
- return $ this ;
230
- }
231
-
232
- /**
233
- * Returns whether translator is enabled and should be used
234
- *
235
- * @return bool
236
- */
237
- public function isTranslatorEnabled ()
238
- {
239
- return $ this ->translatorEnabled ;
240
- }
241
-
242
- /**
243
- * Set translation text domain
244
- *
245
- * @param string $textDomain
246
- * @return HeadTitle
247
- */
248
- public function setTranslatorTextDomain ($ textDomain = 'default ' )
249
- {
250
- $ this ->translatorTextDomain = $ textDomain ;
251
- return $ this ;
252
- }
253
-
254
- /**
255
- * Return the translation text domain
256
- *
257
- * @return string
258
- */
259
- public function getTranslatorTextDomain ()
260
- {
261
- return $ this ->translatorTextDomain ;
166
+ $ translator = $ this ->getTranslator ();
167
+ $ textDomain = $ this ->getTranslatorTextDomain ();
168
+ return function ($ item ) use ($ translator , $ textDomain ) {
169
+ return $ translator ->translate ($ item , $ textDomain );
170
+ };
262
171
}
263
172
}
0 commit comments