File indexing completed on 2025-01-19 05:21:35

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_Tool
0017  * @subpackage Framework
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  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @category   Zend
0025  * @package    Zend_Tool
0026  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0027  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0028  */
0029 class Zend_Tool_Framework_Client_Response
0030 {
0031     /**
0032      * @var callback|null
0033      */
0034     protected $_callback = null;
0035 
0036     /**
0037      * @var array
0038      */
0039     protected $_content = array();
0040 
0041     /**
0042      * @var Zend_Tool_Framework_Exception
0043      */
0044     protected $_exception = null;
0045 
0046     /**
0047      * @var array
0048      */
0049     protected $_decorators = array();
0050 
0051     /**
0052      * @var array
0053      */
0054     protected $_defaultDecoratorOptions = array();
0055 
0056     /**
0057      * setContentCallback()
0058      *
0059      * @param callback $callback
0060      * @return Zend_Tool_Framework_Client_Response
0061      */
0062     public function setContentCallback($callback)
0063     {
0064         if (!is_callable($callback)) {
0065             // require_once 'Zend/Tool/Framework/Client/Exception.php';
0066             throw new Zend_Tool_Framework_Client_Exception('The callback provided is not callable');
0067         }
0068         $this->_callback = $callback;
0069         return $this;
0070     }
0071 
0072     /**
0073      * setContent()
0074      *
0075      * @param string $content
0076      * @return Zend_Tool_Framework_Client_Response
0077      */
0078     public function setContent($content, Array $decoratorOptions = array())
0079     {
0080         $content = $this->_applyDecorators($content, $decoratorOptions);
0081 
0082         $this->_content = array();
0083         $this->appendContent($content);
0084         return $this;
0085     }
0086 
0087     /**
0088      * appendCallback
0089      *
0090      * @param string $content
0091      * @return Zend_Tool_Framework_Client_Response
0092      */
0093     public function appendContent($content, Array $decoratorOptions = array())
0094     {
0095         $content = $this->_applyDecorators($content, $decoratorOptions);
0096 
0097         if ($this->_callback !== null) {
0098             call_user_func($this->_callback, $content);
0099         }
0100 
0101         $this->_content[] = $content;
0102 
0103         return $this;
0104     }
0105 
0106     /**
0107      * setDefaultDecoratorOptions()
0108      *
0109      * @param array $decoratorOptions
0110      * @param bool $mergeIntoExisting
0111      * @return Zend_Tool_Framework_Client_Response
0112      */
0113     public function setDefaultDecoratorOptions(Array $decoratorOptions, $mergeIntoExisting = false)
0114     {
0115         if ($mergeIntoExisting == false) {
0116             $this->_defaultDecoratorOptions = array();
0117         }
0118 
0119         $this->_defaultDecoratorOptions = array_merge($this->_defaultDecoratorOptions, $decoratorOptions);
0120         return $this;
0121     }
0122 
0123     /**
0124      * getContent()
0125      *
0126      * @return string
0127      */
0128     public function getContent()
0129     {
0130         return implode('', $this->_content);
0131     }
0132 
0133     /**
0134      * isException()
0135      *
0136      * @return bool
0137      */
0138     public function isException()
0139     {
0140         return isset($this->_exception);
0141     }
0142 
0143     /**
0144      * setException()
0145      *
0146      * @param Exception $exception
0147      * @return Zend_Tool_Framework_Client_Response
0148      */
0149     public function setException(Exception $exception)
0150     {
0151         $this->_exception = $exception;
0152         return $this;
0153     }
0154 
0155     /**
0156      * getException()
0157      *
0158      * @return Exception
0159      */
0160     public function getException()
0161     {
0162         return $this->_exception;
0163     }
0164 
0165     /**
0166      * Add Content Decorator
0167      *
0168      * @param Zend_Tool_Framework_Client_Response_ContentDecorator_Interface $contentDecorator
0169      * @return unknown
0170      */
0171     public function addContentDecorator(Zend_Tool_Framework_Client_Response_ContentDecorator_Interface $contentDecorator)
0172     {
0173         $decoratorName = strtolower($contentDecorator->getName());
0174         $this->_decorators[$decoratorName] = $contentDecorator;
0175         return $this;
0176     }
0177 
0178     /**
0179      * getContentDecorators()
0180      *
0181      * @return array
0182      */
0183     public function getContentDecorators()
0184     {
0185         return $this->_decorators;
0186     }
0187 
0188     /**
0189      * __toString() to cast to a string
0190      *
0191      * @return string
0192      */
0193     public function __toString()
0194     {
0195         return (string) implode('', $this->_content);
0196     }
0197 
0198     /**
0199      * _applyDecorators() apply a group of decorators
0200      *
0201      * @param string $content
0202      * @param array $decoratorOptions
0203      * @return string
0204      */
0205     protected function _applyDecorators($content, Array $decoratorOptions)
0206     {
0207         $options = array_merge($this->_defaultDecoratorOptions, $decoratorOptions);
0208 
0209         $options = array_change_key_case($options, CASE_LOWER);
0210 
0211         if ($options) {
0212             foreach ($this->_decorators as $decoratorName => $decorator) {
0213                 if (array_key_exists($decoratorName, $options)) {
0214                     $content = $decorator->decorate($content, $options[$decoratorName]);
0215                 }
0216             }
0217         }
0218 
0219         return $content;
0220 
0221     }
0222 
0223 }