/library/Zend/Form/Element/Multi.php

https://github.com/MarcelloDuarte/zf2 · PHP · 319 lines · 151 code · 29 blank · 139 comment · 21 complexity · 2150db1e90d61d2796138740aad6cb19 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Form
  17. * @subpackage Element
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Form\Element;
  25. /**
  26. * Base class for multi-option form elements
  27. *
  28. * @uses \Zend\Form\Element\Xhtml
  29. * @category Zend
  30. * @package Zend_Form
  31. * @subpackage Element
  32. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. abstract class Multi extends Xhtml
  36. {
  37. /**
  38. * Array of options for multi-item
  39. * @var array
  40. */
  41. public $options = array();
  42. /**
  43. * Flag: autoregister inArray validator?
  44. * @var bool
  45. */
  46. protected $_registerInArrayValidator = true;
  47. /**
  48. * Separator to use between options; defaults to '<br />'.
  49. * @var string
  50. */
  51. protected $_separator = '<br />';
  52. /**
  53. * Which values are translated already?
  54. * @var array
  55. */
  56. protected $_translated = array();
  57. /**
  58. * Retrieve separator
  59. *
  60. * @return mixed
  61. */
  62. public function getSeparator()
  63. {
  64. return $this->_separator;
  65. }
  66. /**
  67. * Set separator
  68. *
  69. * @param mixed $separator
  70. * @return self
  71. */
  72. public function setSeparator($separator)
  73. {
  74. $this->_separator = $separator;
  75. return $this;
  76. }
  77. /**
  78. * Retrieve options array
  79. *
  80. * @return array
  81. */
  82. protected function _getMultiOptions()
  83. {
  84. if (null === $this->options || !is_array($this->options)) {
  85. $this->options = array();
  86. }
  87. return $this->options;
  88. }
  89. /**
  90. * Add an option
  91. *
  92. * @param string $option
  93. * @param string $value
  94. * @return \Zend\Form\Element\Multi
  95. */
  96. public function addMultiOption($option, $value = '')
  97. {
  98. $option = (string) $option;
  99. $this->_getMultiOptions();
  100. if (!$this->_translateOption($option, $value)) {
  101. $this->options[$option] = $value;
  102. }
  103. return $this;
  104. }
  105. /**
  106. * Add many options at once
  107. *
  108. * @param array $options
  109. * @return \Zend\Form\Element\Multi
  110. */
  111. public function addMultiOptions(array $options)
  112. {
  113. foreach ($options as $option => $value) {
  114. if (is_array($value)
  115. && array_key_exists('key', $value)
  116. && array_key_exists('value', $value)
  117. ) {
  118. $this->addMultiOption($value['key'], $value['value']);
  119. } else {
  120. $this->addMultiOption($option, $value);
  121. }
  122. }
  123. return $this;
  124. }
  125. /**
  126. * Set all options at once (overwrites)
  127. *
  128. * @param array $options
  129. * @return \Zend\Form\Element\Multi
  130. */
  131. public function setMultiOptions(array $options)
  132. {
  133. $this->clearMultiOptions();
  134. return $this->addMultiOptions($options);
  135. }
  136. /**
  137. * Retrieve single multi option
  138. *
  139. * @param string $option
  140. * @return mixed
  141. */
  142. public function getMultiOption($option)
  143. {
  144. $option = (string) $option;
  145. $this->_getMultiOptions();
  146. if (isset($this->options[$option])) {
  147. $this->_translateOption($option, $this->options[$option]);
  148. return $this->options[$option];
  149. }
  150. return null;
  151. }
  152. /**
  153. * Retrieve options
  154. *
  155. * @return array
  156. */
  157. public function getMultiOptions()
  158. {
  159. $this->_getMultiOptions();
  160. foreach ($this->options as $option => $value) {
  161. $this->_translateOption($option, $value);
  162. }
  163. return $this->options;
  164. }
  165. /**
  166. * Remove a single multi option
  167. *
  168. * @param string $option
  169. * @return bool
  170. */
  171. public function removeMultiOption($option)
  172. {
  173. $option = (string) $option;
  174. $this->_getMultiOptions();
  175. if (isset($this->options[$option])) {
  176. unset($this->options[$option]);
  177. if (isset($this->_translated[$option])) {
  178. unset($this->_translated[$option]);
  179. }
  180. return true;
  181. }
  182. return false;
  183. }
  184. /**
  185. * Clear all options
  186. *
  187. * @return \Zend\Form\Element\Multi
  188. */
  189. public function clearMultiOptions()
  190. {
  191. $this->options = array();
  192. $this->_translated = array();
  193. return $this;
  194. }
  195. /**
  196. * Set flag indicating whether or not to auto-register inArray validator
  197. *
  198. * @param bool $flag
  199. * @return \Zend\Form\Element\Multi
  200. */
  201. public function setRegisterInArrayValidator($flag)
  202. {
  203. $this->_registerInArrayValidator = (bool) $flag;
  204. return $this;
  205. }
  206. /**
  207. * Get status of auto-register inArray validator flag
  208. *
  209. * @return bool
  210. */
  211. public function registerInArrayValidator()
  212. {
  213. return $this->_registerInArrayValidator;
  214. }
  215. /**
  216. * Is the value provided valid?
  217. *
  218. * Autoregisters InArray validator if necessary.
  219. *
  220. * @param string $value
  221. * @param mixed $context
  222. * @return bool
  223. */
  224. public function isValid($value, $context = null)
  225. {
  226. if ($this->registerInArrayValidator()) {
  227. if (!$this->getValidator('InArray')) {
  228. $multiOptions = $this->getMultiOptions();
  229. $options = array();
  230. foreach ($multiOptions as $opt_value => $opt_label) {
  231. // optgroup instead of option label
  232. if (is_array($opt_label)) {
  233. $options = array_merge($options, array_keys($opt_label));
  234. }
  235. else {
  236. $options[] = $opt_value;
  237. }
  238. }
  239. $this->addValidator(
  240. 'InArray',
  241. true,
  242. array($options)
  243. );
  244. }
  245. }
  246. return parent::isValid($value, $context);
  247. }
  248. /**
  249. * Translate an option
  250. *
  251. * @param string $option
  252. * @param string $value
  253. * @return bool
  254. */
  255. protected function _translateOption($option, $value)
  256. {
  257. if ($this->translatorIsDisabled()) {
  258. return false;
  259. }
  260. if (!isset($this->_translated[$option]) && !empty($value)) {
  261. $this->options[$option] = $this->_translateValue($value);
  262. if ($this->options[$option] === $value) {
  263. return false;
  264. }
  265. $this->_translated[$option] = true;
  266. return true;
  267. }
  268. return false;
  269. }
  270. /**
  271. * Translate a multi option value
  272. *
  273. * @param string $value
  274. * @return string
  275. */
  276. protected function _translateValue($value)
  277. {
  278. if (is_array($value)) {
  279. foreach ($value as $key => $val) {
  280. $value[$key] = $this->_translateValue($val);
  281. }
  282. return $value;
  283. } else {
  284. if (null !== ($translator = $this->getTranslator())) {
  285. return $translator->translate($value);
  286. }
  287. return $value;
  288. }
  289. }
  290. }