PageRenderTime 25ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/module/SlmLocale/locale/src/SlmLocale/View/Helper/LocaleMenu.php

https://gitlab.com/my-application.bjoernbartels.earth/my-application
PHP | 320 lines | 146 code | 40 blank | 134 comment | 6 complexity | 0da4bc30c4ee859990e4a8f20860ce87 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright (c) 2012-2013 Jurian Sluiman.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * * Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. *
  18. * * Neither the names of the copyright holders nor the names of the
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. * @author Jurian Sluiman <jurian@juriansluiman.nl>
  36. * @copyright 2012-2013 Jurian Sluiman.
  37. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  38. * @link http://juriansluiman.nl
  39. */
  40. namespace SlmLocale\View\Helper;
  41. use Locale;
  42. use SlmLocale\Locale\Detector;
  43. use Zend\View\Helper\AbstractHelper;
  44. use Zend\View\Exception\RuntimeException;
  45. class LocaleMenu extends AbstractHelper
  46. {
  47. /**
  48. * @var Detector $detector
  49. */
  50. protected $detector;
  51. /**
  52. * Set the class to be used on the list container
  53. *
  54. * @var string || null
  55. */
  56. protected $class;
  57. /**
  58. * Method used to construct a title for each item
  59. *
  60. * @var string || null
  61. */
  62. protected $titleMethod = 'displayLanguage';
  63. /**
  64. * Flag to specify specifies whether the title should be in the current locale
  65. *
  66. * @var boolean default false
  67. */
  68. protected $titleInCurrentLocale = false;
  69. /**
  70. * Method used to construct a label for each item
  71. *
  72. * @var string || null
  73. */
  74. protected $labelMethod = 'displayLanguage';
  75. /**
  76. * Flag to specify specifies whether the label should be in the current locale
  77. *
  78. * @var boolean default true
  79. */
  80. protected $labelInCurrentLocale = true;
  81. /**
  82. * Flag to specify the current locale should be omitted from the menu
  83. *
  84. * @var boolean default false
  85. */
  86. protected $omitCurrent = false;
  87. /**
  88. * @param Detector $detector
  89. */
  90. public function setDetector($detector)
  91. {
  92. $this->detector = $detector;
  93. }
  94. /**
  95. * @return Detector $detector
  96. */
  97. public function getDetector()
  98. {
  99. return $this->detector;
  100. }
  101. /**
  102. * @param string $class
  103. */
  104. public function setUlClass($class)
  105. {
  106. $this->class = $class;
  107. return $this;
  108. }
  109. /**
  110. * @return string
  111. */
  112. public function getUlClass()
  113. {
  114. return $this->class;
  115. }
  116. /**
  117. * @param string $itemTitleMethod
  118. */
  119. public function setTitleMethod($titleMethod)
  120. {
  121. $this->checkLocaleMethod($titleMethod);
  122. $this->titleMethod = $titleMethod;
  123. return $this;
  124. }
  125. /**
  126. * @return string
  127. */
  128. public function getTitleMethod()
  129. {
  130. return $this->titleMethod;
  131. }
  132. /**
  133. * @param boolean $flag
  134. */
  135. public function setTitleInCurrentLocale($flag)
  136. {
  137. $this->titleInCurrentLocale = (bool) $flag;
  138. return $this;
  139. }
  140. /**
  141. * @return boolean
  142. */
  143. public function getTitleInCurrentLocale()
  144. {
  145. return $this->titleInCurrentLocale;
  146. }
  147. /**
  148. * @param string $labelMethod
  149. */
  150. public function setLabelMethod($labelMethod)
  151. {
  152. $this->checkLocaleMethod($labelMethod);
  153. $this->labelMethod = $labelMethod;
  154. return $this;
  155. }
  156. /**
  157. * @return string
  158. */
  159. public function getLabelMethod()
  160. {
  161. return $this->labelMethod;
  162. }
  163. /**
  164. * @param boolean $flag
  165. */
  166. public function setLabelInCurrentLocale($flag)
  167. {
  168. $this->labelInCurrentLocale = (bool) $flag;
  169. return $this;
  170. }
  171. /**
  172. * @return boolean
  173. */
  174. public function getLabelInCurrentLocale()
  175. {
  176. return $this->labelInCurrentLocale;
  177. }
  178. /**
  179. * @param boolean $omitCurrent
  180. */
  181. public function setOmitCurrent($omitCurrent)
  182. {
  183. $this->omitCurrent = (bool) $omitCurrent;
  184. return $this;
  185. }
  186. /**
  187. * @return boolean
  188. */
  189. public function omitCurrent()
  190. {
  191. return $this->omitCurrent;
  192. }
  193. public function __invoke()
  194. {
  195. return $this;
  196. }
  197. /**
  198. * @param array $options
  199. * @return string
  200. * @throws RuntimeException
  201. * @todo implement add way to completely default rendering for maximum flexibility (see Zend\View\Helper\Navigation::renderPartial)
  202. */
  203. public function __toString()
  204. {
  205. if (!($detector = $this->getDetector())) {
  206. throw new RuntimeException('To assemble an url, a detector is required');
  207. }
  208. $list = '';
  209. $current = Locale::getDefault();
  210. foreach($detector->getSupported() as $locale) {
  211. if ($this->omitCurrent() && $current === $locale) {
  212. continue;
  213. }
  214. $titleLocale = $this->getTitleInCurrentLocale() ? $locale : $current;
  215. $labelLocale = $this->getLabelInCurrentLocale() ? $locale : $current;
  216. $url = $this->getView()->localeUrl($locale);
  217. $title = $this->getLocaleProperty($this->getTitleMethod(), $locale, $titleLocale);
  218. $label = $this->getLocaleProperty($this->getLabelMethod(), $locale, $labelLocale);
  219. $item = sprintf(
  220. '<li><a href="%s" title="%s"%s>%s</a></li>' . "\n",
  221. $url,
  222. $title,
  223. ($current === $locale) ? ' class="active"' : '',
  224. $label
  225. );
  226. $list .= $item;
  227. }
  228. $class = $this->getUlClass();
  229. $html = sprintf(
  230. '<ul%s>%s</ul>',
  231. ($class) ? sprintf(' class="%s"', $class) : '',
  232. $list
  233. );
  234. return $html;
  235. }
  236. /**
  237. * Check whether method part of the Locale class is
  238. *
  239. * @param string $method Method to check
  240. * @throws RuntimeException If method is not part of locale
  241. * @return true
  242. */
  243. protected function checkLocaleMethod($method)
  244. {
  245. $options = array(
  246. 'displayLanguage',
  247. 'displayName',
  248. 'displayRegion',
  249. 'displayScript',
  250. 'displayVariant',
  251. 'primaryLanguage',
  252. 'region',
  253. 'script'
  254. );
  255. if (!in_array($method, $options)) {
  256. throw new RuntimeException(sprintf(
  257. 'Unknown method "%s" for Locale, expecting one of these: %s.',
  258. $method,
  259. implode(', ', $options)
  260. ));
  261. }
  262. }
  263. /**
  264. * Retrieves a value by property from Locale
  265. *
  266. * @param $property
  267. * @param $locale
  268. * @param bool $in_locale
  269. * @return mixed
  270. */
  271. protected function getLocaleProperty($property, $locale, $in_locale = false)
  272. {
  273. $callback = sprintf('\Locale::get%s', ucfirst($property));
  274. $args = array($locale);
  275. if ($in_locale && !in_array($property, array('primaryLanguage', 'region', 'script'))) {
  276. $args[] = $in_locale;
  277. }
  278. return call_user_func_array($callback, $args);
  279. }
  280. }