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