/library/Zend/Form/Element/MultiCheckbox.php

https://github.com/ant7/zf2 · PHP · 205 lines · 100 code · 22 blank · 83 comment · 9 complexity · c99563e0e94dde5a838b4e0ca10df1c4 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-2014 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 Zend\Form\ElementInterface;
  11. use Zend\Form\Exception\InvalidArgumentException;
  12. use Zend\Validator\Explode as ExplodeValidator;
  13. use Zend\Validator\InArray as InArrayValidator;
  14. use Zend\Validator\ValidatorInterface;
  15. class MultiCheckbox extends Checkbox
  16. {
  17. /**
  18. * Seed attributes
  19. *
  20. * @var array
  21. */
  22. protected $attributes = array(
  23. 'type' => 'multi_checkbox',
  24. );
  25. /**
  26. * @var bool
  27. */
  28. protected $disableInArrayValidator = false;
  29. /**
  30. * @var bool
  31. */
  32. protected $useHiddenElement = false;
  33. /**
  34. * @var string
  35. */
  36. protected $uncheckedValue = '';
  37. /**
  38. * @var array
  39. */
  40. protected $valueOptions = array();
  41. /**
  42. * @return array
  43. */
  44. public function getValueOptions()
  45. {
  46. return $this->valueOptions;
  47. }
  48. /**
  49. * @param array $options
  50. * @return MultiCheckbox
  51. */
  52. public function setValueOptions(array $options)
  53. {
  54. $this->valueOptions = $options;
  55. // Update Explode validator haystack
  56. if ($this->validator instanceof ExplodeValidator) {
  57. $validator = $this->validator->getValidator();
  58. $validator->setHaystack($this->getValueOptionsValues());
  59. }
  60. return $this;
  61. }
  62. /**
  63. * @param string $key
  64. * @return self
  65. */
  66. public function unsetValueOption($key)
  67. {
  68. if (isset($this->valueOptions[$key])) {
  69. unset($this->valueOptions[$key]);
  70. }
  71. return $this;
  72. }
  73. /**
  74. * Set options for an element. Accepted options are:
  75. * - label: label to associate with the element
  76. * - label_attributes: attributes to use when the label is rendered
  77. * - value_options: list of values and labels for the select options
  78. *
  79. * @param array|\Traversable $options
  80. * @return MultiCheckbox|ElementInterface
  81. * @throws InvalidArgumentException
  82. */
  83. public function setOptions($options)
  84. {
  85. parent::setOptions($options);
  86. if (isset($this->options['value_options'])) {
  87. $this->setValueOptions($this->options['value_options']);
  88. }
  89. // Alias for 'value_options'
  90. if (isset($this->options['options'])) {
  91. $this->setValueOptions($this->options['options']);
  92. }
  93. if (isset($this->options['disable_inarray_validator'])) {
  94. $this->setDisableInArrayValidator($this->options['disable_inarray_validator']);
  95. }
  96. return $this;
  97. }
  98. /**
  99. * Set a single element attribute
  100. *
  101. * @param string $key
  102. * @param mixed $value
  103. * @return MultiCheckbox|ElementInterface
  104. */
  105. public function setAttribute($key, $value)
  106. {
  107. // Do not include the options in the list of attributes
  108. // TODO: Deprecate this
  109. if ($key === 'options') {
  110. $this->setValueOptions($value);
  111. return $this;
  112. }
  113. return parent::setAttribute($key, $value);
  114. }
  115. /**
  116. * Set the flag to allow for disabling the automatic addition of an InArray validator.
  117. *
  118. * @param bool $disableOption
  119. * @return Select
  120. */
  121. public function setDisableInArrayValidator($disableOption)
  122. {
  123. $this->disableInArrayValidator = (bool) $disableOption;
  124. return $this;
  125. }
  126. /**
  127. * Get the disable in array validator flag.
  128. *
  129. * @return bool
  130. */
  131. public function disableInArrayValidator()
  132. {
  133. return $this->disableInArrayValidator;
  134. }
  135. /**
  136. * Get validator
  137. *
  138. * @return ValidatorInterface
  139. */
  140. protected function getValidator()
  141. {
  142. if (null === $this->validator && !$this->disableInArrayValidator()) {
  143. $inArrayValidator = new InArrayValidator(array(
  144. 'haystack' => $this->getValueOptionsValues(),
  145. 'strict' => false,
  146. ));
  147. $this->validator = new ExplodeValidator(array(
  148. 'validator' => $inArrayValidator,
  149. 'valueDelimiter' => null, // skip explode if only one value
  150. ));
  151. }
  152. return $this->validator;
  153. }
  154. /**
  155. * Get only the values from the options attribute
  156. *
  157. * @return array
  158. */
  159. protected function getValueOptionsValues()
  160. {
  161. $values = array();
  162. $options = $this->getValueOptions();
  163. foreach ($options as $key => $optionSpec) {
  164. $value = (is_array($optionSpec)) ? $optionSpec['value'] : $key;
  165. $values[] = $value;
  166. }
  167. if ($this->useHiddenElement()) {
  168. $values[] = $this->getUncheckedValue();
  169. }
  170. return $values;
  171. }
  172. /**
  173. * Sets the value that should be selected.
  174. *
  175. * @param mixed $value The value to set.
  176. * @return MultiCheckbox
  177. */
  178. public function setValue($value)
  179. {
  180. $this->value = $value;
  181. return $this;
  182. }
  183. }