PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/zendframework/zendframework/library/Zend/Form/View/Helper/FormMonthSelect.php

https://bitbucket.org/zbahij/eprojets_app
PHP | 306 lines | 158 code | 43 blank | 105 comment | 18 complexity | 59ce4d883d8b2fb92d68ddc178a1751d 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-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Form\View\Helper;
  10. use DateTime;
  11. use IntlDateFormatter;
  12. use Locale;
  13. use Zend\Form\ElementInterface;
  14. use Zend\Form\Element\MonthSelect as MonthSelectElement;
  15. use Zend\Form\Exception;
  16. class FormMonthSelect extends AbstractHelper
  17. {
  18. /**
  19. * FormSelect helper
  20. *
  21. * @var FormSelect
  22. */
  23. protected $selectHelper;
  24. /**
  25. * Date formatter to use
  26. *
  27. * @var int
  28. */
  29. protected $dateType;
  30. /**
  31. * Pattern to use for Date rendering
  32. *
  33. * @var string
  34. */
  35. protected $pattern;
  36. /**
  37. * Locale to use
  38. *
  39. * @var string
  40. */
  41. protected $locale;
  42. /**
  43. * @throws Exception\ExtensionNotLoadedException if ext/intl is not present
  44. */
  45. public function __construct()
  46. {
  47. if (!extension_loaded('intl')) {
  48. throw new Exception\ExtensionNotLoadedException(sprintf(
  49. '%s component requires the intl PHP extension',
  50. __NAMESPACE__
  51. ));
  52. }
  53. // Delaying initialization until we know ext/intl is available
  54. $this->dateType = IntlDateFormatter::LONG;
  55. }
  56. /**
  57. * Invoke helper as function
  58. *
  59. * Proxies to {@link render()}.
  60. *
  61. * @param ElementInterface $element
  62. * @param int $dateType
  63. * @param null|string $locale
  64. * @return FormDateSelect
  65. */
  66. public function __invoke(ElementInterface $element = null, $dateType = IntlDateFormatter::LONG, $locale = null)
  67. {
  68. if (!$element) {
  69. return $this;
  70. }
  71. $this->setDateType($dateType);
  72. if ($locale !== null) {
  73. $this->setLocale($locale);
  74. }
  75. return $this->render($element);
  76. }
  77. /**
  78. * Render a month element that is composed of two selects
  79. *
  80. * @param \Zend\Form\ElementInterface $element
  81. * @throws \Zend\Form\Exception\InvalidArgumentException
  82. * @throws \Zend\Form\Exception\DomainException
  83. * @return string
  84. */
  85. public function render(ElementInterface $element)
  86. {
  87. if (!$element instanceof MonthSelectElement) {
  88. throw new Exception\InvalidArgumentException(sprintf(
  89. '%s requires that the element is of type Zend\Form\Element\MonthSelect',
  90. __METHOD__
  91. ));
  92. }
  93. $name = $element->getName();
  94. if ($name === null || $name === '') {
  95. throw new Exception\DomainException(sprintf(
  96. '%s requires that the element has an assigned name; none discovered',
  97. __METHOD__
  98. ));
  99. }
  100. $selectHelper = $this->getSelectElementHelper();
  101. $pattern = $this->parsePattern($element->shouldRenderDelimiters());
  102. // The pattern always contains "day" part and the first separator, so we have to remove it
  103. unset($pattern['day']);
  104. unset($pattern[0]);
  105. $monthsOptions = $this->getMonthsOptions($pattern['month']);
  106. $yearOptions = $this->getYearsOptions($element->getMinYear(), $element->getMaxYear());
  107. $monthElement = $element->getMonthElement()->setValueOptions($monthsOptions);
  108. $yearElement = $element->getYearElement()->setValueOptions($yearOptions);
  109. if ($element->shouldCreateEmptyOption()) {
  110. $monthElement->setEmptyOption('');
  111. $yearElement->setEmptyOption('');
  112. }
  113. $data = array();
  114. $data[$pattern['month']] = $selectHelper->render($monthElement);
  115. $data[$pattern['year']] = $selectHelper->render($yearElement);
  116. $markup = '';
  117. foreach ($pattern as $key => $value) {
  118. // Delimiter
  119. if (is_numeric($key)) {
  120. $markup .= $value;
  121. } else {
  122. $markup .= $data[$value];
  123. }
  124. }
  125. return $markup;
  126. }
  127. /**
  128. * Parse the pattern
  129. *
  130. * @param bool $renderDelimiters
  131. * @return array
  132. */
  133. protected function parsePattern($renderDelimiters = true)
  134. {
  135. $pattern = $this->getPattern();
  136. $pregResult = preg_split("/([ -,.\/]*(?:'[a-zA-Z]+')*[ -,.\/]+)/", $pattern, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  137. $result = array();
  138. foreach ($pregResult as $value) {
  139. if (stripos($value, "'") === false && stripos($value, 'd') !== false) {
  140. $result['day'] = $value;
  141. } elseif (stripos($value, "'") === false && stripos($value, 'm') !== false) {
  142. $result['month'] = $value;
  143. } elseif (stripos($value, "'") === false && stripos($value, 'y') !== false) {
  144. $result['year'] = $value;
  145. } elseif ($renderDelimiters) {
  146. $result[] = str_replace("'", '', $value);
  147. }
  148. }
  149. return $result;
  150. }
  151. /**
  152. * Retrive pattern to use for Date rendering
  153. *
  154. * @return string
  155. */
  156. public function getPattern()
  157. {
  158. if (null === $this->pattern) {
  159. $intl = new IntlDateFormatter($this->getLocale(), $this->dateType, IntlDateFormatter::NONE);
  160. $this->pattern = $intl->getPattern();
  161. }
  162. return $this->pattern;
  163. }
  164. /**
  165. * Set date formatter
  166. *
  167. * @param int $dateType
  168. * @return FormDateSelect
  169. */
  170. public function setDateType($dateType)
  171. {
  172. // The FULL format uses values that are not used
  173. if ($dateType === IntlDateFormatter::FULL) {
  174. $dateType = IntlDateFormatter::LONG;
  175. }
  176. $this->dateType = $dateType;
  177. return $this;
  178. }
  179. /**
  180. * Get date formatter
  181. *
  182. * @return int
  183. */
  184. public function getDateType()
  185. {
  186. return $this->dateType;
  187. }
  188. /**
  189. * Set locale
  190. *
  191. * @param string $locale
  192. * @return FormDateSelect
  193. */
  194. public function setLocale($locale)
  195. {
  196. $this->locale = $locale;
  197. return $this;
  198. }
  199. /**
  200. * Get locale
  201. *
  202. * @return string
  203. */
  204. public function getLocale()
  205. {
  206. if (null === $this->locale) {
  207. $this->locale = Locale::getDefault();
  208. }
  209. return $this->locale;
  210. }
  211. /**
  212. * Create a key => value options for months
  213. *
  214. * @param string $pattern Pattern to use for months
  215. * @return array
  216. */
  217. protected function getMonthsOptions($pattern)
  218. {
  219. $keyFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, 'MM');
  220. $valueFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, $pattern);
  221. $date = new DateTime('1970-01-01');
  222. $result = array();
  223. for ($month = 1; $month <= 12; $month++) {
  224. $key = $keyFormatter->format($date);
  225. $value = $valueFormatter->format($date);
  226. $result[$key] = $value;
  227. $date->modify('+1 month');
  228. }
  229. return $result;
  230. }
  231. /**
  232. * Create a key => value options for years
  233. * NOTE: we don't use a pattern for years, as years written as two digits can lead to hard to
  234. * read date for users, so we only use four digits years
  235. *
  236. * @param int $minYear
  237. * @param int $maxYear
  238. * @return array
  239. */
  240. protected function getYearsOptions($minYear, $maxYear)
  241. {
  242. $result = array();
  243. for ($i = $maxYear; $i >= $minYear; --$i) {
  244. $result[$i] = $i;
  245. }
  246. return $result;
  247. }
  248. /**
  249. * Retrieve the FormSelect helper
  250. *
  251. * @return FormSelect
  252. */
  253. protected function getSelectElementHelper()
  254. {
  255. if ($this->selectHelper) {
  256. return $this->selectHelper;
  257. }
  258. if (method_exists($this->view, 'plugin')) {
  259. $this->selectHelper = $this->view->plugin('formselect');
  260. }
  261. return $this->selectHelper;
  262. }
  263. }