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\Reader\Http;
12 use Zend\Feed\Reader\Exception;
14 class Response implements HeaderAwareResponseInterface
32 * @param int $statusCode
33 * @param string|object $body
34 * @param array $headers
35 * @throws Exception\InvalidArgumentException
37 public function __construct($statusCode, $body = '', array $headers = [])
39 $this->validateStatusCode($statusCode);
40 $this->validateBody($body);
41 $this->validateHeaders($headers);
43 $this->statusCode = (int) $statusCode;
44 $this->body = (string) $body;
45 $this->headers = $this->normalizeHeaders($headers);
51 public function getStatusCode()
53 return $this->statusCode;
59 public function getBody()
67 public function getHeaderLine($name, $default = null)
69 $normalizedName = strtolower($name);
70 return isset($this->headers[$normalizedName])
71 ? $this->headers[$normalizedName]
76 * Validate that we have a status code argument that will work for our context.
79 * @throws Exception\InvalidArgumentException for arguments not castable
80 * to integer HTTP status codes.
82 private function validateStatusCode($statusCode)
84 if (! is_numeric($statusCode)) {
85 throw new Exception\InvalidArgumentException(sprintf(
86 '%s expects a numeric status code; received %s',
88 (is_object($statusCode) ? get_class($statusCode) : gettype($statusCode))
92 if (100 > $statusCode || 599 < $statusCode) {
93 throw new Exception\InvalidArgumentException(sprintf(
94 '%s expects an integer status code between 100 and 599 inclusive; received %s',
100 if (intval($statusCode) != $statusCode) {
101 throw new Exception\InvalidArgumentException(sprintf(
102 '%s expects an integer status code; received %s',
110 * Validate that we have a body argument that will work for our context.
113 * @throws Exception\InvalidArgumentException for arguments not castable
116 private function validateBody($body)
118 if (is_string($body)) {
122 if (is_object($body) && method_exists($body, '__toString')) {
126 throw new Exception\InvalidArgumentException(sprintf(
127 '%s expects a string body, or an object that can cast to string; received %s',
129 (is_object($body) ? get_class($body) : gettype($body))
134 * Validate header values.
136 * @param array $headers
137 * @throws Exception\InvalidArgumentException
139 private function validateHeaders(array $headers)
141 foreach ($headers as $name => $value) {
142 if (! is_string($name) || is_numeric($name) || empty($name)) {
143 throw new Exception\InvalidArgumentException(sprintf(
144 'Header names provided to %s must be non-empty, non-numeric strings; received %s',
150 if (! is_string($value) && ! is_numeric($value)) {
151 throw new Exception\InvalidArgumentException(sprintf(
152 'Individual header values provided to %s must be a string or numeric; received %s for header %s',
154 (is_object($value) ? get_class($value) : gettype($value)),
162 * Normalize header names to lowercase.
164 * @param array $headers
167 private function normalizeHeaders(array $headers)
170 foreach ($headers as $name => $value) {
171 $normalized[strtolower($name)] = $value;