bytic /
http
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace Nip\Http\Response; |
||||||
| 4 | |||||||
| 5 | use Nyholm\Psr7\Stream; |
||||||
| 6 | use Psr\Http\Message\StreamInterface; |
||||||
| 7 | |||||||
| 8 | /** |
||||||
| 9 | * Class PsrBridgeTrait |
||||||
| 10 | * Add methods to make Symfony Request compliant with PSR |
||||||
| 11 | * |
||||||
| 12 | * @package Nip\Http\Response |
||||||
| 13 | */ |
||||||
| 14 | trait PsrBridgeTrait |
||||||
| 15 | { |
||||||
| 16 | |||||||
| 17 | /** @var StreamInterface|null */ |
||||||
| 18 | protected $stream; |
||||||
| 19 | |||||||
| 20 | /** |
||||||
| 21 | * Return an instance with the specified HTTP protocol version. |
||||||
| 22 | * |
||||||
| 23 | * The version string MUST contain only the HTTP version number (e.g., |
||||||
| 24 | * "1.1", "1.0"). |
||||||
| 25 | * |
||||||
| 26 | * This method MUST be implemented in such a way as to retain the |
||||||
| 27 | * immutability of the message, and MUST return an instance that has the |
||||||
| 28 | * new protocol version. |
||||||
| 29 | * |
||||||
| 30 | * @param string $version HTTP protocol version |
||||||
| 31 | * @return static |
||||||
| 32 | */ |
||||||
| 33 | public function withProtocolVersion($version) |
||||||
|
0 ignored issues
–
show
|
|||||||
| 34 | { |
||||||
| 35 | } |
||||||
| 36 | |||||||
| 37 | /** |
||||||
| 38 | * Retrieves all message header values. |
||||||
| 39 | * |
||||||
| 40 | * The keys represent the header name as it will be sent over the wire, and |
||||||
| 41 | * each value is an array of strings associated with the header. |
||||||
| 42 | * |
||||||
| 43 | * // Represent the headers as a string |
||||||
| 44 | * foreach ($message->getHeaders() as $name => $values) { |
||||||
| 45 | * echo $name . ": " . implode(", ", $values); |
||||||
| 46 | * } |
||||||
| 47 | * |
||||||
| 48 | * // Emit headers iteratively: |
||||||
| 49 | * foreach ($message->getHeaders() as $name => $values) { |
||||||
| 50 | * foreach ($values as $value) { |
||||||
| 51 | * header(sprintf('%s: %s', $name, $value), false); |
||||||
| 52 | * } |
||||||
| 53 | * } |
||||||
| 54 | * |
||||||
| 55 | * While header names are not case-sensitive, getHeaders() will preserve the |
||||||
| 56 | * exact case in which headers were originally specified. |
||||||
| 57 | * |
||||||
| 58 | * @return string[][] Returns an associative array of the message's headers. Each |
||||||
| 59 | * key MUST be a header name, and each value MUST be an array of strings |
||||||
| 60 | * for that header. |
||||||
| 61 | */ |
||||||
| 62 | public function getHeaders() |
||||||
| 63 | { |
||||||
| 64 | } |
||||||
| 65 | |||||||
| 66 | /** |
||||||
| 67 | * Checks if a header exists by the given case-insensitive name. |
||||||
| 68 | * |
||||||
| 69 | * @param string $name Case-insensitive header field name. |
||||||
| 70 | * @return boolean|null Returns true if any header names match the given header |
||||||
| 71 | * name using a case-insensitive string comparison. Returns false if |
||||||
| 72 | * no matching header name is found in the message. |
||||||
| 73 | */ |
||||||
| 74 | public function hasHeader($name) |
||||||
| 75 | { |
||||||
| 76 | return $this->headers->has($name); |
||||||
| 77 | } |
||||||
| 78 | |||||||
| 79 | /** |
||||||
| 80 | * Retrieves a message header value by the given case-insensitive name. |
||||||
| 81 | * |
||||||
| 82 | * This method returns an array of all the header values of the given |
||||||
| 83 | * case-insensitive header name. |
||||||
| 84 | * |
||||||
| 85 | * If the header does not appear in the message, this method MUST return an |
||||||
| 86 | * empty array. |
||||||
| 87 | * |
||||||
| 88 | * @param string $name Case-insensitive header field name. |
||||||
| 89 | * @return string[] An array of string values as provided for the given |
||||||
| 90 | * header. If the header does not appear in the message, this method MUST |
||||||
| 91 | * return an empty array. |
||||||
| 92 | */ |
||||||
| 93 | public function getHeader($name) |
||||||
| 94 | { |
||||||
| 95 | return $this->headers->get($name); |
||||||
| 96 | } |
||||||
| 97 | |||||||
| 98 | /** |
||||||
| 99 | * Retrieves a comma-separated string of the values for a single header. |
||||||
| 100 | * |
||||||
| 101 | * This method returns all of the header values of the given |
||||||
| 102 | * case-insensitive header name as a string concatenated together using |
||||||
| 103 | * a comma. |
||||||
| 104 | * |
||||||
| 105 | * NOTE: Not all header values may be appropriately represented using |
||||||
| 106 | * comma concatenation. For such headers, use getHeader() instead |
||||||
| 107 | * and supply your own delimiter when concatenating. |
||||||
| 108 | * |
||||||
| 109 | * If the header does not appear in the message, this method MUST return |
||||||
| 110 | * an empty string. |
||||||
| 111 | * |
||||||
| 112 | * @param string $name Case-insensitive header field name. |
||||||
| 113 | * @return string A string of values as provided for the given header |
||||||
| 114 | * concatenated together using a comma. If the header does not appear in |
||||||
| 115 | * the message, this method MUST return an empty string. |
||||||
| 116 | */ |
||||||
| 117 | public function getHeaderLine($name) |
||||||
|
0 ignored issues
–
show
The parameter
$name is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||||
| 118 | { |
||||||
| 119 | } |
||||||
| 120 | |||||||
| 121 | /** |
||||||
| 122 | * Return an instance with the provided value replacing the specified header. |
||||||
| 123 | * |
||||||
| 124 | * While header names are case-insensitive, the casing of the header will |
||||||
| 125 | * be preserved by this function, and returned from getHeaders(). |
||||||
| 126 | * |
||||||
| 127 | * This method MUST be implemented in such a way as to retain the |
||||||
| 128 | * immutability of the message, and MUST return an instance that has the |
||||||
| 129 | * new and/or updated header and value. |
||||||
| 130 | * |
||||||
| 131 | * @param string $name Case-insensitive header field name. |
||||||
| 132 | * @param string|string[] $value Header value(s). |
||||||
| 133 | * @return static |
||||||
| 134 | * @throws \InvalidArgumentException for invalid header names or values. |
||||||
| 135 | */ |
||||||
| 136 | public function withHeader($name, $value) |
||||||
| 137 | { |
||||||
| 138 | $new = clone $this; |
||||||
| 139 | $new->headers->set($name, $value); |
||||||
| 140 | return $new; |
||||||
| 141 | } |
||||||
| 142 | |||||||
| 143 | /** |
||||||
| 144 | * Return an instance with the specified header appended with the given value. |
||||||
| 145 | * |
||||||
| 146 | * Existing values for the specified header will be maintained. The new |
||||||
| 147 | * value(s) will be appended to the existing list. If the header did not |
||||||
| 148 | * exist previously, it will be added. |
||||||
| 149 | * |
||||||
| 150 | * This method MUST be implemented in such a way as to retain the |
||||||
| 151 | * immutability of the message, and MUST return an instance that has the |
||||||
| 152 | * new header and/or value. |
||||||
| 153 | * |
||||||
| 154 | * @param string $name Case-insensitive header field name to add. |
||||||
| 155 | * @param string|string[] $value Header value(s). |
||||||
| 156 | * @return static |
||||||
| 157 | * @throws \InvalidArgumentException for invalid header names or values. |
||||||
| 158 | */ |
||||||
| 159 | public function withAddedHeader($name, $value) |
||||||
|
0 ignored issues
–
show
The parameter
$name is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$value is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||||
| 160 | { |
||||||
| 161 | } |
||||||
| 162 | |||||||
| 163 | /** |
||||||
| 164 | * Return an instance without the specified header. |
||||||
| 165 | * |
||||||
| 166 | * Header resolution MUST be done without case-sensitivity. |
||||||
| 167 | * |
||||||
| 168 | * This method MUST be implemented in such a way as to retain the |
||||||
| 169 | * immutability of the message, and MUST return an instance that removes |
||||||
| 170 | * the named header. |
||||||
| 171 | * |
||||||
| 172 | * @param string $name Case-insensitive header field name to remove. |
||||||
| 173 | * @return static |
||||||
| 174 | */ |
||||||
| 175 | public function withoutHeader($name) |
||||||
|
0 ignored issues
–
show
The parameter
$name is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||||
| 176 | { |
||||||
| 177 | } |
||||||
| 178 | |||||||
| 179 | |||||||
| 180 | public function getBody(): StreamInterface |
||||||
| 181 | { |
||||||
| 182 | if (null === $this->stream) { |
||||||
| 183 | $this->stream = Stream::create(''); |
||||||
| 184 | } |
||||||
| 185 | |||||||
| 186 | return $this->stream; |
||||||
| 187 | } |
||||||
| 188 | |||||||
| 189 | /** |
||||||
| 190 | * Return an instance with the specified message body. |
||||||
| 191 | * |
||||||
| 192 | * The body MUST be a StreamInterface object. |
||||||
| 193 | * |
||||||
| 194 | * This method MUST be implemented in such a way as to retain the |
||||||
| 195 | * immutability of the message, and MUST return a new instance that has the |
||||||
| 196 | * new body stream. |
||||||
| 197 | * |
||||||
| 198 | * @param StreamInterface $body Body. |
||||||
| 199 | * @return static |
||||||
| 200 | * @throws \InvalidArgumentException When the body is not valid. |
||||||
| 201 | */ |
||||||
| 202 | public function withBody(StreamInterface $body) |
||||||
| 203 | { |
||||||
| 204 | if ($body === $this->stream) { |
||||||
| 205 | return $this; |
||||||
| 206 | } |
||||||
| 207 | |||||||
| 208 | $new = clone $this; |
||||||
| 209 | $new->stream = $body; |
||||||
| 210 | |||||||
| 211 | return $new; |
||||||
| 212 | } |
||||||
| 213 | |||||||
| 214 | /** |
||||||
| 215 | * Return an instance with the specified status code and, optionally, reason phrase. |
||||||
| 216 | * |
||||||
| 217 | * If no reason phrase is specified, implementations MAY choose to default |
||||||
| 218 | * to the RFC 7231 or IANA recommended reason phrase for the response's |
||||||
| 219 | * status code. |
||||||
| 220 | * |
||||||
| 221 | * This method MUST be implemented in such a way as to retain the |
||||||
| 222 | * immutability of the message, and MUST return an instance that has the |
||||||
| 223 | * updated status and reason phrase. |
||||||
| 224 | * |
||||||
| 225 | * @link http://tools.ietf.org/html/rfc7231#section-6 |
||||||
| 226 | * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
||||||
| 227 | * @param int $code The 3-digit integer result code to set. |
||||||
| 228 | * @param string $reasonPhrase The reason phrase to use with the |
||||||
| 229 | * provided status code; if none is provided, implementations MAY |
||||||
| 230 | * use the defaults as suggested in the HTTP specification. |
||||||
| 231 | * @return static |
||||||
| 232 | * @throws \InvalidArgumentException For invalid status code arguments. |
||||||
| 233 | */ |
||||||
| 234 | public function withStatus($code, $reasonPhrase = '') |
||||||
| 235 | { |
||||||
| 236 | if (!\is_int($code) && !\is_string($code)) { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 237 | throw new \InvalidArgumentException('Status code has to be an integer'); |
||||||
| 238 | } |
||||||
| 239 | |||||||
| 240 | $code = (int) $code; |
||||||
| 241 | if ($code < 100 || $code > 599) { |
||||||
| 242 | throw new \InvalidArgumentException('Status code has to be an integer between 100 and 599'); |
||||||
| 243 | } |
||||||
| 244 | |||||||
| 245 | $new = clone $this; |
||||||
| 246 | $new->setStatusCode($code, $reasonPhrase); |
||||||
|
0 ignored issues
–
show
It seems like
setStatusCode() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 247 | |||||||
| 248 | return $new; |
||||||
| 249 | } |
||||||
| 250 | |||||||
| 251 | /** |
||||||
| 252 | * Gets the response reason phrase associated with the status code. |
||||||
| 253 | * |
||||||
| 254 | * Because a reason phrase is not a required element in a response |
||||||
| 255 | * status line, the reason phrase value MAY be null. Implementations MAY |
||||||
| 256 | * choose to return the default RFC 7231 recommended reason phrase (or those |
||||||
| 257 | * listed in the IANA HTTP Status Code Registry) for the response's |
||||||
| 258 | * status code. |
||||||
| 259 | * |
||||||
| 260 | * @link http://tools.ietf.org/html/rfc7231#section-6 |
||||||
| 261 | * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
||||||
| 262 | * @return string Reason phrase; must return an empty string if none present. |
||||||
| 263 | */ |
||||||
| 264 | public function getReasonPhrase() |
||||||
| 265 | { |
||||||
| 266 | } |
||||||
| 267 | |||||||
| 268 | /** |
||||||
| 269 | * @inheritDoc |
||||||
| 270 | */ |
||||||
| 271 | public function setContent(?string $content): static |
||||||
| 272 | { |
||||||
| 273 | parent::setContent($content); |
||||||
| 274 | |||||||
| 275 | $this->stream = Stream::create($this->getContent()); |
||||||
|
0 ignored issues
–
show
It seems like
getContent() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 276 | |||||||
| 277 | return $this; |
||||||
| 278 | } |
||||||
| 279 | } |
||||||
| 280 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.