Source of file FormMonthSelect.php
Size: 8,231 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Form/View/Helper/FormMonthSelect.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Form\View\Helper; use DateTime; use IntlDateFormatter; use Locale; use Zend\Form\ElementInterface; use Zend\Form\Element\MonthSelect as MonthSelectElement; use Zend\Form\Exception; class FormMonthSelect extends AbstractHelper { /** * FormSelect helper * * @var FormSelect */ protected $selectHelper; /** * Date formatter to use * * @var int */ protected $dateType; /** * Pattern to use for Date rendering * * @var string */ protected $pattern; /** * Locale to use * * @var string */ protected $locale; /** * @throws Exception\ExtensionNotLoadedException if ext/intl is not present */ public function __construct() { if (!extension_loaded('intl')) { throw new Exception\ExtensionNotLoadedException(sprintf( '%s component requires the intl PHP extension', __NAMESPACE__ )); } // Delaying initialization until we know ext/intl is available $this->dateType = IntlDateFormatter::LONG; } /** * Invoke helper as function * * Proxies to {@link render()}. * * @param ElementInterface $element * @param int $dateType * @param null|string $locale * @return FormDateSelect */ public function __invoke(ElementInterface $element = null, $dateType = IntlDateFormatter::LONG, $locale = null) { if (!$element) { return $this; } $this->setDateType($dateType); if ($locale !== null) { $this->setLocale($locale); } return $this->render($element); } /** * Render a month element that is composed of two selects * * @param \Zend\Form\ElementInterface $element * @throws \Zend\Form\Exception\InvalidArgumentException * @throws \Zend\Form\Exception\DomainException * @return string */ public function render(ElementInterface $element) { if (!$element instanceof MonthSelectElement) { throw new Exception\InvalidArgumentException(sprintf( '%s requires that the element is of type Zend\Form\Element\MonthSelect', __METHOD__ )); } $name = $element->getName(); if ($name === null || $name === '') { throw new Exception\DomainException(sprintf( '%s requires that the element has an assigned name; none discovered', __METHOD__ )); } $selectHelper = $this->getSelectElementHelper(); $pattern = $this->parsePattern($element->shouldRenderDelimiters()); // The pattern always contains "day" part and the first separator, so we have to remove it unset($pattern['day']); unset($pattern[0]); $monthsOptions = $this->getMonthsOptions($pattern['month']); $yearOptions = $this->getYearsOptions($element->getMinYear(), $element->getMaxYear()); $monthElement = $element->getMonthElement()->setValueOptions($monthsOptions); $yearElement = $element->getYearElement()->setValueOptions($yearOptions); if ($element->shouldCreateEmptyOption()) { $monthElement->setEmptyOption(''); $yearElement->setEmptyOption(''); } $data = array(); $data[$pattern['month']] = $selectHelper->render($monthElement); $data[$pattern['year']] = $selectHelper->render($yearElement); $markup = ''; foreach ($pattern as $key => $value) { // Delimiter if (is_numeric($key)) { $markup .= $value; } else { $markup .= $data[$value]; } } return $markup; } /** * Parse the pattern * * @param bool $renderDelimiters * @return array */ protected function parsePattern($renderDelimiters = true) { $pattern = $this->getPattern(); $pregResult = preg_split("/([ -,.\/]*(?:'[a-zA-Z]+')*[ -,.\/]+)/", $pattern, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); $result = array(); foreach ($pregResult as $value) { if (stripos($value, "'") === false && stripos($value, 'd') !== false) { $result['day'] = $value; } elseif (stripos($value, "'") === false && stripos($value, 'm') !== false) { $result['month'] = $value; } elseif (stripos($value, "'") === false && stripos($value, 'y') !== false) { $result['year'] = $value; } elseif ($renderDelimiters) { $result[] = str_replace("'", '', $value); } } return $result; } /** * Retrieve pattern to use for Date rendering * * @return string */ public function getPattern() { if (null === $this->pattern) { $intl = new IntlDateFormatter($this->getLocale(), $this->dateType, IntlDateFormatter::NONE); $this->pattern = $intl->getPattern(); } return $this->pattern; } /** * Set date formatter * * @param int $dateType * @return FormDateSelect */ public function setDateType($dateType) { // The FULL format uses values that are not used if ($dateType === IntlDateFormatter::FULL) { $dateType = IntlDateFormatter::LONG; } $this->dateType = $dateType; return $this; } /** * Get date formatter * * @return int */ public function getDateType() { return $this->dateType; } /** * Set locale * * @param string $locale * @return FormDateSelect */ public function setLocale($locale) { $this->locale = $locale; return $this; } /** * Get locale * * @return string */ public function getLocale() { if (null === $this->locale) { $this->locale = Locale::getDefault(); } return $this->locale; } /** * Create a key => value options for months * * @param string $pattern Pattern to use for months * @return array */ protected function getMonthsOptions($pattern) { $keyFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, 'MM'); $valueFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, $pattern); $date = new DateTime('1970-01-01'); $result = array(); for ($month = 1; $month <= 12; $month++) { $key = $keyFormatter->format($date->getTimestamp()); $value = $valueFormatter->format($date->getTimestamp()); $result[$key] = $value; $date->modify('+1 month'); } return $result; } /** * Create a key => value options for years * NOTE: we don't use a pattern for years, as years written as two digits can lead to hard to * read date for users, so we only use four digits years * * @param int $minYear * @param int $maxYear * @return array */ protected function getYearsOptions($minYear, $maxYear) { $result = array(); for ($i = $maxYear; $i >= $minYear; --$i) { $result[$i] = $i; } return $result; } /** * Retrieve the FormSelect helper * * @return FormSelect */ protected function getSelectElementHelper() { if ($this->selectHelper) { return $this->selectHelper; } if (method_exists($this->view, 'plugin')) { $this->selectHelper = $this->view->plugin('formselect'); } return $this->selectHelper; } } |