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 / Select.php
blob5b9f830955f43776bc7a39febd759c3e6192f768
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\Form\ElementInterface;
15 use Zend\Form\Exception\InvalidArgumentException;
16 use Zend\InputFilter\InputProviderInterface;
17 use Zend\Validator\Explode as ExplodeValidator;
18 use Zend\Validator\InArray as InArrayValidator;
20 class Select extends Element implements InputProviderInterface
22 /**
23 * Seed attributes
25 * @var array
27 protected $attributes = array(
28 'type' => 'select',
31 /**
32 * @var \Zend\Validator\ValidatorInterface
34 protected $validator;
36 /**
37 * @var bool
39 protected $disableInArrayValidator = false;
41 /**
42 * Create an empty option (option with label but no value). If set to null, no option is created
44 * @var bool
46 protected $emptyOption = null;
48 /**
49 * @var array
51 protected $valueOptions = array();
53 /**
54 * @return array
56 public function getValueOptions()
58 return $this->valueOptions;
61 /**
62 * @param array $options
63 * @return Select
65 public function setValueOptions(array $options)
67 $this->valueOptions = $options;
69 // Update InArrayValidator validator haystack
70 if (null !== $this->validator) {
71 if ($this->validator instanceof InArrayValidator) {
72 $validator = $this->validator;
74 if ($this->validator instanceof ExplodeValidator
75 && $this->validator->getValidator() instanceof InArrayValidator
76 ) {
77 $validator = $this->validator->getValidator();
79 if (!empty($validator)) {
80 $validator->setHaystack($this->getValueOptionsValues());
84 return $this;
87 /**
88 * Set options for an element. Accepted options are:
89 * - label: label to associate with the element
90 * - label_attributes: attributes to use when the label is rendered
91 * - value_options: list of values and labels for the select options
92 * _ empty_option: should an empty option be prepended to the options ?
94 * @param array|Traversable $options
95 * @return Select|ElementInterface
96 * @throws InvalidArgumentException
98 public function setOptions($options)
100 parent::setOptions($options);
102 if (isset($this->options['value_options'])) {
103 $this->setValueOptions($this->options['value_options']);
105 // Alias for 'value_options'
106 if (isset($this->options['options'])) {
107 $this->setValueOptions($this->options['options']);
110 if (isset($this->options['empty_option'])) {
111 $this->setEmptyOption($this->options['empty_option']);
114 if (isset($this->options['disable_inarray_validator'])) {
115 $this->setDisableInArrayValidator($this->options['disable_inarray_validator']);
118 return $this;
122 * Set a single element attribute
124 * @param string $key
125 * @param mixed $value
126 * @return Select|ElementInterface
128 public function setAttribute($key, $value)
130 // Do not include the options in the list of attributes
131 // TODO: Deprecate this
132 if ($key === 'options') {
133 $this->setValueOptions($value);
134 return $this;
136 return parent::setAttribute($key, $value);
140 * Set the flag to allow for disabling the automatic addition of an InArray validator.
142 * @param bool $disableOption
143 * @return Select
145 public function setDisableInArrayValidator($disableOption)
147 $this->disableInArrayValidator = (bool) $disableOption;
148 return $this;
152 * Get the disable in array validator flag.
154 * @return bool
156 public function disableInArrayValidator()
158 return $this->disableInArrayValidator;
162 * Set the string for an empty option (can be empty string). If set to null, no option will be added
164 * @param string|null $emptyOption
165 * @return Select
167 public function setEmptyOption($emptyOption)
169 $this->emptyOption = $emptyOption;
170 return $this;
174 * Return the string for the empty option (null if none)
176 * @return string|null
178 public function getEmptyOption()
180 return $this->emptyOption;
184 * Get validator
186 * @return \Zend\Validator\ValidatorInterface
188 protected function getValidator()
190 if (null === $this->validator && !$this->disableInArrayValidator()) {
191 $validator = new InArrayValidator(array(
192 'haystack' => $this->getValueOptionsValues(),
193 'strict' => false
196 $multiple = (isset($this->attributes['multiple']))
197 ? $this->attributes['multiple'] : null;
199 if (true === $multiple || 'multiple' === $multiple) {
200 $validator = new ExplodeValidator(array(
201 'validator' => $validator,
202 'valueDelimiter' => null, // skip explode if only one value
206 $this->validator = $validator;
208 return $this->validator;
212 * Provide default input rules for this element
214 * Attaches the captcha as a validator.
216 * @return array
218 public function getInputSpecification()
220 $spec = array(
221 'name' => $this->getName(),
222 'required' => true,
225 if ($validator = $this->getValidator()) {
226 $spec['validators'] = array(
227 $validator,
231 return $spec;
235 * Get only the values from the options attribute
237 * @return array
239 protected function getValueOptionsValues()
241 $values = array();
242 $options = $this->getValueOptions();
243 foreach ($options as $key => $optionSpec) {
244 if (is_array($optionSpec) && array_key_exists('options', $optionSpec)) {
245 foreach ($optionSpec['options'] as $nestedKey => $nestedOptionSpec) {
246 $values[] = $this->getOptionValue($nestedKey, $nestedOptionSpec);
248 continue;
251 $values[] = $this->getOptionValue($key, $optionSpec);
253 return $values;
256 protected function getOptionValue($key, $optionSpec)
258 return is_array($optionSpec) ? $optionSpec['value'] : $key;