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

/vendor/zendframework/zend-form/src/Element/MonthSelect.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 361 lines | 169 code | 43 blank | 149 comment | 8 complexity | 9289e80a5d2d51e10106232204755028 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\Form\Element;
  10. use DateTime as PhpDateTime;
  11. use Traversable;
  12. use Zend\Form\Element;
  13. use Zend\Form\ElementPrepareAwareInterface;
  14. use Zend\Form\FormInterface;
  15. use Zend\InputFilter\InputProviderInterface;
  16. use Zend\Stdlib\ArrayUtils;
  17. use Zend\Validator\Regex as RegexValidator;
  18. use Zend\Validator\ValidatorInterface;
  19. class MonthSelect extends Element implements InputProviderInterface, ElementPrepareAwareInterface
  20. {
  21. /**
  22. * Select form element that contains values for month
  23. *
  24. * @var Select
  25. */
  26. protected $monthElement;
  27. /**
  28. * Select form element that contains values for year
  29. *
  30. * @var Select
  31. */
  32. protected $yearElement;
  33. /**
  34. * Min year to use for the select (default: current year - 100)
  35. *
  36. * @var int
  37. */
  38. protected $minYear;
  39. /**
  40. * Max year to use for the select (default: current year)
  41. *
  42. * @var int
  43. */
  44. protected $maxYear;
  45. /**
  46. * If set to true, it will generate an empty option for every select (this is mainly needed by most JavaScript
  47. * libraries to allow to have a placeholder)
  48. *
  49. * @var bool
  50. */
  51. protected $createEmptyOption = false;
  52. /**
  53. * If set to true, view helpers will render delimiters between <select> elements, according to the
  54. * specified locale
  55. *
  56. * @var bool
  57. */
  58. protected $renderDelimiters = true;
  59. /**
  60. * @var ValidatorInterface
  61. */
  62. protected $validator;
  63. /**
  64. * Constructor. Add two selects elements
  65. *
  66. * @param null|int|string $name Optional name for the element
  67. * @param array $options Optional options for the element
  68. */
  69. public function __construct($name = null, $options = array())
  70. {
  71. $this->minYear = date('Y') - 100;
  72. $this->maxYear = date('Y');
  73. $this->monthElement = new Select('month');
  74. $this->yearElement = new Select('year');
  75. parent::__construct($name, $options);
  76. }
  77. /**
  78. * Set element options.
  79. *
  80. * Accepted options for MonthSelect:
  81. *
  82. * - month_attributes: HTML attributes to be rendered with the month element
  83. * - year_attributes: HTML attributes to be rendered with the month element
  84. * - min_year: min year to use in the year select
  85. * - max_year: max year to use in the year select
  86. *
  87. * @param array|Traversable $options
  88. * @return self
  89. */
  90. public function setOptions($options)
  91. {
  92. parent::setOptions($options);
  93. if ($options instanceof Traversable) {
  94. $options = ArrayUtils::iteratorToArray($options);
  95. }
  96. if (isset($options['month_attributes'])) {
  97. $this->setMonthAttributes($options['month_attributes']);
  98. }
  99. if (isset($options['year_attributes'])) {
  100. $this->setYearAttributes($options['year_attributes']);
  101. }
  102. if (isset($options['min_year'])) {
  103. $this->setMinYear($options['min_year']);
  104. }
  105. if (isset($options['max_year'])) {
  106. $this->setMaxYear($options['max_year']);
  107. }
  108. if (isset($options['create_empty_option'])) {
  109. $this->setShouldCreateEmptyOption($options['create_empty_option']);
  110. }
  111. if (isset($options['render_delimiters'])) {
  112. $this->setShouldRenderDelimiters($options['render_delimiters']);
  113. }
  114. return $this;
  115. }
  116. /**
  117. * @return Select
  118. */
  119. public function getMonthElement()
  120. {
  121. return $this->monthElement;
  122. }
  123. /**
  124. * @return Select
  125. */
  126. public function getYearElement()
  127. {
  128. return $this->yearElement;
  129. }
  130. /**
  131. * Get both the year and month elements
  132. *
  133. * @return array
  134. */
  135. public function getElements()
  136. {
  137. return array($this->monthElement, $this->yearElement);
  138. }
  139. /**
  140. * Set the month attributes
  141. *
  142. * @param array $monthAttributes
  143. * @return self
  144. */
  145. public function setMonthAttributes(array $monthAttributes)
  146. {
  147. $this->monthElement->setAttributes($monthAttributes);
  148. return $this;
  149. }
  150. /**
  151. * Get the month attributes
  152. *
  153. * @return array
  154. */
  155. public function getMonthAttributes()
  156. {
  157. return $this->monthElement->getAttributes();
  158. }
  159. /**
  160. * Set the year attributes
  161. *
  162. * @param array $yearAttributes
  163. * @return self
  164. */
  165. public function setYearAttributes(array $yearAttributes)
  166. {
  167. $this->yearElement->setAttributes($yearAttributes);
  168. return $this;
  169. }
  170. /**
  171. * Get the year attributes
  172. *
  173. * @return array
  174. */
  175. public function getYearAttributes()
  176. {
  177. return $this->yearElement->getAttributes();
  178. }
  179. /**
  180. * @param int $minYear
  181. * @return self
  182. */
  183. public function setMinYear($minYear)
  184. {
  185. $this->minYear = $minYear;
  186. return $this;
  187. }
  188. /**
  189. * @return int
  190. */
  191. public function getMinYear()
  192. {
  193. return $this->minYear;
  194. }
  195. /**
  196. * @param int $maxYear
  197. * @return self
  198. */
  199. public function setMaxYear($maxYear)
  200. {
  201. $this->maxYear = $maxYear;
  202. return $this;
  203. }
  204. /**
  205. * @return int
  206. */
  207. public function getMaxYear()
  208. {
  209. return $this->maxYear;
  210. }
  211. /**
  212. * @param bool $createEmptyOption
  213. * @return self
  214. */
  215. public function setShouldCreateEmptyOption($createEmptyOption)
  216. {
  217. $this->createEmptyOption = (bool) $createEmptyOption;
  218. return $this;
  219. }
  220. /**
  221. * @return bool
  222. */
  223. public function shouldCreateEmptyOption()
  224. {
  225. return $this->createEmptyOption;
  226. }
  227. /**
  228. * @param bool $renderDelimiters
  229. * @return self
  230. */
  231. public function setShouldRenderDelimiters($renderDelimiters)
  232. {
  233. $this->renderDelimiters = (bool) $renderDelimiters;
  234. return $this;
  235. }
  236. /**
  237. * @return bool
  238. */
  239. public function shouldRenderDelimiters()
  240. {
  241. return $this->renderDelimiters;
  242. }
  243. /**
  244. * @param mixed $value
  245. * @return self
  246. */
  247. public function setValue($value)
  248. {
  249. if ($value instanceof PhpDateTime) {
  250. $value = array(
  251. 'year' => $value->format('Y'),
  252. 'month' => $value->format('m')
  253. );
  254. }
  255. $this->yearElement->setValue($value['year']);
  256. $this->monthElement->setValue($value['month']);
  257. return $this;
  258. }
  259. /**
  260. * @return string
  261. */
  262. public function getValue()
  263. {
  264. return sprintf(
  265. '%s-%s',
  266. $this->getYearElement()->getValue(),
  267. $this->getMonthElement()->getValue()
  268. );
  269. }
  270. /**
  271. * Prepare the form element (mostly used for rendering purposes)
  272. *
  273. * @param FormInterface $form
  274. * @return void
  275. */
  276. public function prepareElement(FormInterface $form)
  277. {
  278. $name = $this->getName();
  279. $this->monthElement->setName($name . '[month]');
  280. $this->yearElement->setName($name . '[year]');
  281. }
  282. /**
  283. * Get validator
  284. *
  285. * @return ValidatorInterface
  286. */
  287. protected function getValidator()
  288. {
  289. return new RegexValidator('/^[0-9]{4}\-(0?[1-9]|1[012])$/');
  290. }
  291. /**
  292. * Should return an array specification compatible with
  293. * {@link Zend\InputFilter\Factory::createInput()}.
  294. *
  295. * @return array
  296. */
  297. public function getInputSpecification()
  298. {
  299. return array(
  300. 'name' => $this->getName(),
  301. 'required' => false,
  302. 'filters' => array(
  303. array('name' => 'MonthSelect'),
  304. ),
  305. 'validators' => array(
  306. $this->getValidator(),
  307. ),
  308. );
  309. }
  310. /**
  311. * Clone the element (this is needed by Collection element, as it needs different copies of the elements)
  312. */
  313. public function __clone()
  314. {
  315. $this->monthElement = clone $this->monthElement;
  316. $this->yearElement = clone $this->yearElement;
  317. }
  318. }