3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Feed\PubSubHubbub;
15 * The body of any response to the current callback request
19 protected $content = '';
22 * Array of headers. Each header is an array with keys 'name' and 'value'
26 protected $headers = [];
29 * HTTP response code to use in headers
33 protected $statusCode = 200;
36 * Send the response, including all headers
40 public function send()
43 echo $this->getContent();
49 * Sends any headers specified. If an {@link setHttpResponseCode() HTTP response code}
50 * has been specified, it is sent with the first header.
54 public function sendHeaders()
56 if (count($this->headers) || (200 != $this->statusCode)) {
57 $this->canSendHeaders(true);
58 } elseif (200 == $this->statusCode) {
61 $httpCodeSent = false;
62 foreach ($this->headers as $header) {
63 if (! $httpCodeSent && $this->statusCode) {
64 header($header['name'] . ': ' . $header['value'], $header['replace'], $this->statusCode);
67 header($header['name'] . ': ' . $header['value'], $header['replace']);
70 if (! $httpCodeSent) {
71 header('HTTP/1.1 ' . $this->statusCode);
78 * If $replace is true, replaces any headers already defined with that
82 * @param string $value
83 * @param bool $replace
84 * @return \Zend\Feed\PubSubHubbub\HttpResponse
86 public function setHeader($name, $value, $replace = false)
88 $name = $this->_normalizeHeader($name);
89 $value = (string) $value;
91 foreach ($this->headers as $key => $header) {
92 if ($name == $header['name']) {
93 unset($this->headers[$key]);
100 'replace' => $replace,
107 * Check if a specific Header is set and return its value
109 * @param string $name
110 * @return string|null
112 public function getHeader($name)
114 $name = $this->_normalizeHeader($name);
115 foreach ($this->headers as $header) {
116 if ($header['name'] == $name) {
117 return $header['value'];
123 * Return array of headers; see {@link $headers} for format
127 public function getHeaders()
129 return $this->headers;
133 * Can we send headers?
135 * @param bool $throw Whether or not to throw an exception if headers have been sent; defaults to false
136 * @return HttpResponse
137 * @throws Exception\RuntimeException
139 public function canSendHeaders($throw = false)
141 $ok = headers_sent($file, $line);
143 throw new Exception\RuntimeException(
144 'Cannot send headers; headers already sent in ' . $file . ', line ' . $line
151 * Set HTTP response code to use with headers
154 * @return HttpResponse
155 * @throws Exception\InvalidArgumentException
157 public function setStatusCode($code)
159 if (! is_int($code) || (100 > $code) || (599 < $code)) {
160 throw new Exception\InvalidArgumentException('Invalid HTTP response'
163 $this->statusCode = $code;
168 * Retrieve HTTP response code
172 public function getStatusCode()
174 return $this->statusCode;
180 * @param string $content
181 * @return \Zend\Feed\PubSubHubbub\HttpResponse
183 public function setContent($content)
185 $this->content = (string) $content;
186 $this->setHeader('content-length', strlen($content));
191 * Return the body content
195 public function getContent()
197 return $this->content;
201 * Normalizes a header name to X-Capitalized-Names
203 * @param string $name
206 // @codingStandardsIgnoreStart
207 protected function _normalizeHeader($name)
209 // @codingStandardsIgnoreEnd
210 $filtered = str_replace(['-', '_'], ' ', (string) $name);
211 $filtered = ucwords(strtolower($filtered));
212 $filtered = str_replace(' ', '-', $filtered);