[ZF-10089] Zend_Log
[zend/radio.git] / library / Zend / Form / Element / Multi.php
blobb459b810216ac67ebfadb510925881ea111233fa
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
15 * @category Zend
16 * @package Zend_Form
17 * @subpackage Element
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
22 /** Zend_Form_Element_Xhtml */
23 require_once 'Zend/Form/Element/Xhtml.php';
25 /**
26 * Base class for multi-option form elements
28 * @category Zend
29 * @package Zend_Form
30 * @subpackage Element
31 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
32 * @license http://framework.zend.com/license/new-bsd New BSD License
33 * @version $Id$
35 abstract class Zend_Form_Element_Multi extends Zend_Form_Element_Xhtml
37 /**
38 * Array of options for multi-item
39 * @var array
41 public $options = array();
43 /**
44 * Flag: autoregister inArray validator?
45 * @var bool
47 protected $_registerInArrayValidator = true;
49 /**
50 * Separator to use between options; defaults to '<br />'.
51 * @var string
53 protected $_separator = '<br />';
55 /**
56 * Which values are translated already?
57 * @var array
59 protected $_translated = array();
61 /**
62 * Retrieve separator
64 * @return mixed
66 public function getSeparator()
68 return $this->_separator;
71 /**
72 * Set separator
74 * @param mixed $separator
75 * @return self
77 public function setSeparator($separator)
79 $this->_separator = $separator;
80 return $this;
83 /**
84 * Retrieve options array
86 * @return array
88 protected function _getMultiOptions()
90 if (null === $this->options || !is_array($this->options)) {
91 $this->options = array();
94 return $this->options;
97 /**
98 * Add an option
100 * @param string $option
101 * @param string $value
102 * @return Zend_Form_Element_Multi
104 public function addMultiOption($option, $value = '')
106 $option = (string) $option;
107 $this->_getMultiOptions();
108 if (!$this->_translateOption($option, $value)) {
109 $this->options[$option] = $value;
112 return $this;
116 * Add many options at once
118 * @param array $options
119 * @return Zend_Form_Element_Multi
121 public function addMultiOptions(array $options)
123 foreach ($options as $option => $value) {
124 if (is_array($value)
125 && array_key_exists('key', $value)
126 && array_key_exists('value', $value)
128 $this->addMultiOption($value['key'], $value['value']);
129 } else {
130 $this->addMultiOption($option, $value);
133 return $this;
137 * Set all options at once (overwrites)
139 * @param array $options
140 * @return Zend_Form_Element_Multi
142 public function setMultiOptions(array $options)
144 $this->clearMultiOptions();
145 return $this->addMultiOptions($options);
149 * Retrieve single multi option
151 * @param string $option
152 * @return mixed
154 public function getMultiOption($option)
156 $option = (string) $option;
157 $this->_getMultiOptions();
158 if (isset($this->options[$option])) {
159 $this->_translateOption($option, $this->options[$option]);
160 return $this->options[$option];
163 return null;
167 * Retrieve options
169 * @return array
171 public function getMultiOptions()
173 $this->_getMultiOptions();
174 foreach ($this->options as $option => $value) {
175 $this->_translateOption($option, $value);
177 return $this->options;
181 * Remove a single multi option
183 * @param string $option
184 * @return bool
186 public function removeMultiOption($option)
188 $option = (string) $option;
189 $this->_getMultiOptions();
190 if (isset($this->options[$option])) {
191 unset($this->options[$option]);
192 if (isset($this->_translated[$option])) {
193 unset($this->_translated[$option]);
195 return true;
198 return false;
202 * Clear all options
204 * @return Zend_Form_Element_Multi
206 public function clearMultiOptions()
208 $this->options = array();
209 $this->_translated = array();
210 return $this;
214 * Set flag indicating whether or not to auto-register inArray validator
216 * @param bool $flag
217 * @return Zend_Form_Element_Multi
219 public function setRegisterInArrayValidator($flag)
221 $this->_registerInArrayValidator = (bool) $flag;
222 return $this;
226 * Get status of auto-register inArray validator flag
228 * @return bool
230 public function registerInArrayValidator()
232 return $this->_registerInArrayValidator;
236 * Is the value provided valid?
238 * Autoregisters InArray validator if necessary.
240 * @param string $value
241 * @param mixed $context
242 * @return bool
244 public function isValid($value, $context = null)
246 if ($this->registerInArrayValidator()) {
247 if (!$this->getValidator('InArray')) {
248 $multiOptions = $this->getMultiOptions();
249 $options = array();
251 foreach ($multiOptions as $opt_value => $opt_label) {
252 // optgroup instead of option label
253 if (is_array($opt_label)) {
254 $options = array_merge($options, array_keys($opt_label));
256 else {
257 $options[] = $opt_value;
261 $this->addValidator(
262 'InArray',
263 true,
264 array($options)
268 return parent::isValid($value, $context);
272 * Translate an option
274 * @param string $option
275 * @param string $value
276 * @return bool
278 protected function _translateOption($option, $value)
280 if ($this->translatorIsDisabled()) {
281 return false;
284 if (!isset($this->_translated[$option]) && !empty($value)) {
285 $this->options[$option] = $this->_translateValue($value);
286 if ($this->options[$option] === $value) {
287 return false;
289 $this->_translated[$option] = true;
290 return true;
293 return false;
297 * Translate a multi option value
299 * @param string $value
300 * @return string
302 protected function _translateValue($value)
304 if (is_array($value)) {
305 foreach ($value as $key => $val) {
306 $value[$key] = $this->_translateValue($val);
308 return $value;
309 } else {
310 if (null !== ($translator = $this->getTranslator())) {
311 return $translator->translate($value);
314 return $value;