upgrade zend (#1559)
[openemr.git] / vendor / zendframework / zend-form / src / Element / Checkbox.php
blob32683155b04b0332d9265f6ad75333e0873e2446
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
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 */
10 namespace Zend\Form\Element;
12 use Traversable;
13 use Zend\Form\Element;
14 use Zend\InputFilter\InputProviderInterface;
15 use Zend\Validator\InArray as InArrayValidator;
17 class Checkbox extends Element implements InputProviderInterface
19 /**
20 * Seed attributes
22 * @var array
24 protected $attributes = [
25 'type' => 'checkbox'
28 /**
29 * @var \Zend\Validator\ValidatorInterface
31 protected $validator;
33 /**
34 * @var bool
36 protected $useHiddenElement = true;
38 /**
39 * @var string
41 protected $uncheckedValue = '0';
43 /**
44 * @var string
46 protected $checkedValue = '1';
48 /**
49 * Accepted options for MultiCheckbox:
50 * - use_hidden_element: do we render hidden element?
51 * - unchecked_value: value for checkbox when unchecked
52 * - checked_value: value for checkbox when checked
54 * @param array|Traversable $options
55 * @return Checkbox
57 public function setOptions($options)
59 parent::setOptions($options);
61 if (isset($options['use_hidden_element'])) {
62 $this->setUseHiddenElement($options['use_hidden_element']);
65 if (isset($options['unchecked_value'])) {
66 $this->setUncheckedValue($options['unchecked_value']);
69 if (isset($options['checked_value'])) {
70 $this->setCheckedValue($options['checked_value']);
73 return $this;
76 /**
77 * Do we render hidden element?
79 * @param bool $useHiddenElement
80 * @return Checkbox
82 public function setUseHiddenElement($useHiddenElement)
84 $this->useHiddenElement = (bool) $useHiddenElement;
85 return $this;
88 /**
89 * Do we render hidden element?
91 * @return bool
93 public function useHiddenElement()
95 return $this->useHiddenElement;
98 /**
99 * Set the value to use when checkbox is unchecked
101 * @param $uncheckedValue
102 * @return Checkbox
104 public function setUncheckedValue($uncheckedValue)
106 $this->uncheckedValue = $uncheckedValue;
107 return $this;
111 * Get the value to use when checkbox is unchecked
113 * @return string
115 public function getUncheckedValue()
117 return $this->uncheckedValue;
121 * Set the value to use when checkbox is checked
123 * @param $checkedValue
124 * @return Checkbox
126 public function setCheckedValue($checkedValue)
128 $this->checkedValue = $checkedValue;
129 return $this;
133 * Get the value to use when checkbox is checked
135 * @return string
137 public function getCheckedValue()
139 return $this->checkedValue;
143 * Get validator
145 * @return \Zend\Validator\ValidatorInterface
147 protected function getValidator()
149 if (null === $this->validator) {
150 $this->validator = new InArrayValidator([
151 'haystack' => [$this->checkedValue, $this->uncheckedValue],
152 'strict' => false
155 return $this->validator;
159 * Provide default input rules for this element
161 * Attaches the captcha as a validator.
163 * @return array
165 public function getInputSpecification()
167 $spec = [
168 'name' => $this->getName(),
169 'required' => true,
172 if ($validator = $this->getValidator()) {
173 $spec['validators'] = [
174 $validator,
178 return $spec;
182 * Checks if this checkbox is checked.
184 * @return bool
186 public function isChecked()
188 return $this->value === $this->getCheckedValue();
192 * Checks or unchecks the checkbox.
194 * @param bool $value The flag to set.
195 * @return Checkbox
197 public function setChecked($value)
199 $this->value = $value ? $this->getCheckedValue() : $this->getUncheckedValue();
200 return $this;
204 * Checks or unchecks the checkbox.
206 * @param mixed $value A boolean flag or string that is checked against the "checked value".
207 * @return Element
209 public function setValue($value)
211 // Cast to strings because POST data comes in string form
212 $checked = (string) $value === (string) $this->getCheckedValue();
213 $this->value = $checked ? $this->getCheckedValue() : $this->getUncheckedValue();
214 return $this;