File indexing completed on 2024-12-22 05:36:41

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Form
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  */
0020 
0021 /** Zend_Form_Decorator_Interface */
0022 // require_once 'Zend/Form/Decorator/Interface.php';
0023 
0024 /**
0025  * Zend_Form_Decorator_Abstract
0026  *
0027  * @category   Zend
0028  * @package    Zend_Form
0029  * @subpackage Decorator
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  * @version    $Id$
0033  */
0034 abstract class Zend_Form_Decorator_Abstract implements Zend_Form_Decorator_Interface
0035 {
0036     /**
0037      * Placement constants
0038      */
0039     const APPEND  = 'APPEND';
0040     const PREPEND = 'PREPEND';
0041 
0042     /**
0043      * Default placement: append
0044      * @var string
0045      */
0046     protected $_placement = 'APPEND';
0047 
0048     /**
0049      * @var Zend_Form_Element|Zend_Form
0050      */
0051     protected $_element;
0052 
0053     /**
0054      * Decorator options
0055      * @var array
0056      */
0057     protected $_options = array();
0058 
0059     /**
0060      * Separator between new content and old
0061      * @var string
0062      */
0063     protected $_separator = PHP_EOL;
0064 
0065     /**
0066      * Constructor
0067      *
0068      * @param  array|Zend_Config $options
0069      * @return void
0070      */
0071     public function __construct($options = null)
0072     {
0073         if (is_array($options)) {
0074             $this->setOptions($options);
0075         } elseif ($options instanceof Zend_Config) {
0076             $this->setConfig($options);
0077         }
0078     }
0079 
0080     /**
0081      * Set options
0082      *
0083      * @param  array $options
0084      * @return Zend_Form_Decorator_Abstract
0085      */
0086     public function setOptions(array $options)
0087     {
0088         $this->_options = $options;
0089         return $this;
0090     }
0091 
0092     /**
0093      * Set options from config object
0094      *
0095      * @param  Zend_Config $config
0096      * @return Zend_Form_Decorator_Abstract
0097      */
0098     public function setConfig(Zend_Config $config)
0099     {
0100         return $this->setOptions($config->toArray());
0101     }
0102 
0103     /**
0104      * Set option
0105      *
0106      * @param  string $key
0107      * @param  mixed $value
0108      * @return Zend_Form_Decorator_Abstract
0109      */
0110     public function setOption($key, $value)
0111     {
0112         $this->_options[(string) $key] = $value;
0113         return $this;
0114     }
0115 
0116     /**
0117      * Get option
0118      *
0119      * @param  string $key
0120      * @return mixed
0121      */
0122     public function getOption($key)
0123     {
0124         $key = (string) $key;
0125         if (isset($this->_options[$key])) {
0126             return $this->_options[$key];
0127         }
0128 
0129         return null;
0130     }
0131 
0132     /**
0133      * Retrieve options
0134      *
0135      * @return array
0136      */
0137     public function getOptions()
0138     {
0139         return $this->_options;
0140     }
0141 
0142     /**
0143      * Remove single option
0144      *
0145      * @param mixed $key
0146      * @return void
0147      */
0148     public function removeOption($key)
0149     {
0150         if (null !== $this->getOption($key)) {
0151             unset($this->_options[$key]);
0152             return true;
0153         }
0154 
0155         return false;
0156     }
0157 
0158     /**
0159      * Clear all options
0160      *
0161      * @return Zend_Form_Decorator_Abstract
0162      */
0163     public function clearOptions()
0164     {
0165         $this->_options = array();
0166         return $this;
0167     }
0168 
0169     /**
0170      * Set current form element
0171      *
0172      * @param  Zend_Form_Element|Zend_Form $element
0173      * @return Zend_Form_Decorator_Abstract
0174      * @throws Zend_Form_Decorator_Exception on invalid element type
0175      */
0176     public function setElement($element)
0177     {
0178         if ((!$element instanceof Zend_Form_Element)
0179             && (!$element instanceof Zend_Form)
0180             && (!$element instanceof Zend_Form_DisplayGroup))
0181         {
0182             // require_once 'Zend/Form/Decorator/Exception.php';
0183             throw new Zend_Form_Decorator_Exception('Invalid element type passed to decorator');
0184         }
0185 
0186         $this->_element = $element;
0187         return $this;
0188     }
0189 
0190     /**
0191      * Retrieve current element
0192      *
0193      * @return Zend_Form_Element|Zend_Form
0194      */
0195     public function getElement()
0196     {
0197         return $this->_element;
0198     }
0199 
0200     /**
0201      * Determine if decorator should append or prepend content
0202      *
0203      * @return string
0204      */
0205     public function getPlacement()
0206     {
0207         $placement = $this->_placement;
0208         if (null !== ($placementOpt = $this->getOption('placement'))) {
0209             $placementOpt = strtoupper($placementOpt);
0210             switch ($placementOpt) {
0211                 case self::APPEND:
0212                 case self::PREPEND:
0213                     $placement = $this->_placement = $placementOpt;
0214                     break;
0215                 case false:
0216                     $placement = $this->_placement = null;
0217                     break;
0218                 default:
0219                     break;
0220             }
0221             $this->removeOption('placement');
0222         }
0223 
0224         return $placement;
0225     }
0226 
0227     /**
0228      * Retrieve separator to use between old and new content
0229      *
0230      * @return string
0231      */
0232     public function getSeparator()
0233     {
0234         $separator = $this->_separator;
0235         if (null !== ($separatorOpt = $this->getOption('separator'))) {
0236             $separator = $this->_separator = (string) $separatorOpt;
0237             $this->removeOption('separator');
0238         }
0239         return $separator;
0240     }
0241 
0242     /**
0243      * Decorate content and/or element
0244      *
0245      * @param  string $content
0246      * @return string
0247      * @throws Zend_Form_Decorator_Exception when unimplemented
0248      */
0249     public function render($content)
0250     {
0251         // require_once 'Zend/Form/Decorator/Exception.php';
0252         throw new Zend_Form_Decorator_Exception('render() not implemented');
0253     }
0254 }