Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Form / Element / Checkbox.php
blob87447ad01d285054291eb8d87c32d7d3f8794c36
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-2013 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 = array(
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(array(
151 'haystack' => array($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 = array(
168 'name' => $this->getName(),
169 'required' => true,
170 'validators' => array(
171 $this->getValidator()
175 return $spec;
179 * Checks if this checkbox is checked.
181 * @return bool
183 public function isChecked()
185 return $this->value === $this->getCheckedValue();
189 * Checks or unchecks the checkbox.
191 * @param bool $value The flag to set.
192 * @return Checkbox
194 public function setChecked($value)
196 $this->value = $value ? $this->getCheckedValue() : $this->getUncheckedValue();
197 return $this;
201 * Checks or unchecks the checkbox.
203 * @param mixed $value A boolean flag or string that is checked against the "checked value".
204 * @return Element
206 public function setValue($value)
208 // Cast to strings because POST data comes in string form
209 $checked = (string) $value === (string) $this->getCheckedValue();
210 $this->value = $checked ? $this->getCheckedValue() : $this->getUncheckedValue();
211 return $this;