PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://gitlab.com/daigiangaitu91/magento
PHP | 359 lines | 187 code | 43 blank | 129 comment | 24 complexity | 5dee96335475138da14d8ad1d01afa0f 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 Traversable;
  11. use Zend\Form\Element;
  12. use Zend\Form\ElementInterface;
  13. use Zend\Form\Exception\InvalidArgumentException;
  14. use Zend\InputFilter\InputProviderInterface;
  15. use Zend\Validator\Explode as ExplodeValidator;
  16. use Zend\Validator\InArray as InArrayValidator;
  17. class Select extends Element implements InputProviderInterface
  18. {
  19. /**
  20. * Seed attributes
  21. *
  22. * @var array
  23. */
  24. protected $attributes = array(
  25. 'type' => 'select',
  26. );
  27. /**
  28. * @var \Zend\Validator\ValidatorInterface
  29. */
  30. protected $validator;
  31. /**
  32. * @var bool
  33. */
  34. protected $disableInArrayValidator = false;
  35. /**
  36. * Create an empty option (option with label but no value). If set to null, no option is created
  37. *
  38. * @var bool
  39. */
  40. protected $emptyOption = null;
  41. /**
  42. * @var array
  43. */
  44. protected $valueOptions = array();
  45. /**
  46. * @var bool
  47. */
  48. protected $useHiddenElement = false;
  49. /**
  50. * @var string
  51. */
  52. protected $unselectedValue = '';
  53. /**
  54. * @return array
  55. */
  56. public function getValueOptions()
  57. {
  58. return $this->valueOptions;
  59. }
  60. /**
  61. * @param array $options
  62. * @return Select
  63. */
  64. public function setValueOptions(array $options)
  65. {
  66. $this->valueOptions = $options;
  67. // Update InArrayValidator validator haystack
  68. if (null !== $this->validator) {
  69. if ($this->validator instanceof InArrayValidator) {
  70. $validator = $this->validator;
  71. }
  72. if ($this->validator instanceof ExplodeValidator
  73. && $this->validator->getValidator() instanceof InArrayValidator
  74. ) {
  75. $validator = $this->validator->getValidator();
  76. }
  77. if (!empty($validator)) {
  78. $validator->setHaystack($this->getValueOptionsValues());
  79. }
  80. }
  81. return $this;
  82. }
  83. /**
  84. * @param string $key
  85. * @return self
  86. */
  87. public function unsetValueOption($key)
  88. {
  89. if (isset($this->valueOptions[$key])) {
  90. unset($this->valueOptions[$key]);
  91. }
  92. return $this;
  93. }
  94. /**
  95. * Set options for an element. Accepted options are:
  96. * - label: label to associate with the element
  97. * - label_attributes: attributes to use when the label is rendered
  98. * - value_options: list of values and labels for the select options
  99. * _ empty_option: should an empty option be prepended to the options ?
  100. *
  101. * @param array|Traversable $options
  102. * @return Select|ElementInterface
  103. * @throws InvalidArgumentException
  104. */
  105. public function setOptions($options)
  106. {
  107. parent::setOptions($options);
  108. if (isset($this->options['value_options'])) {
  109. $this->setValueOptions($this->options['value_options']);
  110. }
  111. // Alias for 'value_options'
  112. if (isset($this->options['options'])) {
  113. $this->setValueOptions($this->options['options']);
  114. }
  115. if (isset($this->options['empty_option'])) {
  116. $this->setEmptyOption($this->options['empty_option']);
  117. }
  118. if (isset($this->options['disable_inarray_validator'])) {
  119. $this->setDisableInArrayValidator($this->options['disable_inarray_validator']);
  120. }
  121. if (isset($options['use_hidden_element'])) {
  122. $this->setUseHiddenElement($options['use_hidden_element']);
  123. }
  124. if (isset($options['unselected_value'])) {
  125. $this->setUnselectedValue($options['unselected_value']);
  126. }
  127. return $this;
  128. }
  129. /**
  130. * Set a single element attribute
  131. *
  132. * @param string $key
  133. * @param mixed $value
  134. * @return Select|ElementInterface
  135. */
  136. public function setAttribute($key, $value)
  137. {
  138. // Do not include the options in the list of attributes
  139. // TODO: Deprecate this
  140. if ($key === 'options') {
  141. $this->setValueOptions($value);
  142. return $this;
  143. }
  144. return parent::setAttribute($key, $value);
  145. }
  146. /**
  147. * Set the flag to allow for disabling the automatic addition of an InArray validator.
  148. *
  149. * @param bool $disableOption
  150. * @return Select
  151. */
  152. public function setDisableInArrayValidator($disableOption)
  153. {
  154. $this->disableInArrayValidator = (bool) $disableOption;
  155. return $this;
  156. }
  157. /**
  158. * Get the disable in array validator flag.
  159. *
  160. * @return bool
  161. */
  162. public function disableInArrayValidator()
  163. {
  164. return $this->disableInArrayValidator;
  165. }
  166. /**
  167. * Set the string for an empty option (can be empty string). If set to null, no option will be added
  168. *
  169. * @param string|null $emptyOption
  170. * @return Select
  171. */
  172. public function setEmptyOption($emptyOption)
  173. {
  174. $this->emptyOption = $emptyOption;
  175. return $this;
  176. }
  177. /**
  178. * Return the string for the empty option (null if none)
  179. *
  180. * @return string|null
  181. */
  182. public function getEmptyOption()
  183. {
  184. return $this->emptyOption;
  185. }
  186. /**
  187. * Get validator
  188. *
  189. * @return \Zend\Validator\ValidatorInterface
  190. */
  191. protected function getValidator()
  192. {
  193. if (null === $this->validator && !$this->disableInArrayValidator()) {
  194. $validator = new InArrayValidator(array(
  195. 'haystack' => $this->getValueOptionsValues(),
  196. 'strict' => false
  197. ));
  198. if ($this->isMultiple()) {
  199. $validator = new ExplodeValidator(array(
  200. 'validator' => $validator,
  201. 'valueDelimiter' => null, // skip explode if only one value
  202. ));
  203. }
  204. $this->validator = $validator;
  205. }
  206. return $this->validator;
  207. }
  208. /**
  209. * Do we render hidden element?
  210. *
  211. * @param bool $useHiddenElement
  212. * @return Select
  213. */
  214. public function setUseHiddenElement($useHiddenElement)
  215. {
  216. $this->useHiddenElement = (bool) $useHiddenElement;
  217. return $this;
  218. }
  219. /**
  220. * Do we render hidden element?
  221. *
  222. * @return bool
  223. */
  224. public function useHiddenElement()
  225. {
  226. return $this->useHiddenElement;
  227. }
  228. /**
  229. * Set the value if the select is not selected
  230. *
  231. * @param string $unselectedValue
  232. * @return Select
  233. */
  234. public function setUnselectedValue($unselectedValue)
  235. {
  236. $this->unselectedValue = (string) $unselectedValue;
  237. return $this;
  238. }
  239. /**
  240. * Get the value when the select is not selected
  241. *
  242. * @return string
  243. */
  244. public function getUnselectedValue()
  245. {
  246. return $this->unselectedValue;
  247. }
  248. /**
  249. * Provide default input rules for this element
  250. *
  251. * @return array
  252. */
  253. public function getInputSpecification()
  254. {
  255. $spec = array(
  256. 'name' => $this->getName(),
  257. 'required' => true,
  258. );
  259. if ($this->useHiddenElement() && $this->isMultiple()) {
  260. $unselectedValue = $this->getUnselectedValue();
  261. $spec['allow_empty'] = true;
  262. $spec['continue_if_empty'] = true;
  263. $spec['filters'] = array(array(
  264. 'name' => 'Callback',
  265. 'options' => array(
  266. 'callback' => function ($value) use ($unselectedValue) {
  267. if ($value === $unselectedValue) {
  268. $value = array();
  269. }
  270. return $value;
  271. }
  272. )
  273. ));
  274. }
  275. if ($validator = $this->getValidator()) {
  276. $spec['validators'] = array(
  277. $validator,
  278. );
  279. }
  280. return $spec;
  281. }
  282. /**
  283. * Get only the values from the options attribute
  284. *
  285. * @return array
  286. */
  287. protected function getValueOptionsValues()
  288. {
  289. $values = array();
  290. $options = $this->getValueOptions();
  291. foreach ($options as $key => $optionSpec) {
  292. if (is_array($optionSpec) && array_key_exists('options', $optionSpec)) {
  293. foreach ($optionSpec['options'] as $nestedKey => $nestedOptionSpec) {
  294. $values[] = $this->getOptionValue($nestedKey, $nestedOptionSpec);
  295. }
  296. continue;
  297. }
  298. $values[] = $this->getOptionValue($key, $optionSpec);
  299. }
  300. return $values;
  301. }
  302. protected function getOptionValue($key, $optionSpec)
  303. {
  304. return is_array($optionSpec) ? $optionSpec['value'] : $key;
  305. }
  306. /**
  307. * Element has the multiple attribute
  308. *
  309. * @return bool
  310. */
  311. public function isMultiple()
  312. {
  313. return isset($this->attributes['multiple'])
  314. && ($this->attributes['multiple'] === true || $this->attributes['multiple'] === 'multiple');
  315. }
  316. }