Source of file MonthSelect.php
Size: 8,505 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Form/Element/MonthSelect.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 | <?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\Element; use DateTime as PhpDateTime; use Zend\Form\Element; use Zend\Form\ElementPrepareAwareInterface; use Zend\Form\FormInterface; use Zend\InputFilter\InputProviderInterface; use Zend\Validator\Regex as RegexValidator; use Zend\Validator\ValidatorInterface; class MonthSelect extends Element implements InputProviderInterface, ElementPrepareAwareInterface { /** * Select form element that contains values for month * * @var Select */ protected $monthElement; /** * Select form element that contains values for year * * @var Select */ protected $yearElement; /** * Min year to use for the select (default: current year - 100) * * @var int */ protected $minYear; /** * Max year to use for the select (default: current year) * * @var int */ protected $maxYear; /** * If set to true, it will generate an empty option for every select (this is mainly needed by most JavaScript * libraries to allow to have a placeholder) * * @var bool */ protected $createEmptyOption = false; /** * If set to true, view helpers will render delimiters between <select> elements, according to the * specified locale * * @var bool */ protected $renderDelimiters = true; /** * @var ValidatorInterface */ protected $validator; /** * Constructor. Add two selects elements * * @param null|int|string $name Optional name for the element * @param array $options Optional options for the element */ public function __construct($name = null, $options = array()) { $this->minYear = date('Y') - 100; $this->maxYear = date('Y'); $this->monthElement = new Select('month'); $this->yearElement = new Select('year'); parent::__construct($name, $options); } /** * Accepted options for DateSelect: * - month_attributes: HTML attributes to be rendered with the month element * - year_attributes: HTML attributes to be rendered with the month element * - min_year: min year to use in the year select * - max_year: max year to use in the year select * * @param array|\Traversable $options * @return MonthSelect */ public function setOptions($options) { parent::setOptions($options); if (isset($options['month_attributes'])) { $this->setMonthAttributes($options['month_attributes']); } if (isset($options['year_attributes'])) { $this->setYearAttributes($options['year_attributes']); } if (isset($options['min_year'])) { $this->setMinYear($options['min_year']); } if (isset($options['max_year'])) { $this->setMaxYear($options['max_year']); } if (isset($options['create_empty_option'])) { $this->setShouldCreateEmptyOption($options['create_empty_option']); } if (isset($options['render_delimiters'])) { $this->setShouldRenderDelimiters($options['render_delimiters']); } return $this; } /** * @return Select */ public function getMonthElement() { return $this->monthElement; } /** * @return Select */ public function getYearElement() { return $this->yearElement; } /** * Set the month attributes * * @param array $monthAttributes * @return MonthSelect */ public function setMonthAttributes(array $monthAttributes) { $this->monthElement->setAttributes($monthAttributes); return $this; } /** * Get the month attributes * * @return array */ public function getMonthAttributes() { return $this->monthElement->getAttributes(); } /** * Set the year attributes * * @param array $yearAttributes * @return MonthSelect */ public function setYearAttributes(array $yearAttributes) { $this->yearElement->setAttributes($yearAttributes); return $this; } /** * Get the year attributes * * @return array */ public function getYearAttributes() { return $this->yearElement->getAttributes(); } /** * @param int $minYear * @return MonthSelect */ public function setMinYear($minYear) { $this->minYear = $minYear; return $this; } /** * @return int */ public function getMinYear() { return $this->minYear; } /** * @param int $maxYear * @return MonthSelect */ public function setMaxYear($maxYear) { $this->maxYear = $maxYear; return $this; } /** * @return int */ public function getMaxYear() { return $this->maxYear; } /** * @param bool $createEmptyOption * @return MonthSelect */ public function setShouldCreateEmptyOption($createEmptyOption) { $this->createEmptyOption = (bool) $createEmptyOption; return $this; } /** * @return bool */ public function shouldCreateEmptyOption() { return $this->createEmptyOption; } /** * @param bool $renderDelimiters * @return MonthSelect */ public function setShouldRenderDelimiters($renderDelimiters) { $this->renderDelimiters = (bool) $renderDelimiters; return $this; } /** * @return bool */ public function shouldRenderDelimiters() { return $this->renderDelimiters; } /** * @param mixed $value * @return void|\Zend\Form\Element */ public function setValue($value) { if ($value instanceof PhpDateTime) { $value = array( 'year' => $value->format('Y'), 'month' => $value->format('m') ); } $this->yearElement->setValue($value['year']); $this->monthElement->setValue($value['month']); } /** * @return String */ public function getValue() { return sprintf('%s-%s', $this->getYearElement()->getValue(), $this->getMonthElement()->getValue() ); } /** * Prepare the form element (mostly used for rendering purposes) * * @param FormInterface $form * @return mixed */ public function prepareElement(FormInterface $form) { $name = $this->getName(); $this->monthElement->setName($name . '[month]'); $this->yearElement->setName($name . '[year]'); } /** * Get validator * * @return ValidatorInterface */ protected function getValidator() { return new RegexValidator('/^[0-9]{4}\-(0?[1-9]|1[012])$/'); } /** * Should return an array specification compatible with * {@link Zend\InputFilter\Factory::createInput()}. * * @return array */ public function getInputSpecification() { return array( 'name' => $this->getName(), 'required' => false, 'filters' => array( array( 'name' => 'Callback', 'options' => array( 'callback' => function ($date) { // Convert the date to a specific format if (is_array($date)) { $date = $date['year'] . '-' . $date['month']; } return $date; } ) ) ), 'validators' => array( $this->getValidator(), ) ); } /** * Clone the element (this is needed by Collection element, as it needs different copies of the elements) */ public function __clone() { $this->monthElement = clone $this->monthElement; $this->yearElement = clone $this->yearElement; } } |