comparison vendor/zendframework/zend-feed/src/Reader/Http/Response.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
4 *
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
8 */
9
10 namespace Zend\Feed\Reader\Http;
11
12 use Zend\Feed\Reader\Exception;
13
14 class Response implements HeaderAwareResponseInterface
15 {
16 /**
17 * @var string
18 */
19 private $body;
20
21 /**
22 * @var array
23 */
24 private $headers;
25
26 /**
27 * @var int
28 */
29 private $statusCode;
30
31 /**
32 * @param int $statusCode
33 * @param string|object $body
34 * @param array $headers
35 * @throws Exception\InvalidArgumentException
36 */
37 public function __construct($statusCode, $body = '', array $headers = [])
38 {
39 $this->validateStatusCode($statusCode);
40 $this->validateBody($body);
41 $this->validateHeaders($headers);
42
43 $this->statusCode = (int) $statusCode;
44 $this->body = (string) $body;
45 $this->headers = $this->normalizeHeaders($headers);
46 }
47
48 /**
49 * {@inheritDoc}
50 */
51 public function getStatusCode()
52 {
53 return $this->statusCode;
54 }
55
56 /**
57 * {@inheritDoc}
58 */
59 public function getBody()
60 {
61 return $this->body;
62 }
63
64 /**
65 * {@inheritDoc}
66 */
67 public function getHeaderLine($name, $default = null)
68 {
69 $normalizedName = strtolower($name);
70 return isset($this->headers[$normalizedName])
71 ? $this->headers[$normalizedName]
72 : $default;
73 }
74
75 /**
76 * Validate that we have a status code argument that will work for our context.
77 *
78 * @param mixed $body
79 * @throws Exception\InvalidArgumentException for arguments not castable
80 * to integer HTTP status codes.
81 */
82 private function validateStatusCode($statusCode)
83 {
84 if (! is_numeric($statusCode)) {
85 throw new Exception\InvalidArgumentException(sprintf(
86 '%s expects a numeric status code; received %s',
87 __CLASS__,
88 (is_object($statusCode) ? get_class($statusCode) : gettype($statusCode))
89 ));
90 }
91
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',
95 __CLASS__,
96 $statusCode
97 ));
98 }
99
100 if (intval($statusCode) != $statusCode) {
101 throw new Exception\InvalidArgumentException(sprintf(
102 '%s expects an integer status code; received %s',
103 __CLASS__,
104 $statusCode
105 ));
106 }
107 }
108
109 /**
110 * Validate that we have a body argument that will work for our context.
111 *
112 * @param mixed $body
113 * @throws Exception\InvalidArgumentException for arguments not castable
114 * to strings.
115 */
116 private function validateBody($body)
117 {
118 if (is_string($body)) {
119 return;
120 }
121
122 if (is_object($body) && method_exists($body, '__toString')) {
123 return;
124 }
125
126 throw new Exception\InvalidArgumentException(sprintf(
127 '%s expects a string body, or an object that can cast to string; received %s',
128 __CLASS__,
129 (is_object($body) ? get_class($body) : gettype($body))
130 ));
131 }
132
133 /**
134 * Validate header values.
135 *
136 * @param array $headers
137 * @throws Exception\InvalidArgumentException
138 */
139 private function validateHeaders(array $headers)
140 {
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',
145 __CLASS__,
146 $name
147 ));
148 }
149
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',
153 __CLASS__,
154 (is_object($value) ? get_class($value) : gettype($value)),
155 $name
156 ));
157 }
158 }
159 }
160
161 /**
162 * Normalize header names to lowercase.
163 *
164 * @param array $headers
165 * @return array
166 */
167 private function normalizeHeaders(array $headers)
168 {
169 $normalized = [];
170 foreach ($headers as $name => $value) {
171 $normalized[strtolower($name)] = $value;
172 }
173 return $normalized;
174 }
175 }