PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zend-view/src/Helper/HeadTitle.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 263 lines | 122 code | 33 blank | 108 comment | 13 complexity | f1b8402b2daf5c1536b91671964a2008 MD5 | raw file
  1. <?php
  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)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\View\Helper;
  10. use Zend\I18n\Translator\TranslatorInterface as Translator;
  11. use Zend\I18n\Translator\TranslatorAwareInterface;
  12. use Zend\View\Exception;
  13. /**
  14. * Helper for setting and retrieving title element for HTML head
  15. */
  16. class HeadTitle extends Placeholder\Container\AbstractStandalone implements
  17. TranslatorAwareInterface
  18. {
  19. /**
  20. * Registry key for placeholder
  21. *
  22. * @var string
  23. */
  24. protected $regKey = 'Zend_View_Helper_HeadTitle';
  25. /**
  26. * Default title rendering order (i.e. order in which each title attached)
  27. *
  28. * @var string
  29. */
  30. protected $defaultAttachOrder = null;
  31. /**
  32. * Translator (optional)
  33. *
  34. * @var Translator
  35. */
  36. protected $translator;
  37. /**
  38. * Translator text domain (optional)
  39. *
  40. * @var string
  41. */
  42. protected $translatorTextDomain = 'default';
  43. /**
  44. * Whether translator should be used
  45. *
  46. * @var bool
  47. */
  48. protected $translatorEnabled = true;
  49. /**
  50. * Retrieve placeholder for title element and optionally set state
  51. *
  52. * @param string $title
  53. * @param string $setType
  54. * @return HeadTitle
  55. */
  56. public function __invoke($title = null, $setType = null)
  57. {
  58. if (null === $setType) {
  59. $setType = (null === $this->getDefaultAttachOrder())
  60. ? Placeholder\Container\AbstractContainer::APPEND
  61. : $this->getDefaultAttachOrder();
  62. }
  63. $title = (string) $title;
  64. if ($title !== '') {
  65. if ($setType == Placeholder\Container\AbstractContainer::SET) {
  66. $this->set($title);
  67. } elseif ($setType == Placeholder\Container\AbstractContainer::PREPEND) {
  68. $this->prepend($title);
  69. } else {
  70. $this->append($title);
  71. }
  72. }
  73. return $this;
  74. }
  75. /**
  76. * Render title (wrapped by title tag)
  77. *
  78. * @param string|null $indent
  79. * @return string
  80. */
  81. public function toString($indent = null)
  82. {
  83. $indent = (null !== $indent)
  84. ? $this->getWhitespace($indent)
  85. : $this->getIndent();
  86. $output = $this->renderTitle();
  87. return $indent . '<title>' . $output . '</title>';
  88. }
  89. /**
  90. * Render title string
  91. *
  92. * @return string
  93. */
  94. public function renderTitle()
  95. {
  96. $items = array();
  97. if (null !== ($translator = $this->getTranslator())) {
  98. foreach ($this as $item) {
  99. $items[] = $translator->translate($item, $this->getTranslatorTextDomain());
  100. }
  101. } else {
  102. foreach ($this as $item) {
  103. $items[] = $item;
  104. }
  105. }
  106. $separator = $this->getSeparator();
  107. $output = '';
  108. $prefix = $this->getPrefix();
  109. if ($prefix) {
  110. $output .= $prefix;
  111. }
  112. $output .= implode($separator, $items);
  113. $postfix = $this->getPostfix();
  114. if ($postfix) {
  115. $output .= $postfix;
  116. }
  117. $output = ($this->autoEscape) ? $this->escape($output) : $output;
  118. return $output;
  119. }
  120. /**
  121. * Set a default order to add titles
  122. *
  123. * @param string $setType
  124. * @throws Exception\DomainException
  125. * @return HeadTitle
  126. */
  127. public function setDefaultAttachOrder($setType)
  128. {
  129. if (!in_array($setType, array(
  130. Placeholder\Container\AbstractContainer::APPEND,
  131. Placeholder\Container\AbstractContainer::SET,
  132. Placeholder\Container\AbstractContainer::PREPEND
  133. ))) {
  134. throw new Exception\DomainException(
  135. "You must use a valid attach order: 'PREPEND', 'APPEND' or 'SET'"
  136. );
  137. }
  138. $this->defaultAttachOrder = $setType;
  139. return $this;
  140. }
  141. /**
  142. * Get the default attach order, if any.
  143. *
  144. * @return mixed
  145. */
  146. public function getDefaultAttachOrder()
  147. {
  148. return $this->defaultAttachOrder;
  149. }
  150. // Translator methods - Good candidate to refactor as a trait with PHP 5.4
  151. /**
  152. * Sets translator to use in helper
  153. *
  154. * @param Translator $translator [optional] translator.
  155. * Default is null, which sets no translator.
  156. * @param string $textDomain [optional] text domain
  157. * Default is null, which skips setTranslatorTextDomain
  158. * @return HeadTitle
  159. */
  160. public function setTranslator(Translator $translator = null, $textDomain = null)
  161. {
  162. $this->translator = $translator;
  163. if (null !== $textDomain) {
  164. $this->setTranslatorTextDomain($textDomain);
  165. }
  166. return $this;
  167. }
  168. /**
  169. * Returns translator used in helper
  170. *
  171. * @return Translator|null
  172. */
  173. public function getTranslator()
  174. {
  175. if (! $this->isTranslatorEnabled()) {
  176. return;
  177. }
  178. return $this->translator;
  179. }
  180. /**
  181. * Checks if the helper has a translator
  182. *
  183. * @return bool
  184. */
  185. public function hasTranslator()
  186. {
  187. return (bool) $this->getTranslator();
  188. }
  189. /**
  190. * Sets whether translator is enabled and should be used
  191. *
  192. * @param bool $enabled [optional] whether translator should be used.
  193. * Default is true.
  194. * @return HeadTitle
  195. */
  196. public function setTranslatorEnabled($enabled = true)
  197. {
  198. $this->translatorEnabled = (bool) $enabled;
  199. return $this;
  200. }
  201. /**
  202. * Returns whether translator is enabled and should be used
  203. *
  204. * @return bool
  205. */
  206. public function isTranslatorEnabled()
  207. {
  208. return $this->translatorEnabled;
  209. }
  210. /**
  211. * Set translation text domain
  212. *
  213. * @param string $textDomain
  214. * @return HeadTitle
  215. */
  216. public function setTranslatorTextDomain($textDomain = 'default')
  217. {
  218. $this->translatorTextDomain = $textDomain;
  219. return $this;
  220. }
  221. /**
  222. * Return the translation text domain
  223. *
  224. * @return string
  225. */
  226. public function getTranslatorTextDomain()
  227. {
  228. return $this->translatorTextDomain;
  229. }
  230. }