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