PageRenderTime 29ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Form/Decorator/Abstract.php

https://bitbucket.org/acidel/buykoala
PHP | 254 lines | 105 code | 23 blank | 126 comment | 9 complexity | 40b193408b5f2e6dc50a3497cf0016ff 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. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /** Zend_Form_Decorator_Interface */
  21. #require_once 'Zend/Form/Decorator/Interface.php';
  22. /**
  23. * Zend_Form_Decorator_Abstract
  24. *
  25. * @category Zend
  26. * @package Zend_Form
  27. * @subpackage Decorator
  28. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. * @version $Id: Abstract.php 21146 2010-02-23 14:35:57Z yoshida@zend.co.jp $
  31. */
  32. abstract class Zend_Form_Decorator_Abstract implements Zend_Form_Decorator_Interface
  33. {
  34. /**
  35. * Placement constants
  36. */
  37. const APPEND = 'APPEND';
  38. const PREPEND = 'PREPEND';
  39. /**
  40. * Default placement: append
  41. * @var string
  42. */
  43. protected $_placement = 'APPEND';
  44. /**
  45. * @var Zend_Form_Element|Zend_Form
  46. */
  47. protected $_element;
  48. /**
  49. * Decorator options
  50. * @var array
  51. */
  52. protected $_options = array();
  53. /**
  54. * Separator between new content and old
  55. * @var string
  56. */
  57. protected $_separator = PHP_EOL;
  58. /**
  59. * Constructor
  60. *
  61. * @param array|Zend_Config $options
  62. * @return void
  63. */
  64. public function __construct($options = null)
  65. {
  66. if (is_array($options)) {
  67. $this->setOptions($options);
  68. } elseif ($options instanceof Zend_Config) {
  69. $this->setConfig($options);
  70. }
  71. }
  72. /**
  73. * Set options
  74. *
  75. * @param array $options
  76. * @return Zend_Form_Decorator_Abstract
  77. */
  78. public function setOptions(array $options)
  79. {
  80. $this->_options = $options;
  81. return $this;
  82. }
  83. /**
  84. * Set options from config object
  85. *
  86. * @param Zend_Config $config
  87. * @return Zend_Form_Decorator_Abstract
  88. */
  89. public function setConfig(Zend_Config $config)
  90. {
  91. return $this->setOptions($config->toArray());
  92. }
  93. /**
  94. * Set option
  95. *
  96. * @param string $key
  97. * @param mixed $value
  98. * @return Zend_Form_Decorator_Abstract
  99. */
  100. public function setOption($key, $value)
  101. {
  102. $this->_options[(string) $key] = $value;
  103. return $this;
  104. }
  105. /**
  106. * Get option
  107. *
  108. * @param string $key
  109. * @return mixed
  110. */
  111. public function getOption($key)
  112. {
  113. $key = (string) $key;
  114. if (isset($this->_options[$key])) {
  115. return $this->_options[$key];
  116. }
  117. return null;
  118. }
  119. /**
  120. * Retrieve options
  121. *
  122. * @return array
  123. */
  124. public function getOptions()
  125. {
  126. return $this->_options;
  127. }
  128. /**
  129. * Remove single option
  130. *
  131. * @param mixed $key
  132. * @return void
  133. */
  134. public function removeOption($key)
  135. {
  136. if (null !== $this->getOption($key)) {
  137. unset($this->_options[$key]);
  138. return true;
  139. }
  140. return false;
  141. }
  142. /**
  143. * Clear all options
  144. *
  145. * @return Zend_Form_Decorator_Abstract
  146. */
  147. public function clearOptions()
  148. {
  149. $this->_options = array();
  150. return $this;
  151. }
  152. /**
  153. * Set current form element
  154. *
  155. * @param Zend_Form_Element|Zend_Form $element
  156. * @return Zend_Form_Decorator_Abstract
  157. * @throws Zend_Form_Decorator_Exception on invalid element type
  158. */
  159. public function setElement($element)
  160. {
  161. if ((!$element instanceof Zend_Form_Element)
  162. && (!$element instanceof Zend_Form)
  163. && (!$element instanceof Zend_Form_DisplayGroup))
  164. {
  165. #require_once 'Zend/Form/Decorator/Exception.php';
  166. throw new Zend_Form_Decorator_Exception('Invalid element type passed to decorator');
  167. }
  168. $this->_element = $element;
  169. return $this;
  170. }
  171. /**
  172. * Retrieve current element
  173. *
  174. * @return Zend_Form_Element|Zend_Form
  175. */
  176. public function getElement()
  177. {
  178. return $this->_element;
  179. }
  180. /**
  181. * Determine if decorator should append or prepend content
  182. *
  183. * @return string
  184. */
  185. public function getPlacement()
  186. {
  187. $placement = $this->_placement;
  188. if (null !== ($placementOpt = $this->getOption('placement'))) {
  189. $placementOpt = strtoupper($placementOpt);
  190. switch ($placementOpt) {
  191. case self::APPEND:
  192. case self::PREPEND:
  193. $placement = $this->_placement = $placementOpt;
  194. break;
  195. case false:
  196. $placement = $this->_placement = null;
  197. break;
  198. default:
  199. break;
  200. }
  201. $this->removeOption('placement');
  202. }
  203. return $placement;
  204. }
  205. /**
  206. * Retrieve separator to use between old and new content
  207. *
  208. * @return string
  209. */
  210. public function getSeparator()
  211. {
  212. $separator = $this->_separator;
  213. if (null !== ($separatorOpt = $this->getOption('separator'))) {
  214. $separator = $this->_separator = (string) $separatorOpt;
  215. $this->removeOption('separator');
  216. }
  217. return $separator;
  218. }
  219. /**
  220. * Decorate content and/or element
  221. *
  222. * @param string $content
  223. * @return string
  224. * @throws Zend_Form_Decorator_Exception when unimplemented
  225. */
  226. public function render($content)
  227. {
  228. #require_once 'Zend/Form/Decorator/Exception.php';
  229. throw new Zend_Form_Decorator_Exception('render() not implemented');
  230. }
  231. }