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  * @subpackage Decorator
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  */
0021 
0022 /** Zend_Form_Decorator_Abstract */
0023 // require_once 'Zend/Form/Decorator/Abstract.php';
0024 
0025 /**
0026  * Zend_Form_Decorator_Callback
0027  *
0028  * Execute an arbitrary callback to decorate an element. Callbacks should take
0029  * three arguments, $content, $element, and $options:
0030  *
0031  * function mycallback($content, $element, array $options)
0032  * {
0033  * }
0034  *
0035  * and should return a string. ($options are whatever options were provided to
0036  * the decorator.)
0037  *
0038  * To specify a callback, pass a valid callback as the 'callback' option.
0039  *
0040  * Callback results will be either appended, prepended, or replace the provided
0041  * content. To replace the content, specify a placement of boolean false;
0042  * defaults to append content.
0043  *
0044  * @category   Zend
0045  * @package    Zend_Form
0046  * @subpackage Decorator
0047  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0048  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0049  * @version    $Id$
0050  */
0051 class Zend_Form_Decorator_Callback extends Zend_Form_Decorator_Abstract
0052 {
0053     /**
0054      * Callback
0055      * @var string|array
0056      */
0057     protected $_callback;
0058 
0059     /**
0060      * Set callback
0061      *
0062      * @param  callback $callback
0063      * @return Zend_Form_Decorator_Callback
0064      * @throws Zend_Form_Exception
0065      */
0066     public function setCallback($callback)
0067     {
0068         if (!is_callable($callback)) {
0069             // require_once 'Zend/Form/Exception.php';
0070             throw new Zend_Form_Exception('Invalid callback provided to callback decorator');
0071         }
0072         $this->_callback = $callback;
0073         return $this;
0074     }
0075 
0076     /**
0077      * Get registered callback
0078      *
0079      * If not previously registered, checks to see if it exists in registered
0080      * options.
0081      *
0082      * @return null|string|array
0083      */
0084     public function getCallback()
0085     {
0086         if (null === $this->_callback) {
0087             if (null !== ($callback = $this->getOption('callback'))) {
0088                 $this->setCallback($callback);
0089                 $this->removeOption('callback');
0090             }
0091         }
0092 
0093         return $this->_callback;
0094     }
0095 
0096     /**
0097      * Render
0098      *
0099      * If no callback registered, returns callback. Otherwise, gets return
0100      * value of callback and either appends, prepends, or replaces passed in
0101      * content.
0102      *
0103      * @param  string $content
0104      * @return string
0105      */
0106     public function render($content)
0107     {
0108         $callback = $this->getCallback();
0109         if (null === $callback) {
0110             return $content;
0111         }
0112 
0113         $placement = $this->getPlacement();
0114         $separator = $this->getSeparator();
0115 
0116         $response = call_user_func($callback, $content, $this->getElement(), $this->getOptions());
0117 
0118         switch ($placement) {
0119             case self::APPEND:
0120                 return $content . $separator . $response;
0121             case self::PREPEND:
0122                 return $response . $separator . $content;
0123             default:
0124                 // replace content
0125                 return $response;
0126         }
0127     }
0128 }