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 / MultiCheckbox.php
blob40bb8a847eff55fcb764a42371e663f81d28ca27
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 Zend\Form\ElementInterface;
13 use Zend\Form\Exception\InvalidArgumentException;
14 use Zend\Validator\Explode as ExplodeValidator;
15 use Zend\Validator\InArray as InArrayValidator;
16 use Zend\Validator\ValidatorInterface;
18 class MultiCheckbox extends Checkbox
20 /**
21 * Seed attributes
23 * @var array
25 protected $attributes = array(
26 'type' => 'multi_checkbox',
29 /**
30 * @var bool
32 protected $useHiddenElement = false;
34 /**
35 * @var string
37 protected $uncheckedValue = '';
39 /**
40 * @var array
42 protected $valueOptions = array();
44 /**
45 * @return array
47 public function getValueOptions()
49 return $this->valueOptions;
52 /**
53 * @param array $options
54 * @return MultiCheckbox
56 public function setValueOptions(array $options)
58 $this->valueOptions = $options;
60 // Update Explode validator haystack
61 if ($this->validator instanceof ExplodeValidator) {
62 $validator = $this->validator->getValidator();
63 $validator->setHaystack($this->getValueOptionsValues());
66 return $this;
69 /**
70 * Set options for an element. Accepted options are:
71 * - label: label to associate with the element
72 * - label_attributes: attributes to use when the label is rendered
73 * - value_options: list of values and labels for the select options
75 * @param array|\Traversable $options
76 * @return MultiCheckbox|ElementInterface
77 * @throws InvalidArgumentException
79 public function setOptions($options)
81 parent::setOptions($options);
83 if (isset($this->options['value_options'])) {
84 $this->setValueOptions($this->options['value_options']);
86 // Alias for 'value_options'
87 if (isset($this->options['options'])) {
88 $this->setValueOptions($this->options['options']);
91 return $this;
94 /**
95 * Set a single element attribute
97 * @param string $key
98 * @param mixed $value
99 * @return MultiCheckbox|ElementInterface
101 public function setAttribute($key, $value)
103 // Do not include the options in the list of attributes
104 // TODO: Deprecate this
105 if ($key === 'options') {
106 $this->setValueOptions($value);
107 return $this;
109 return parent::setAttribute($key, $value);
113 * Get validator
115 * @return ValidatorInterface
117 protected function getValidator()
119 if (null === $this->validator) {
120 $inArrayValidator = new InArrayValidator(array(
121 'haystack' => $this->getValueOptionsValues(),
122 'strict' => false,
124 $this->validator = new ExplodeValidator(array(
125 'validator' => $inArrayValidator,
126 'valueDelimiter' => null, // skip explode if only one value
129 return $this->validator;
133 * Get only the values from the options attribute
135 * @return array
137 protected function getValueOptionsValues()
139 $values = array();
140 $options = $this->getValueOptions();
141 foreach ($options as $key => $optionSpec) {
142 $value = (is_array($optionSpec)) ? $optionSpec['value'] : $key;
143 $values[] = $value;
145 if ($this->useHiddenElement()) {
146 $values[] = $this->getUncheckedValue();
148 return $values;
152 * Sets the value that should be selected.
154 * @param mixed $value The value to set.
155 * @return MultiCheckbox
157 public function setValue($value)
159 $this->value = $value;
160 return $this;