/library/Zend/Tool/Framework/Client/Response.php

https://gitlab.com/fabiorf/curso-zend1-aula1 · PHP · 223 lines · 86 code · 27 blank · 110 comment · 6 complexity · 89813d56c8f20f85ff77d20e02aed387 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_Tool
  17. * @subpackage Framework
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Response.php 24593 2012-01-05 20:35:02Z matthew $
  21. */
  22. /**
  23. * @category Zend
  24. * @package Zend_Tool
  25. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  26. * @license http://framework.zend.com/license/new-bsd New BSD License
  27. */
  28. class Zend_Tool_Framework_Client_Response
  29. {
  30. /**
  31. * @var callback|null
  32. */
  33. protected $_callback = null;
  34. /**
  35. * @var array
  36. */
  37. protected $_content = array();
  38. /**
  39. * @var Zend_Tool_Framework_Exception
  40. */
  41. protected $_exception = null;
  42. /**
  43. * @var array
  44. */
  45. protected $_decorators = array();
  46. /**
  47. * @var array
  48. */
  49. protected $_defaultDecoratorOptions = array();
  50. /**
  51. * setContentCallback()
  52. *
  53. * @param callback $callback
  54. * @return Zend_Tool_Framework_Client_Response
  55. */
  56. public function setContentCallback($callback)
  57. {
  58. if (!is_callable($callback)) {
  59. require_once 'Zend/Tool/Framework/Client/Exception.php';
  60. throw new Zend_Tool_Framework_Client_Exception('The callback provided is not callable');
  61. }
  62. $this->_callback = $callback;
  63. return $this;
  64. }
  65. /**
  66. * setContent()
  67. *
  68. * @param string $content
  69. * @return Zend_Tool_Framework_Client_Response
  70. */
  71. public function setContent($content, Array $decoratorOptions = array())
  72. {
  73. $this->_applyDecorators($content, $decoratorOptions);
  74. $this->_content = array();
  75. $this->appendContent($content);
  76. return $this;
  77. }
  78. /**
  79. * appendCallback
  80. *
  81. * @param string $content
  82. * @return Zend_Tool_Framework_Client_Response
  83. */
  84. public function appendContent($content, Array $decoratorOptions = array())
  85. {
  86. $content = $this->_applyDecorators($content, $decoratorOptions);
  87. if ($this->_callback !== null) {
  88. call_user_func($this->_callback, $content);
  89. }
  90. $this->_content[] = $content;
  91. return $this;
  92. }
  93. /**
  94. * setDefaultDecoratorOptions()
  95. *
  96. * @param array $decoratorOptions
  97. * @param bool $mergeIntoExisting
  98. * @return Zend_Tool_Framework_Client_Response
  99. */
  100. public function setDefaultDecoratorOptions(Array $decoratorOptions, $mergeIntoExisting = false)
  101. {
  102. if ($mergeIntoExisting == false) {
  103. $this->_defaultDecoratorOptions = array();
  104. }
  105. $this->_defaultDecoratorOptions = array_merge($this->_defaultDecoratorOptions, $decoratorOptions);
  106. return $this;
  107. }
  108. /**
  109. * getContent()
  110. *
  111. * @return string
  112. */
  113. public function getContent()
  114. {
  115. return implode('', $this->_content);
  116. }
  117. /**
  118. * isException()
  119. *
  120. * @return bool
  121. */
  122. public function isException()
  123. {
  124. return isset($this->_exception);
  125. }
  126. /**
  127. * setException()
  128. *
  129. * @param Exception $exception
  130. * @return Zend_Tool_Framework_Client_Response
  131. */
  132. public function setException(Exception $exception)
  133. {
  134. $this->_exception = $exception;
  135. return $this;
  136. }
  137. /**
  138. * getException()
  139. *
  140. * @return Exception
  141. */
  142. public function getException()
  143. {
  144. return $this->_exception;
  145. }
  146. /**
  147. * Add Content Decorator
  148. *
  149. * @param Zend_Tool_Framework_Client_Response_ContentDecorator_Interface $contentDecorator
  150. * @return unknown
  151. */
  152. public function addContentDecorator(Zend_Tool_Framework_Client_Response_ContentDecorator_Interface $contentDecorator)
  153. {
  154. $decoratorName = strtolower($contentDecorator->getName());
  155. $this->_decorators[$decoratorName] = $contentDecorator;
  156. return $this;
  157. }
  158. /**
  159. * getContentDecorators()
  160. *
  161. * @return array
  162. */
  163. public function getContentDecorators()
  164. {
  165. return $this->_decorators;
  166. }
  167. /**
  168. * __toString() to cast to a string
  169. *
  170. * @return string
  171. */
  172. public function __toString()
  173. {
  174. return (string) implode('', $this->_content);
  175. }
  176. /**
  177. * _applyDecorators() apply a group of decorators
  178. *
  179. * @param string $content
  180. * @param array $decoratorOptions
  181. * @return string
  182. */
  183. protected function _applyDecorators($content, Array $decoratorOptions)
  184. {
  185. $options = array_merge($this->_defaultDecoratorOptions, $decoratorOptions);
  186. $options = array_change_key_case($options, CASE_LOWER);
  187. if ($options) {
  188. foreach ($this->_decorators as $decoratorName => $decorator) {
  189. if (array_key_exists($decoratorName, $options)) {
  190. $content = $decorator->decorate($content, $options[$decoratorName]);
  191. }
  192. }
  193. }
  194. return $content;
  195. }
  196. }