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.
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id: Frame.php 16971 2009-07-22 18:05:45Z mikaelkael $
24 * @see Zend_Queue_Stomp_FrameInterface
26 require_once 'Zend/Queue/Stomp/FrameInterface.php';
29 * This class represents a Stomp Frame
34 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
35 * @license http://framework.zend.com/license/new-bsd New BSD License
37 class Zend_Queue_Stomp_Frame
38 implements Zend_Queue_Stomp_FrameInterface
40 const END_OF_FRAME
= "\x00\n";
41 const CONTENT_LENGTH
= 'content-length';
45 * Headers for the frame
49 protected $_headers = array();
52 * The command for the frame
56 protected $_command = null;
59 * The body of the frame
63 protected $_body = null;
66 * Do the content-length automatically?
68 protected $_autoContentLength = null;
73 public function __construct()
75 $this->setHeaders(array());
77 $this->setCommand(null);
78 $this->setAutoContentLength(true);
82 * get the status of the auto content length
84 * If AutoContentLength is true this code will automatically put the
85 * content-length header in, even if it is already set by the user.
87 * This is done to make the message sending more reliable.
91 public function getAutoContentLength()
93 return $this->_autoContentLength
;
97 * setAutoContentLength()
99 * Set the value on or off.
101 * @param boolean $auto
103 * @throws Zend_Queue_Exception
105 public function setAutoContentLength($auto)
107 if (!is_bool($auto)) {
108 require_once 'Zend/Queue/Exception.php';
109 throw new Zend_Queue_Exception('$auto is not a boolean');
112 $this->_autoContentLength
= $auto;
121 public function getHeaders()
123 return $this->_headers
;
129 * Throws an exception if the array values are not strings.
131 * @param array $headers
133 * @throws Zend_Queue_Exception
135 public function setHeaders(array $headers)
137 foreach ($headers as $header => $value) {
138 $this->setHeader($header, $value);
145 * Sets a value for a header
147 * @param string $header
148 * @param string $value
149 * @return Zend_Queue_Stomp_Frame
150 * @throws Zend_Queue_Exception
152 public function setHeader($header, $value) {
153 if (!is_string($header)) {
154 require_once 'Zend/Queue/Exception.php';
155 throw new Zend_Queue_Exception('$header is not a string: ' . print_r($header, true));
158 if (!is_scalar($value)) {
159 require_once 'Zend/Queue/Exception.php';
160 throw new Zend_Queue_Exception('$value is not a string: ' . print_r($value, true));
163 $this->_headers
[$header] = $value;
169 * Returns a value for a header
171 * Returns false if the header does not exist.
173 * @param string $header
174 * @return string|false
175 * @throws Zend_Queue_Exception
177 public function getHeader($header)
179 if (!is_string($header)) {
180 require_once 'Zend/Queue/Exception.php';
181 throw new Zend_Queue_Exception('$header is not a string');
184 return isset($this->_headers
[$header])
185 ?
$this->_headers
[$header]
190 * Return the body for this frame
192 * Returns false if the body does not exist
194 * @return false|string
196 public function getBody()
198 return is_null($this->_body
)
204 * Set the body for this frame
206 * Set to null for no body.
208 * @param string|null $body
209 * @return Zend_Queue_Stomp_Frame
210 * @throws Zend_Queue_Exception
212 public function setBody($body)
214 if (!is_string($body) && !is_null($body)) {
215 require_once 'Zend/Queue/Exception.php';
216 throw new Zend_Queue_Exception('$body is not a string or null');
219 $this->_body
= $body;
224 * Return the command for this frame
226 * Return false if the command does not exist
228 * @return string|false
230 public function getCommand()
232 return is_null($this->_command
)
238 * Set the body for this frame
241 * @return Zend_Queue_Stomp_Frame
242 * @throws Zend_Queue_Exception
244 public function setCommand($command)
246 if (!is_string($command) && !is_null($command)) {
247 require_once 'Zend/Queue/Exception.php';
248 throw new Zend_Queue_Exception('$command is not a string or null');
251 $this->_command
= $command;
256 * Takes the current parameters and returns a Stomp Frame
259 * @throws Zend_Queue_Exception
261 public function toFrame()
263 if ($this->getCommand() === false) {
264 require_once 'Zend/Queue/Exception.php';
265 throw new Zend_Queue_Exception('You must set the command');
268 $command = $this->getCommand();
269 $headers = $this->getHeaders();
270 $body = $this->getBody();
273 // add a content-length to the SEND command.
274 // @see http://stomp.codehaus.org/Protocol
275 if ($this->getAutoContentLength()) {
276 $headers[self
::CONTENT_LENGTH
] = strlen($this->getBody());
280 $frame = $command . self
::EOL
;
283 foreach ($headers as $key=>$value) {
284 $frame .= $key . ': ' . $value . self
::EOL
;
288 $frame .= self
::EOL
; // blank line required by protocol
290 // add the body if any
291 if ($body !== false) {
294 $frame .= self
::END_OF_FRAME
;
303 public function __toString()
306 $return = $this->toFrame();
307 } catch (Zend_Queue_Exception
$e) {
314 * Accepts a frame and deconstructs the frame into its component parts
316 * @param string $frame - a stomp frame
319 public function fromFrame($frame)
321 if (!is_string($frame)) {
322 require_once 'Zend/Queue/Exception.php';
323 throw new Zend_Queue_Exception('$frame is not a string');
331 // separate the headers and the body
332 $match = self
::EOL
. self
::EOL
;
333 if (preg_match('/' . $match . '/', $frame)) {
334 list ($header, $body) = explode($match, $frame, 2);
340 $headers = explode(self
::EOL
, $header);
343 // get the command (first line)
344 $this->setCommand(array_shift($headers));
346 // set each of the headers.
347 foreach ($headers as $header) {
348 if (strpos($header, ':') > 0) {
349 list($name, $value) = explode(':', $header, 2);
350 $this->setHeader($name, $value);
354 // crop the body if content-length is present
355 if ($this->getHeader(self
::CONTENT_LENGTH
) !== false ) {
356 $length = (int) $this->getHeader(self
::CONTENT_LENGTH
);
357 $body = substr($body, 0, $length);
360 $this->setBody($body);