11<?php
22/**
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)
75 * @license http://framework.zend.com/license/new-bsd New BSD License
86 */
97
108namespace Zend \View \Helper ;
119
12- use Zend \I18n \Translator \TranslatorInterface as Translator ;
13- use Zend \I18n \Translator \TranslatorAwareInterface ;
1410use Zend \View \Exception ;
1511
1612/**
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.
1816 */
19- class HeadTitle extends Placeholder \Container \AbstractStandalone implements
20- TranslatorAwareInterface
17+ class HeadTitle extends Placeholder \Container \AbstractStandalone
2118{
19+ use TranslatorAwareTrait;
20+
2221 /**
2322 * Registry key for placeholder
2423 *
@@ -33,27 +32,6 @@ class HeadTitle extends Placeholder\Container\AbstractStandalone implements
3332 */
3433 protected $ defaultAttachOrder = null ;
3534
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-
5735 /**
5836 * Retrieve placeholder for title element and optionally set state
5937 *
@@ -109,14 +87,9 @@ public function renderTitle()
10987 {
11088 $ items = [];
11189
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 );
12093 }
12194
12295 $ separator = $ this ->getSeparator ();
@@ -172,92 +145,28 @@ public function getDefaultAttachOrder()
172145 return $ this ->defaultAttachOrder ;
173146 }
174147
175- // Translator methods - Good candidate to refactor as a trait with PHP 5.4
176148
177149 /**
178- * Sets translator to use in helper
150+ * Create and return a callback for normalizing title items.
179151 *
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.
197155 *
198- * @return Translator|null
156+ * @return callable
199157 */
200- public function getTranslator ()
158+ private function getTitleItemCallback ()
201159 {
202- if (! $ this ->isTranslatorEnabled ()) {
203- return ;
160+ if (! $ this ->isTranslatorEnabled () || ! $ this ->hasTranslator ()) {
161+ return function ($ item ) {
162+ return $ item ;
163+ };
204164 }
205165
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+ };
262171 }
263172}
0 commit comments