comparison vendor/zendframework/zend-diactoros/src/Response.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents 5311817fb629
children
comparison
equal deleted inserted replaced
3:307d7a7fd348 4:a9cd425dd02b
1 <?php 1 <?php
2 /** 2 /**
3 * @see https://github.com/zendframework/zend-diactoros for the canonical source repository 3 * @see https://github.com/zendframework/zend-diactoros for the canonical source repository
4 * @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com) 4 * @copyright Copyright (c) 2015-2018 Zend Technologies USA Inc. (http://www.zend.com)
5 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License 5 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
6 */ 6 */
7 7
8 namespace Zend\Diactoros; 8 namespace Zend\Diactoros;
9 9
111 ]; 111 ];
112 112
113 /** 113 /**
114 * @var string 114 * @var string
115 */ 115 */
116 private $reasonPhrase = ''; 116 private $reasonPhrase;
117 117
118 /** 118 /**
119 * @var int 119 * @var int
120 */ 120 */
121 private $statusCode; 121 private $statusCode;
144 /** 144 /**
145 * {@inheritdoc} 145 * {@inheritdoc}
146 */ 146 */
147 public function getReasonPhrase() 147 public function getReasonPhrase()
148 { 148 {
149 if (! $this->reasonPhrase
150 && isset($this->phrases[$this->statusCode])
151 ) {
152 $this->reasonPhrase = $this->phrases[$this->statusCode];
153 }
154
155 return $this->reasonPhrase; 149 return $this->reasonPhrase;
156 } 150 }
157 151
158 /** 152 /**
159 * {@inheritdoc} 153 * {@inheritdoc}
160 */ 154 */
161 public function withStatus($code, $reasonPhrase = '') 155 public function withStatus($code, $reasonPhrase = '')
162 { 156 {
163 $new = clone $this; 157 $new = clone $this;
164 $new->setStatusCode($code); 158 $new->setStatusCode($code, $reasonPhrase);
165 $new->reasonPhrase = $reasonPhrase;
166 return $new; 159 return $new;
167 } 160 }
168 161
169 /** 162 /**
170 * Set a valid status code. 163 * Set a valid status code.
171 * 164 *
172 * @param int $code 165 * @param int $code
166 * @param string $reasonPhrase
173 * @throws InvalidArgumentException on an invalid status code. 167 * @throws InvalidArgumentException on an invalid status code.
174 */ 168 */
175 private function setStatusCode($code) 169 private function setStatusCode($code, $reasonPhrase = '')
176 { 170 {
177 if (! is_numeric($code) 171 if (! is_numeric($code)
178 || is_float($code) 172 || is_float($code)
179 || $code < static::MIN_STATUS_CODE_VALUE 173 || $code < static::MIN_STATUS_CODE_VALUE
180 || $code > static::MAX_STATUS_CODE_VALUE 174 || $code > static::MAX_STATUS_CODE_VALUE
181 ) { 175 ) {
182 throw new InvalidArgumentException(sprintf( 176 throw new InvalidArgumentException(sprintf(
183 'Invalid status code "%s"; must be an integer between %d and %d, inclusive', 177 'Invalid status code "%s"; must be an integer between %d and %d, inclusive',
184 (is_scalar($code) ? $code : gettype($code)), 178 is_scalar($code) ? $code : gettype($code),
185 static::MIN_STATUS_CODE_VALUE, 179 static::MIN_STATUS_CODE_VALUE,
186 static::MAX_STATUS_CODE_VALUE 180 static::MAX_STATUS_CODE_VALUE
187 )); 181 ));
188 } 182 }
189 $this->statusCode = $code; 183
184 if (! is_string($reasonPhrase)) {
185 throw new InvalidArgumentException(sprintf(
186 'Unsupported response reason phrase; must be a string, received %s',
187 is_object($reasonPhrase) ? get_class($reasonPhrase) : gettype($reasonPhrase)
188 ));
189 }
190
191 if ($reasonPhrase === '' && isset($this->phrases[$code])) {
192 $reasonPhrase = $this->phrases[$code];
193 }
194
195 $this->reasonPhrase = $reasonPhrase;
196 $this->statusCode = (int) $code;
190 } 197 }
191 } 198 }