| @@ -21,248 +21,248 @@ | ||
| 21 | 21 | */ | 
| 22 | 22 | class Message implements MessageInterface | 
| 23 | 23 |  { | 
| 24 | - const DEFAULT_VERSION = '1.1'; | |
| 25 | - const VERSION_DELIMITER = 'HTTP/'; | |
| 26 | - const HEADER_DELIMITER = ': '; | |
| 27 | - const HEADER_VALUE_DELIMITER = ','; | |
| 28 | - | |
| 29 | - /** @var string The version. */ | |
| 30 | - private $version; | |
| 31 | - | |
| 32 | - /** @var array The header names. */ | |
| 33 | - private $headerNames; | |
| 34 | - | |
| 35 | - /** @var array The headers. */ | |
| 36 | - private $headers; | |
| 37 | - | |
| 38 | - /** @var StreamInterface The body. */ | |
| 39 | - private $body; | |
| 40 | - | |
| 41 | - /** | |
| 42 | - * Construct a Message object with the given version, headers & body. | |
| 43 | - * | |
| 44 | - * @param string $version = self::DEFAULT_VERSION | |
| 45 | - * @param array $headers = [] | |
| 46 | - * @param StreamInterface|null $body = null | |
| 47 | - */ | |
| 48 | - public function __construct($version = self::DEFAULT_VERSION, array $headers = [], StreamInterface $body = null) | |
| 49 | -	{ | |
| 50 | -		if ($body === null) { | |
| 51 | -			$body = new Stream(fopen('php://temp', 'r+')); | |
| 52 | - } | |
| 53 | - | |
| 54 | - $this->setProtocolVersion($version); | |
| 55 | - $this->setHeaders($headers); | |
| 56 | - $this->setBody($body); | |
| 57 | - } | |
| 58 | - | |
| 59 | - /** | |
| 60 | -	 * {@inheritdoc} | |
| 61 | - */ | |
| 62 | - public function getProtocolVersion() | |
| 63 | -	{ | |
| 64 | - return $this->version; | |
| 65 | - } | |
| 66 | - | |
| 67 | - /** | |
| 68 | - * Set the protocol version. | |
| 69 | - * | |
| 70 | - * @param string $version | |
| 71 | - * @return $this | |
| 72 | - */ | |
| 73 | - private function setProtocolVersion($version) | |
| 74 | -	{ | |
| 75 | - $this->version = $version; | |
| 76 | - | |
| 77 | - return $this; | |
| 78 | - } | |
| 79 | - | |
| 80 | - /** | |
| 81 | -	 * {@inheritdoc} | |
| 82 | - */ | |
| 83 | - public function withProtocolVersion($version) | |
| 84 | -	{ | |
| 85 | - $result = clone $this; | |
| 86 | - | |
| 87 | - return $result->setProtocolVersion($version); | |
| 88 | - } | |
| 89 | - | |
| 90 | - /** | |
| 91 | -	 * {@inheritdoc} | |
| 92 | - */ | |
| 93 | - public function getHeaders() | |
| 94 | -	{ | |
| 95 | - return $this->headers; | |
| 96 | - } | |
| 97 | - | |
| 98 | - /** | |
| 99 | - * Set the headers. | |
| 100 | - * | |
| 101 | - * @param array $headers | |
| 102 | - * @return $this | |
| 103 | - */ | |
| 104 | - private function setHeaders(array $headers) | |
| 105 | -	{ | |
| 106 | - $this->headerNames = []; | |
| 107 | - $this->headers = []; | |
| 108 | - | |
| 109 | -		foreach ($headers as $name => $value) { | |
| 110 | - $this->setHeader($name, $value); | |
| 111 | - } | |
| 112 | - | |
| 113 | - return $this; | |
| 114 | - } | |
| 115 | - | |
| 116 | - /** | |
| 117 | -	 * {@inheritdoc} | |
| 118 | - */ | |
| 119 | - public function hasHeader($name) | |
| 120 | -	{ | |
| 121 | - return array_key_exists(strtolower($name), $this->headerNames); | |
| 122 | - } | |
| 123 | - | |
| 124 | - /** | |
| 125 | -	 * {@inheritdoc} | |
| 126 | - */ | |
| 127 | - public function getHeader($name) | |
| 128 | -	{ | |
| 129 | -		if (!$this->hasHeader($name)) { | |
| 130 | - return []; | |
| 131 | - } | |
| 132 | - | |
| 133 | - return $this->headers[$this->headerNames[strtolower($name)]]; | |
| 134 | - } | |
| 135 | - | |
| 136 | - /** | |
| 137 | -	 * {@inheritdoc} | |
| 138 | - */ | |
| 139 | - public function getHeaderLine($name) | |
| 140 | -	{ | |
| 141 | -		if (!$this->hasHeader($name)) { | |
| 142 | - return null; | |
| 143 | - } | |
| 144 | - | |
| 145 | -		return implode(',', $this->getHeader($name)); | |
| 146 | - } | |
| 147 | - | |
| 148 | - /** | |
| 149 | - * Set the header. | |
| 150 | - * | |
| 151 | - * @param string $name | |
| 152 | - * @param string|string[] $value | |
| 153 | - * @return $this | |
| 154 | - */ | |
| 155 | - protected function setHeader($name, $value) | |
| 156 | -	{ | |
| 157 | -		if (!is_array($value)) { | |
| 158 | - $value = [$value]; | |
| 159 | - } | |
| 160 | - | |
| 161 | - $this->headerNames[strtolower($name)] = $name; | |
| 162 | - array_merge($this->headers[$name] = $value); | |
| 163 | - | |
| 164 | - return $this; | |
| 165 | - } | |
| 166 | - | |
| 167 | - /** | |
| 168 | -	 * {@inheritdoc} | |
| 169 | - */ | |
| 170 | - public function withHeader($name, $value) | |
| 171 | -	{ | |
| 172 | - $result = clone $this; | |
| 173 | - | |
| 174 | - return $result->setHeader($name, $value); | |
| 175 | - } | |
| 176 | - | |
| 177 | - /** | |
| 178 | - * Add the header. | |
| 179 | - * | |
| 180 | - * @param string $name | |
| 181 | - * @param string|string[] $value | |
| 182 | - * @return $this | |
| 183 | - */ | |
| 184 | - private function addHeader($name, $value) | |
| 185 | -	{ | |
| 186 | -		if (!$this->hasHeader($name)) { | |
| 187 | - return $this->setHeader($name, $value); | |
| 188 | - } | |
| 189 | - | |
| 190 | -		if (!is_array($value)) { | |
| 191 | - $value = [$value]; | |
| 192 | - } | |
| 193 | - | |
| 194 | -		foreach ($value as $element) { | |
| 195 | - $this->headers[$this->headerNames[strtolower($name)]][] = $element; | |
| 196 | - } | |
| 197 | - | |
| 198 | - return $this; | |
| 199 | - } | |
| 200 | - | |
| 201 | - /** | |
| 202 | -	 * {@inheritdoc} | |
| 203 | - */ | |
| 204 | - public function withAddedHeader($name, $value) | |
| 205 | -	{ | |
| 206 | - $result = clone $this; | |
| 207 | - | |
| 208 | - return $result->addHeader($name, $value); | |
| 209 | - } | |
| 210 | - | |
| 211 | - /** | |
| 212 | - * Remove the header. | |
| 213 | - * | |
| 214 | - * @param string $name | |
| 215 | - * @return $this | |
| 216 | - */ | |
| 217 | - private function removeHeader($name) | |
| 218 | -	{ | |
| 219 | -		if ($this->hasHeader($name)) { | |
| 220 | - $normalized = strtolower($name); | |
| 221 | - | |
| 222 | - unset($this->headers[$this->headerNames[$normalized]], $this->headerNames[$normalized]); | |
| 223 | - } | |
| 224 | - | |
| 225 | - return $this; | |
| 226 | - } | |
| 227 | - | |
| 228 | - /** | |
| 229 | -	 * {@inheritdoc} | |
| 230 | - */ | |
| 231 | - public function withoutHeader($name) | |
| 232 | -	{ | |
| 233 | - $result = clone $this; | |
| 234 | - | |
| 235 | - return $result->removeHeader($name); | |
| 236 | - } | |
| 237 | - | |
| 238 | - /** | |
| 239 | -	 * {@inheritdoc} | |
| 240 | - */ | |
| 241 | - public function getBody() | |
| 242 | -	{ | |
| 243 | - return $this->body; | |
| 244 | - } | |
| 245 | - | |
| 246 | - /** | |
| 247 | - * Sets the body. | |
| 248 | - * | |
| 249 | - * @param StreamInterface $body | |
| 250 | - * @return $this | |
| 251 | - */ | |
| 252 | - private function setBody(StreamInterface $body) | |
| 253 | -	{ | |
| 254 | - $this->body = $body; | |
| 255 | - | |
| 256 | - return $this; | |
| 257 | - } | |
| 258 | - | |
| 259 | - /** | |
| 260 | -	 * {@inheritdoc} | |
| 261 | - */ | |
| 262 | - public function withBody(StreamInterface $body) | |
| 263 | -	{ | |
| 264 | - $result = clone $this; | |
| 265 | - | |
| 266 | - return $result->setBody($body); | |
| 267 | - } | |
| 24 | + const DEFAULT_VERSION = '1.1'; | |
| 25 | + const VERSION_DELIMITER = 'HTTP/'; | |
| 26 | + const HEADER_DELIMITER = ': '; | |
| 27 | + const HEADER_VALUE_DELIMITER = ','; | |
| 28 | + | |
| 29 | + /** @var string The version. */ | |
| 30 | + private $version; | |
| 31 | + | |
| 32 | + /** @var array The header names. */ | |
| 33 | + private $headerNames; | |
| 34 | + | |
| 35 | + /** @var array The headers. */ | |
| 36 | + private $headers; | |
| 37 | + | |
| 38 | + /** @var StreamInterface The body. */ | |
| 39 | + private $body; | |
| 40 | + | |
| 41 | + /** | |
| 42 | + * Construct a Message object with the given version, headers & body. | |
| 43 | + * | |
| 44 | + * @param string $version = self::DEFAULT_VERSION | |
| 45 | + * @param array $headers = [] | |
| 46 | + * @param StreamInterface|null $body = null | |
| 47 | + */ | |
| 48 | + public function __construct($version = self::DEFAULT_VERSION, array $headers = [], StreamInterface $body = null) | |
| 49 | +    { | |
| 50 | +        if ($body === null) { | |
| 51 | +            $body = new Stream(fopen('php://temp', 'r+')); | |
| 52 | + } | |
| 53 | + | |
| 54 | + $this->setProtocolVersion($version); | |
| 55 | + $this->setHeaders($headers); | |
| 56 | + $this->setBody($body); | |
| 57 | + } | |
| 58 | + | |
| 59 | + /** | |
| 60 | +     * {@inheritdoc} | |
| 61 | + */ | |
| 62 | + public function getProtocolVersion() | |
| 63 | +    { | |
| 64 | + return $this->version; | |
| 65 | + } | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * Set the protocol version. | |
| 69 | + * | |
| 70 | + * @param string $version | |
| 71 | + * @return $this | |
| 72 | + */ | |
| 73 | + private function setProtocolVersion($version) | |
| 74 | +    { | |
| 75 | + $this->version = $version; | |
| 76 | + | |
| 77 | + return $this; | |
| 78 | + } | |
| 79 | + | |
| 80 | + /** | |
| 81 | +     * {@inheritdoc} | |
| 82 | + */ | |
| 83 | + public function withProtocolVersion($version) | |
| 84 | +    { | |
| 85 | + $result = clone $this; | |
| 86 | + | |
| 87 | + return $result->setProtocolVersion($version); | |
| 88 | + } | |
| 89 | + | |
| 90 | + /** | |
| 91 | +     * {@inheritdoc} | |
| 92 | + */ | |
| 93 | + public function getHeaders() | |
| 94 | +    { | |
| 95 | + return $this->headers; | |
| 96 | + } | |
| 97 | + | |
| 98 | + /** | |
| 99 | + * Set the headers. | |
| 100 | + * | |
| 101 | + * @param array $headers | |
| 102 | + * @return $this | |
| 103 | + */ | |
| 104 | + private function setHeaders(array $headers) | |
| 105 | +    { | |
| 106 | + $this->headerNames = []; | |
| 107 | + $this->headers = []; | |
| 108 | + | |
| 109 | +        foreach ($headers as $name => $value) { | |
| 110 | + $this->setHeader($name, $value); | |
| 111 | + } | |
| 112 | + | |
| 113 | + return $this; | |
| 114 | + } | |
| 115 | + | |
| 116 | + /** | |
| 117 | +     * {@inheritdoc} | |
| 118 | + */ | |
| 119 | + public function hasHeader($name) | |
| 120 | +    { | |
| 121 | + return array_key_exists(strtolower($name), $this->headerNames); | |
| 122 | + } | |
| 123 | + | |
| 124 | + /** | |
| 125 | +     * {@inheritdoc} | |
| 126 | + */ | |
| 127 | + public function getHeader($name) | |
| 128 | +    { | |
| 129 | +        if (!$this->hasHeader($name)) { | |
| 130 | + return []; | |
| 131 | + } | |
| 132 | + | |
| 133 | + return $this->headers[$this->headerNames[strtolower($name)]]; | |
| 134 | + } | |
| 135 | + | |
| 136 | + /** | |
| 137 | +     * {@inheritdoc} | |
| 138 | + */ | |
| 139 | + public function getHeaderLine($name) | |
| 140 | +    { | |
| 141 | +        if (!$this->hasHeader($name)) { | |
| 142 | + return null; | |
| 143 | + } | |
| 144 | + | |
| 145 | +        return implode(',', $this->getHeader($name)); | |
| 146 | + } | |
| 147 | + | |
| 148 | + /** | |
| 149 | + * Set the header. | |
| 150 | + * | |
| 151 | + * @param string $name | |
| 152 | + * @param string|string[] $value | |
| 153 | + * @return $this | |
| 154 | + */ | |
| 155 | + protected function setHeader($name, $value) | |
| 156 | +    { | |
| 157 | +        if (!is_array($value)) { | |
| 158 | + $value = [$value]; | |
| 159 | + } | |
| 160 | + | |
| 161 | + $this->headerNames[strtolower($name)] = $name; | |
| 162 | + array_merge($this->headers[$name] = $value); | |
| 163 | + | |
| 164 | + return $this; | |
| 165 | + } | |
| 166 | + | |
| 167 | + /** | |
| 168 | +     * {@inheritdoc} | |
| 169 | + */ | |
| 170 | + public function withHeader($name, $value) | |
| 171 | +    { | |
| 172 | + $result = clone $this; | |
| 173 | + | |
| 174 | + return $result->setHeader($name, $value); | |
| 175 | + } | |
| 176 | + | |
| 177 | + /** | |
| 178 | + * Add the header. | |
| 179 | + * | |
| 180 | + * @param string $name | |
| 181 | + * @param string|string[] $value | |
| 182 | + * @return $this | |
| 183 | + */ | |
| 184 | + private function addHeader($name, $value) | |
| 185 | +    { | |
| 186 | +        if (!$this->hasHeader($name)) { | |
| 187 | + return $this->setHeader($name, $value); | |
| 188 | + } | |
| 189 | + | |
| 190 | +        if (!is_array($value)) { | |
| 191 | + $value = [$value]; | |
| 192 | + } | |
| 193 | + | |
| 194 | +        foreach ($value as $element) { | |
| 195 | + $this->headers[$this->headerNames[strtolower($name)]][] = $element; | |
| 196 | + } | |
| 197 | + | |
| 198 | + return $this; | |
| 199 | + } | |
| 200 | + | |
| 201 | + /** | |
| 202 | +     * {@inheritdoc} | |
| 203 | + */ | |
| 204 | + public function withAddedHeader($name, $value) | |
| 205 | +    { | |
| 206 | + $result = clone $this; | |
| 207 | + | |
| 208 | + return $result->addHeader($name, $value); | |
| 209 | + } | |
| 210 | + | |
| 211 | + /** | |
| 212 | + * Remove the header. | |
| 213 | + * | |
| 214 | + * @param string $name | |
| 215 | + * @return $this | |
| 216 | + */ | |
| 217 | + private function removeHeader($name) | |
| 218 | +    { | |
| 219 | +        if ($this->hasHeader($name)) { | |
| 220 | + $normalized = strtolower($name); | |
| 221 | + | |
| 222 | + unset($this->headers[$this->headerNames[$normalized]], $this->headerNames[$normalized]); | |
| 223 | + } | |
| 224 | + | |
| 225 | + return $this; | |
| 226 | + } | |
| 227 | + | |
| 228 | + /** | |
| 229 | +     * {@inheritdoc} | |
| 230 | + */ | |
| 231 | + public function withoutHeader($name) | |
| 232 | +    { | |
| 233 | + $result = clone $this; | |
| 234 | + | |
| 235 | + return $result->removeHeader($name); | |
| 236 | + } | |
| 237 | + | |
| 238 | + /** | |
| 239 | +     * {@inheritdoc} | |
| 240 | + */ | |
| 241 | + public function getBody() | |
| 242 | +    { | |
| 243 | + return $this->body; | |
| 244 | + } | |
| 245 | + | |
| 246 | + /** | |
| 247 | + * Sets the body. | |
| 248 | + * | |
| 249 | + * @param StreamInterface $body | |
| 250 | + * @return $this | |
| 251 | + */ | |
| 252 | + private function setBody(StreamInterface $body) | |
| 253 | +    { | |
| 254 | + $this->body = $body; | |
| 255 | + | |
| 256 | + return $this; | |
| 257 | + } | |
| 258 | + | |
| 259 | + /** | |
| 260 | +     * {@inheritdoc} | |
| 261 | + */ | |
| 262 | + public function withBody(StreamInterface $body) | |
| 263 | +    { | |
| 264 | + $result = clone $this; | |
| 265 | + | |
| 266 | + return $result->setBody($body); | |
| 267 | + } | |
| 268 | 268 | } | 
| @@ -22,141 +22,141 @@ | ||
| 22 | 22 | */ | 
| 23 | 23 | class Request extends Message implements RequestInterface | 
| 24 | 24 |  { | 
| 25 | - /** @var string The request target. */ | |
| 26 | - private $requestTarget; | |
| 27 | - | |
| 28 | - /** @var string The method. */ | |
| 29 | - private $method; | |
| 30 | - | |
| 31 | - /** @var UriInterface The URI. */ | |
| 32 | - private $uri; | |
| 33 | - | |
| 34 | - /** | |
| 35 | - * Construct a Request object with the given method, uri, version, headers & body. | |
| 36 | - * | |
| 37 | - * @param string $method | |
| 38 | - * @param UriInterface $uri | |
| 39 | - * @param string $version = self::DEFAULT_VERSION | |
| 40 | - * @param array $headers = [] | |
| 41 | - * @param StreamInterface|null $body = null | |
| 42 | - */ | |
| 43 | - public function __construct($method, UriInterface $uri, $version = self::DEFAULT_VERSION, array $headers = [], StreamInterface $body = null) | |
| 44 | -	{ | |
| 45 | - parent::__construct($version, $headers, $body); | |
| 46 | - | |
| 47 | - $this->setMethod($method); | |
| 48 | - $this->setUri($uri); | |
| 49 | - } | |
| 50 | - | |
| 51 | - /** | |
| 52 | -	 * {@inheritdoc} | |
| 53 | - */ | |
| 54 | - public function getRequestTarget() | |
| 55 | -	{ | |
| 56 | -		if (isset($this->requestTarget)) { | |
| 57 | - return $this->requestTarget; | |
| 58 | - } | |
| 59 | - | |
| 60 | - $result = $this->getUri()->getPath() ?: URI::DELIMITER_PATH; | |
| 61 | - | |
| 62 | -		if ($this->getUri()->getQuery()) { | |
| 63 | - $result .= URI::DELIMITER_QUERY . $this->getUri()->getQuery(); | |
| 64 | - } | |
| 65 | - | |
| 66 | - return $result; | |
| 67 | - } | |
| 68 | - | |
| 69 | - /** | |
| 70 | - * Set the request target. | |
| 71 | - * | |
| 72 | - * @param string $requestTarget | |
| 73 | - * @return $this | |
| 74 | - */ | |
| 75 | - private function setRequestTarget($requestTarget) | |
| 76 | -	{ | |
| 77 | - $this->requestTarget = $requestTarget; | |
| 78 | - | |
| 79 | - return $this; | |
| 80 | - } | |
| 81 | - | |
| 82 | - /** | |
| 83 | -	 * {@inheritdoc} | |
| 84 | - */ | |
| 85 | - public function withRequestTarget($requestTarget) | |
| 86 | -	{ | |
| 87 | - $result = clone $this; | |
| 88 | - | |
| 89 | - return $result->setRequestTarget($requestTarget); | |
| 90 | - } | |
| 91 | - | |
| 92 | - /** | |
| 93 | -	 * {@inheritdoc} | |
| 94 | - */ | |
| 95 | - public function getMethod() | |
| 96 | -	{ | |
| 97 | - return $this->method; | |
| 98 | - } | |
| 99 | - | |
| 100 | - /** | |
| 101 | - * Set the method. | |
| 102 | - * | |
| 103 | - * @param string $method | |
| 104 | - * @return $this | |
| 105 | - */ | |
| 106 | - private function setMethod($method) | |
| 107 | -	{ | |
| 108 | - $this->method = $method; | |
| 109 | - | |
| 110 | - return $this; | |
| 111 | - } | |
| 112 | - | |
| 113 | - /** | |
| 114 | -	 * {@inheritdoc} | |
| 115 | - */ | |
| 116 | - public function withMethod($method) | |
| 117 | -	{ | |
| 118 | - $result = clone $this; | |
| 119 | - | |
| 120 | - return $result->setMethod($method); | |
| 121 | - } | |
| 122 | - | |
| 123 | - /** | |
| 124 | -	 * {@inheritdoc} | |
| 125 | - */ | |
| 126 | - public function getUri() | |
| 127 | -	{ | |
| 128 | - return $this->uri; | |
| 129 | - } | |
| 130 | - | |
| 131 | - /** | |
| 132 | - * Set the uri. | |
| 133 | - * | |
| 134 | - * @param UriInterface $uri | |
| 135 | - * @param boolean $preserveHost = false | |
| 136 | - * @return $this | |
| 137 | - */ | |
| 138 | - private function setUri(UriInterface $uri, $preserveHost = false) | |
| 139 | -	{ | |
| 140 | - $this->uri = $uri; | |
| 141 | - | |
| 142 | -		if (!$preserveHost && ($host = $uri->getHost())) { | |
| 143 | -			if ($uri->getPort() !== null) { | |
| 144 | - $host .= URI::DELIMITER_PORT . $uri->getPort(); | |
| 145 | - } | |
| 146 | - | |
| 147 | -			$this->setHeader('Host', $host); | |
| 148 | - } | |
| 149 | - | |
| 150 | - return $this; | |
| 151 | - } | |
| 152 | - | |
| 153 | - /** | |
| 154 | -	 * {@inheritdoc} | |
| 155 | - */ | |
| 156 | - public function withUri(UriInterface $uri, $preserveHost = false) | |
| 157 | -	{ | |
| 158 | - $result = clone $this; | |
| 159 | - | |
| 160 | - return $result->setUri($uri, $preserveHost); | |
| 161 | - } | |
| 25 | + /** @var string The request target. */ | |
| 26 | + private $requestTarget; | |
| 27 | + | |
| 28 | + /** @var string The method. */ | |
| 29 | + private $method; | |
| 30 | + | |
| 31 | + /** @var UriInterface The URI. */ | |
| 32 | + private $uri; | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * Construct a Request object with the given method, uri, version, headers & body. | |
| 36 | + * | |
| 37 | + * @param string $method | |
| 38 | + * @param UriInterface $uri | |
| 39 | + * @param string $version = self::DEFAULT_VERSION | |
| 40 | + * @param array $headers = [] | |
| 41 | + * @param StreamInterface|null $body = null | |
| 42 | + */ | |
| 43 | + public function __construct($method, UriInterface $uri, $version = self::DEFAULT_VERSION, array $headers = [], StreamInterface $body = null) | |
| 44 | +    { | |
| 45 | + parent::__construct($version, $headers, $body); | |
| 46 | + | |
| 47 | + $this->setMethod($method); | |
| 48 | + $this->setUri($uri); | |
| 49 | + } | |
| 50 | + | |
| 51 | + /** | |
| 52 | +     * {@inheritdoc} | |
| 53 | + */ | |
| 54 | + public function getRequestTarget() | |
| 55 | +    { | |
| 56 | +        if (isset($this->requestTarget)) { | |
| 57 | + return $this->requestTarget; | |
| 58 | + } | |
| 59 | + | |
| 60 | + $result = $this->getUri()->getPath() ?: URI::DELIMITER_PATH; | |
| 61 | + | |
| 62 | +        if ($this->getUri()->getQuery()) { | |
| 63 | + $result .= URI::DELIMITER_QUERY . $this->getUri()->getQuery(); | |
| 64 | + } | |
| 65 | + | |
| 66 | + return $result; | |
| 67 | + } | |
| 68 | + | |
| 69 | + /** | |
| 70 | + * Set the request target. | |
| 71 | + * | |
| 72 | + * @param string $requestTarget | |
| 73 | + * @return $this | |
| 74 | + */ | |
| 75 | + private function setRequestTarget($requestTarget) | |
| 76 | +    { | |
| 77 | + $this->requestTarget = $requestTarget; | |
| 78 | + | |
| 79 | + return $this; | |
| 80 | + } | |
| 81 | + | |
| 82 | + /** | |
| 83 | +     * {@inheritdoc} | |
| 84 | + */ | |
| 85 | + public function withRequestTarget($requestTarget) | |
| 86 | +    { | |
| 87 | + $result = clone $this; | |
| 88 | + | |
| 89 | + return $result->setRequestTarget($requestTarget); | |
| 90 | + } | |
| 91 | + | |
| 92 | + /** | |
| 93 | +     * {@inheritdoc} | |
| 94 | + */ | |
| 95 | + public function getMethod() | |
| 96 | +    { | |
| 97 | + return $this->method; | |
| 98 | + } | |
| 99 | + | |
| 100 | + /** | |
| 101 | + * Set the method. | |
| 102 | + * | |
| 103 | + * @param string $method | |
| 104 | + * @return $this | |
| 105 | + */ | |
| 106 | + private function setMethod($method) | |
| 107 | +    { | |
| 108 | + $this->method = $method; | |
| 109 | + | |
| 110 | + return $this; | |
| 111 | + } | |
| 112 | + | |
| 113 | + /** | |
| 114 | +     * {@inheritdoc} | |
| 115 | + */ | |
| 116 | + public function withMethod($method) | |
| 117 | +    { | |
| 118 | + $result = clone $this; | |
| 119 | + | |
| 120 | + return $result->setMethod($method); | |
| 121 | + } | |
| 122 | + | |
| 123 | + /** | |
| 124 | +     * {@inheritdoc} | |
| 125 | + */ | |
| 126 | + public function getUri() | |
| 127 | +    { | |
| 128 | + return $this->uri; | |
| 129 | + } | |
| 130 | + | |
| 131 | + /** | |
| 132 | + * Set the uri. | |
| 133 | + * | |
| 134 | + * @param UriInterface $uri | |
| 135 | + * @param boolean $preserveHost = false | |
| 136 | + * @return $this | |
| 137 | + */ | |
| 138 | + private function setUri(UriInterface $uri, $preserveHost = false) | |
| 139 | +    { | |
| 140 | + $this->uri = $uri; | |
| 141 | + | |
| 142 | +        if (!$preserveHost && ($host = $uri->getHost())) { | |
| 143 | +            if ($uri->getPort() !== null) { | |
| 144 | + $host .= URI::DELIMITER_PORT . $uri->getPort(); | |
| 145 | + } | |
| 146 | + | |
| 147 | +            $this->setHeader('Host', $host); | |
| 148 | + } | |
| 149 | + | |
| 150 | + return $this; | |
| 151 | + } | |
| 152 | + | |
| 153 | + /** | |
| 154 | +     * {@inheritdoc} | |
| 155 | + */ | |
| 156 | + public function withUri(UriInterface $uri, $preserveHost = false) | |
| 157 | +    { | |
| 158 | + $result = clone $this; | |
| 159 | + | |
| 160 | + return $result->setUri($uri, $preserveHost); | |
| 161 | + } | |
| 162 | 162 | } | 
| @@ -60,7 +60,7 @@ discard block | ||
| 60 | 60 | $result = $this->getUri()->getPath() ?: URI::DELIMITER_PATH; | 
| 61 | 61 | |
| 62 | 62 |  		if ($this->getUri()->getQuery()) { | 
| 63 | - $result .= URI::DELIMITER_QUERY . $this->getUri()->getQuery(); | |
| 63 | + $result .= URI::DELIMITER_QUERY.$this->getUri()->getQuery(); | |
| 64 | 64 | } | 
| 65 | 65 | |
| 66 | 66 | return $result; | 
| @@ -141,7 +141,7 @@ discard block | ||
| 141 | 141 | |
| 142 | 142 |  		if (!$preserveHost && ($host = $uri->getHost())) { | 
| 143 | 143 |  			if ($uri->getPort() !== null) { | 
| 144 | - $host .= URI::DELIMITER_PORT . $uri->getPort(); | |
| 144 | + $host .= URI::DELIMITER_PORT.$uri->getPort(); | |
| 145 | 145 | } | 
| 146 | 146 | |
| 147 | 147 |  			$this->setHeader('Host', $host); | 
| @@ -21,131 +21,131 @@ | ||
| 21 | 21 | */ | 
| 22 | 22 | class Response extends Message implements ResponseInterface | 
| 23 | 23 |  { | 
| 24 | - /** @var int The status code. */ | |
| 25 | - private $statusCode; | |
| 24 | + /** @var int The status code. */ | |
| 25 | + private $statusCode; | |
| 26 | 26 | |
| 27 | - /** @var string The reason phrase. */ | |
| 28 | - private $reasonPhrase; | |
| 27 | + /** @var string The reason phrase. */ | |
| 28 | + private $reasonPhrase; | |
| 29 | 29 | |
| 30 | - /** @var array The reason phrases */ | |
| 31 | - private static $reasonPhrases = [ | |
| 32 | - 100 => 'Continue', | |
| 33 | - 101 => 'Switching Protocols', | |
| 34 | - 102 => 'Processing', | |
| 35 | - 200 => 'OK', | |
| 36 | - 201 => 'Created', | |
| 37 | - 202 => 'Accepted', | |
| 38 | - 203 => 'Non-Authoritative Information', | |
| 39 | - 204 => 'No Content', | |
| 40 | - 205 => 'Reset Content', | |
| 41 | - 206 => 'Partial Content', | |
| 42 | - 207 => 'Multi-status', | |
| 43 | - 208 => 'Already Reported', | |
| 44 | - 300 => 'Multiple Choices', | |
| 45 | - 301 => 'Moved Permanently', | |
| 46 | - 302 => 'Found', | |
| 47 | - 303 => 'See Other', | |
| 48 | - 304 => 'Not Modified', | |
| 49 | - 305 => 'Use Proxy', | |
| 50 | - 306 => 'Switch Proxy', | |
| 51 | - 307 => 'Temporary Redirect', | |
| 52 | - 400 => 'Bad Request', | |
| 53 | - 401 => 'Unauthorized', | |
| 54 | - 402 => 'Payment Required', | |
| 55 | - 403 => 'Forbidden', | |
| 56 | - 404 => 'Not Found', | |
| 57 | - 405 => 'Method Not Allowed', | |
| 58 | - 406 => 'Not Acceptable', | |
| 59 | - 407 => 'Proxy Authentication Required', | |
| 60 | - 408 => 'Request Time-out', | |
| 61 | - 409 => 'Conflict', | |
| 62 | - 410 => 'Gone', | |
| 63 | - 411 => 'Length Required', | |
| 64 | - 412 => 'Precondition Failed', | |
| 65 | - 413 => 'Request Entity Too Large', | |
| 66 | - 414 => 'Request-URI Too Large', | |
| 67 | - 415 => 'Unsupported Media Type', | |
| 68 | - 416 => 'Requested range not satisfiable', | |
| 69 | - 417 => 'Expectation Failed', | |
| 70 | - 418 => 'I\'m a teapot', | |
| 71 | - 422 => 'Unprocessable Entity', | |
| 72 | - 423 => 'Locked', | |
| 73 | - 424 => 'Failed Dependency', | |
| 74 | - 425 => 'Unordered Collection', | |
| 75 | - 426 => 'Upgrade Required', | |
| 76 | - 428 => 'Precondition Required', | |
| 77 | - 429 => 'Too Many Requests', | |
| 78 | - 431 => 'Request Header Fields Too Large', | |
| 79 | - 500 => 'Internal Server Error', | |
| 80 | - 501 => 'Not Implemented', | |
| 81 | - 502 => 'Bad Gateway', | |
| 82 | - 503 => 'Service Unavailable', | |
| 83 | - 504 => 'Gateway Time-out', | |
| 84 | - 505 => 'HTTP Version not supported', | |
| 85 | - 506 => 'Variant Also Negotiates', | |
| 86 | - 507 => 'Insufficient Storage', | |
| 87 | - 508 => 'Loop Detected', | |
| 88 | - 511 => 'Network Authentication Required', | |
| 89 | - ]; | |
| 30 | + /** @var array The reason phrases */ | |
| 31 | + private static $reasonPhrases = [ | |
| 32 | + 100 => 'Continue', | |
| 33 | + 101 => 'Switching Protocols', | |
| 34 | + 102 => 'Processing', | |
| 35 | + 200 => 'OK', | |
| 36 | + 201 => 'Created', | |
| 37 | + 202 => 'Accepted', | |
| 38 | + 203 => 'Non-Authoritative Information', | |
| 39 | + 204 => 'No Content', | |
| 40 | + 205 => 'Reset Content', | |
| 41 | + 206 => 'Partial Content', | |
| 42 | + 207 => 'Multi-status', | |
| 43 | + 208 => 'Already Reported', | |
| 44 | + 300 => 'Multiple Choices', | |
| 45 | + 301 => 'Moved Permanently', | |
| 46 | + 302 => 'Found', | |
| 47 | + 303 => 'See Other', | |
| 48 | + 304 => 'Not Modified', | |
| 49 | + 305 => 'Use Proxy', | |
| 50 | + 306 => 'Switch Proxy', | |
| 51 | + 307 => 'Temporary Redirect', | |
| 52 | + 400 => 'Bad Request', | |
| 53 | + 401 => 'Unauthorized', | |
| 54 | + 402 => 'Payment Required', | |
| 55 | + 403 => 'Forbidden', | |
| 56 | + 404 => 'Not Found', | |
| 57 | + 405 => 'Method Not Allowed', | |
| 58 | + 406 => 'Not Acceptable', | |
| 59 | + 407 => 'Proxy Authentication Required', | |
| 60 | + 408 => 'Request Time-out', | |
| 61 | + 409 => 'Conflict', | |
| 62 | + 410 => 'Gone', | |
| 63 | + 411 => 'Length Required', | |
| 64 | + 412 => 'Precondition Failed', | |
| 65 | + 413 => 'Request Entity Too Large', | |
| 66 | + 414 => 'Request-URI Too Large', | |
| 67 | + 415 => 'Unsupported Media Type', | |
| 68 | + 416 => 'Requested range not satisfiable', | |
| 69 | + 417 => 'Expectation Failed', | |
| 70 | + 418 => 'I\'m a teapot', | |
| 71 | + 422 => 'Unprocessable Entity', | |
| 72 | + 423 => 'Locked', | |
| 73 | + 424 => 'Failed Dependency', | |
| 74 | + 425 => 'Unordered Collection', | |
| 75 | + 426 => 'Upgrade Required', | |
| 76 | + 428 => 'Precondition Required', | |
| 77 | + 429 => 'Too Many Requests', | |
| 78 | + 431 => 'Request Header Fields Too Large', | |
| 79 | + 500 => 'Internal Server Error', | |
| 80 | + 501 => 'Not Implemented', | |
| 81 | + 502 => 'Bad Gateway', | |
| 82 | + 503 => 'Service Unavailable', | |
| 83 | + 504 => 'Gateway Time-out', | |
| 84 | + 505 => 'HTTP Version not supported', | |
| 85 | + 506 => 'Variant Also Negotiates', | |
| 86 | + 507 => 'Insufficient Storage', | |
| 87 | + 508 => 'Loop Detected', | |
| 88 | + 511 => 'Network Authentication Required', | |
| 89 | + ]; | |
| 90 | 90 | |
| 91 | - /** | |
| 92 | - * Construct a Response object with the given status code, reason phrase, version, headers & body. | |
| 93 | - * | |
| 94 | - * @param int $statusCode | |
| 95 | - * @param string $reasonPhrase = '' | |
| 96 | - * @param string $version = self::DEFAULT_VERSION | |
| 97 | - * @param array $headers = [] | |
| 98 | - * @param StreamInterface|null $body = null | |
| 99 | - */ | |
| 100 | - public function __construct($statusCode, $reasonPhrase = '', $version = self::DEFAULT_VERSION, array $headers = [], StreamInterface $body = null) | |
| 101 | -	{ | |
| 102 | - parent::__construct($version, $headers, $body); | |
| 91 | + /** | |
| 92 | + * Construct a Response object with the given status code, reason phrase, version, headers & body. | |
| 93 | + * | |
| 94 | + * @param int $statusCode | |
| 95 | + * @param string $reasonPhrase = '' | |
| 96 | + * @param string $version = self::DEFAULT_VERSION | |
| 97 | + * @param array $headers = [] | |
| 98 | + * @param StreamInterface|null $body = null | |
| 99 | + */ | |
| 100 | + public function __construct($statusCode, $reasonPhrase = '', $version = self::DEFAULT_VERSION, array $headers = [], StreamInterface $body = null) | |
| 101 | +    { | |
| 102 | + parent::__construct($version, $headers, $body); | |
| 103 | 103 | |
| 104 | - $this->setStatus($statusCode, $reasonPhrase); | |
| 105 | - } | |
| 104 | + $this->setStatus($statusCode, $reasonPhrase); | |
| 105 | + } | |
| 106 | 106 | |
| 107 | - /** | |
| 108 | -	 * {@inheritdoc} | |
| 109 | - */ | |
| 110 | - public function getStatusCode() | |
| 111 | -	{ | |
| 112 | - return $this->statusCode; | |
| 113 | - } | |
| 107 | + /** | |
| 108 | +     * {@inheritdoc} | |
| 109 | + */ | |
| 110 | + public function getStatusCode() | |
| 111 | +    { | |
| 112 | + return $this->statusCode; | |
| 113 | + } | |
| 114 | 114 | |
| 115 | - /** | |
| 116 | - * Set the status. | |
| 117 | - * | |
| 118 | - * @param int $statusCode | |
| 119 | - * @param string $reasonPhrase = '' | |
| 120 | - * @return $this | |
| 121 | - */ | |
| 122 | - private function setStatus($statusCode, $reasonPhrase = '') | |
| 123 | -	{ | |
| 124 | -		if ($reasonPhrase === '' && isset(self::$reasonPhrases[$statusCode])) { | |
| 125 | - $reasonPhrase = self::$reasonPhrases[$statusCode]; | |
| 126 | - } | |
| 115 | + /** | |
| 116 | + * Set the status. | |
| 117 | + * | |
| 118 | + * @param int $statusCode | |
| 119 | + * @param string $reasonPhrase = '' | |
| 120 | + * @return $this | |
| 121 | + */ | |
| 122 | + private function setStatus($statusCode, $reasonPhrase = '') | |
| 123 | +    { | |
| 124 | +        if ($reasonPhrase === '' && isset(self::$reasonPhrases[$statusCode])) { | |
| 125 | + $reasonPhrase = self::$reasonPhrases[$statusCode]; | |
| 126 | + } | |
| 127 | 127 | |
| 128 | - $this->statusCode = $statusCode; | |
| 129 | - $this->reasonPhrase = $reasonPhrase; | |
| 128 | + $this->statusCode = $statusCode; | |
| 129 | + $this->reasonPhrase = $reasonPhrase; | |
| 130 | 130 | |
| 131 | - return $this; | |
| 132 | - } | |
| 131 | + return $this; | |
| 132 | + } | |
| 133 | 133 | |
| 134 | - /** | |
| 135 | -	 * {@inheritdoc} | |
| 136 | - */ | |
| 137 | - public function withStatus($statusCode, $reasonPhrase = '') | |
| 138 | -	{ | |
| 139 | - $result = clone $this; | |
| 134 | + /** | |
| 135 | +     * {@inheritdoc} | |
| 136 | + */ | |
| 137 | + public function withStatus($statusCode, $reasonPhrase = '') | |
| 138 | +    { | |
| 139 | + $result = clone $this; | |
| 140 | 140 | |
| 141 | - return $result->setStatus($statusCode, $reasonPhrase); | |
| 142 | - } | |
| 141 | + return $result->setStatus($statusCode, $reasonPhrase); | |
| 142 | + } | |
| 143 | 143 | |
| 144 | - /** | |
| 145 | -	 * {@inheritdoc} | |
| 146 | - */ | |
| 147 | - public function getReasonPhrase() | |
| 148 | -	{ | |
| 149 | - return $this->reasonPhrase; | |
| 150 | - } | |
| 144 | + /** | |
| 145 | +     * {@inheritdoc} | |
| 146 | + */ | |
| 147 | + public function getReasonPhrase() | |
| 148 | +    { | |
| 149 | + return $this->reasonPhrase; | |
| 150 | + } | |
| 151 | 151 | } | 
| @@ -17,29 +17,29 @@ | ||
| 17 | 17 | */ | 
| 18 | 18 | class ServerResponseException extends \Exception | 
| 19 | 19 |  { | 
| 20 | - /** @var ServerResponse The server response. */ | |
| 21 | - private $serverResponse; | |
| 20 | + /** @var ServerResponse The server response. */ | |
| 21 | + private $serverResponse; | |
| 22 | 22 | |
| 23 | - /** | |
| 24 | - * Contract a server response exception with the gieven server response. | |
| 25 | - * | |
| 26 | - * @param ServerResponse $serverResponse | |
| 27 | - * @param \Exception|null $exception = null | |
| 28 | - */ | |
| 29 | - public function __construct(ServerResponse $serverResponse, \Exception $exception = null) | |
| 30 | -	{ | |
| 31 | - parent::__construct($serverResponse->getReasonPhrase(), $serverResponse->getStatusCode(), $exception); | |
| 23 | + /** | |
| 24 | + * Contract a server response exception with the gieven server response. | |
| 25 | + * | |
| 26 | + * @param ServerResponse $serverResponse | |
| 27 | + * @param \Exception|null $exception = null | |
| 28 | + */ | |
| 29 | + public function __construct(ServerResponse $serverResponse, \Exception $exception = null) | |
| 30 | +    { | |
| 31 | + parent::__construct($serverResponse->getReasonPhrase(), $serverResponse->getStatusCode(), $exception); | |
| 32 | 32 | |
| 33 | - $this->serverResponse = $serverResponse; | |
| 34 | - } | |
| 33 | + $this->serverResponse = $serverResponse; | |
| 34 | + } | |
| 35 | 35 | |
| 36 | - /** | |
| 37 | - * Returns the server response. | |
| 38 | - * | |
| 39 | - * @return ServerResponse the server response. | |
| 40 | - */ | |
| 41 | - public function getServerResponse() | |
| 42 | -	{ | |
| 43 | - return $this->serverResponse; | |
| 44 | - } | |
| 36 | + /** | |
| 37 | + * Returns the server response. | |
| 38 | + * | |
| 39 | + * @return ServerResponse the server response. | |
| 40 | + */ | |
| 41 | + public function getServerResponse() | |
| 42 | +    { | |
| 43 | + return $this->serverResponse; | |
| 44 | + } | |
| 45 | 45 | } | 
| @@ -20,648 +20,648 @@ | ||
| 20 | 20 | */ | 
| 21 | 21 | class URI implements UriInterface | 
| 22 | 22 |  { | 
| 23 | - const DELIMITER_SCHEME = ':'; | |
| 24 | - const DELIMITER_AUTHORITY = '//'; | |
| 25 | - const DELIMITER_USER = '@'; | |
| 26 | - const DELIMITER_PASSWORD = ':'; | |
| 27 | - const DELIMITER_PORT = ':'; | |
| 28 | - const DELIMITER_PATH = '/'; | |
| 29 | - const DELIMITER_QUERY = '?'; | |
| 30 | - const DELIMITER_QUERY_PAIR = '&'; | |
| 31 | - const DELIMITER_QUERY_KEY_VALUE = '='; | |
| 32 | - const DELIMITER_FRAGMENT = '#'; | |
| 33 | - | |
| 34 | - const SCHEME = 'scheme'; | |
| 35 | - const AUTHORITY = 'authority'; | |
| 36 | - const USERNAME = 'user'; | |
| 37 | - const PASSWORD = 'pass'; | |
| 38 | - const HOST = 'host'; | |
| 39 | - const PORT = 'port'; | |
| 40 | - const PATH = 'path'; | |
| 41 | - const DIRECTORY = 'directory'; | |
| 42 | - const FILE = 'file'; | |
| 43 | - const QUERY = 'query'; | |
| 44 | - const FRAGMENT = 'fragment'; | |
| 45 | - | |
| 46 | - /** @var string The scheme. */ | |
| 47 | - private $scheme; | |
| 48 | - | |
| 49 | - /** @var string The username. */ | |
| 50 | - private $username; | |
| 51 | - | |
| 52 | - /** @var string|null The password. */ | |
| 53 | - private $password; | |
| 54 | - | |
| 55 | - /** @var string The host. */ | |
| 56 | - private $host; | |
| 57 | - | |
| 58 | - /** @var int|null The port. */ | |
| 59 | - private $port; | |
| 60 | - | |
| 61 | - /** @var string The directory. */ | |
| 62 | - private $directory; | |
| 63 | - | |
| 64 | - /** @var string The file. */ | |
| 65 | - private $file; | |
| 66 | - | |
| 67 | - /** @var array The query. */ | |
| 68 | - private $query; | |
| 69 | - | |
| 70 | - /** @var string The fragment. */ | |
| 71 | - private $fragment; | |
| 72 | - | |
| 73 | - /** | |
| 74 | - * Construct a URI object with the given URI. | |
| 75 | - * | |
| 76 | - * @param string $uri | |
| 77 | - * @throws \UnexpectedValueException | |
| 78 | - */ | |
| 79 | - public function __construct($uri) | |
| 80 | -	{ | |
| 81 | - $component = parse_url($uri); | |
| 82 | - | |
| 83 | -		if ($component === false) { | |
| 84 | -			throw new \UnexpectedValueException('Invalid uri'); | |
| 85 | - } | |
| 86 | - | |
| 87 | - // Replace with the null coalescing in PHP7. E.g. $component[scheme] ?? '' | |
| 88 | - $component += [ | |
| 89 | - static::SCHEME => '', | |
| 90 | - static::USERNAME => '', | |
| 91 | - static::PASSWORD => null, | |
| 92 | - static::HOST => '', | |
| 93 | - static::PORT => null, | |
| 94 | - static::PATH => '', | |
| 95 | - static::QUERY => '', | |
| 96 | - static::FRAGMENT => '' | |
| 97 | - ]; | |
| 98 | - | |
| 99 | - $this->setScheme($component[static::SCHEME]); | |
| 100 | - $this->setUserInfo($component[static::USERNAME], $component[static::PASSWORD]); | |
| 101 | - $this->setHost($component[static::HOST]); | |
| 102 | - $this->setPort($component[static::PORT]); | |
| 103 | - $this->setPath($component[static::PATH]); | |
| 104 | - $this->setQuery($component[static::QUERY]); | |
| 105 | - $this->setFragment($component[static::FRAGMENT]); | |
| 106 | - } | |
| 107 | - | |
| 108 | - /** | |
| 109 | - * Returns a string representation of the URI object. | |
| 110 | - * | |
| 111 | - * @return string a string representation of the URI object. | |
| 112 | - */ | |
| 113 | - public function __toString() | |
| 114 | -	{ | |
| 115 | - return $this->getUri(); | |
| 116 | - } | |
| 117 | - | |
| 118 | - /** | |
| 119 | - * Returns the URI with the given start and stop component. | |
| 120 | - * | |
| 121 | - * @param string $start = self::SCHEME | |
| 122 | - * @param string $end = self::FRAGMENT | |
| 123 | - * @return string the URI. | |
| 124 | - */ | |
| 125 | - public function getUri($start = self::SCHEME, $end = self::FRAGMENT) | |
| 126 | -	{ | |
| 127 | - $result = ''; | |
| 128 | - | |
| 129 | -		switch ($start) { | |
| 130 | - default: | |
| 131 | - case static::SCHEME: | |
| 132 | - $scheme = $this->getScheme(); | |
| 133 | - | |
| 134 | -				if ($scheme) { | |
| 135 | - $result .= $scheme . static::DELIMITER_SCHEME; | |
| 136 | - } | |
| 137 | - | |
| 138 | -				if ($end === static::SCHEME) { | |
| 139 | - break; | |
| 140 | - } | |
| 23 | + const DELIMITER_SCHEME = ':'; | |
| 24 | + const DELIMITER_AUTHORITY = '//'; | |
| 25 | + const DELIMITER_USER = '@'; | |
| 26 | + const DELIMITER_PASSWORD = ':'; | |
| 27 | + const DELIMITER_PORT = ':'; | |
| 28 | + const DELIMITER_PATH = '/'; | |
| 29 | + const DELIMITER_QUERY = '?'; | |
| 30 | + const DELIMITER_QUERY_PAIR = '&'; | |
| 31 | + const DELIMITER_QUERY_KEY_VALUE = '='; | |
| 32 | + const DELIMITER_FRAGMENT = '#'; | |
| 33 | + | |
| 34 | + const SCHEME = 'scheme'; | |
| 35 | + const AUTHORITY = 'authority'; | |
| 36 | + const USERNAME = 'user'; | |
| 37 | + const PASSWORD = 'pass'; | |
| 38 | + const HOST = 'host'; | |
| 39 | + const PORT = 'port'; | |
| 40 | + const PATH = 'path'; | |
| 41 | + const DIRECTORY = 'directory'; | |
| 42 | + const FILE = 'file'; | |
| 43 | + const QUERY = 'query'; | |
| 44 | + const FRAGMENT = 'fragment'; | |
| 45 | + | |
| 46 | + /** @var string The scheme. */ | |
| 47 | + private $scheme; | |
| 48 | + | |
| 49 | + /** @var string The username. */ | |
| 50 | + private $username; | |
| 51 | + | |
| 52 | + /** @var string|null The password. */ | |
| 53 | + private $password; | |
| 54 | + | |
| 55 | + /** @var string The host. */ | |
| 56 | + private $host; | |
| 57 | + | |
| 58 | + /** @var int|null The port. */ | |
| 59 | + private $port; | |
| 60 | + | |
| 61 | + /** @var string The directory. */ | |
| 62 | + private $directory; | |
| 63 | + | |
| 64 | + /** @var string The file. */ | |
| 65 | + private $file; | |
| 66 | + | |
| 67 | + /** @var array The query. */ | |
| 68 | + private $query; | |
| 69 | + | |
| 70 | + /** @var string The fragment. */ | |
| 71 | + private $fragment; | |
| 72 | + | |
| 73 | + /** | |
| 74 | + * Construct a URI object with the given URI. | |
| 75 | + * | |
| 76 | + * @param string $uri | |
| 77 | + * @throws \UnexpectedValueException | |
| 78 | + */ | |
| 79 | + public function __construct($uri) | |
| 80 | +    { | |
| 81 | + $component = parse_url($uri); | |
| 82 | + | |
| 83 | +        if ($component === false) { | |
| 84 | +            throw new \UnexpectedValueException('Invalid uri'); | |
| 85 | + } | |
| 86 | + | |
| 87 | + // Replace with the null coalescing in PHP7. E.g. $component[scheme] ?? '' | |
| 88 | + $component += [ | |
| 89 | + static::SCHEME => '', | |
| 90 | + static::USERNAME => '', | |
| 91 | + static::PASSWORD => null, | |
| 92 | + static::HOST => '', | |
| 93 | + static::PORT => null, | |
| 94 | + static::PATH => '', | |
| 95 | + static::QUERY => '', | |
| 96 | + static::FRAGMENT => '' | |
| 97 | + ]; | |
| 98 | + | |
| 99 | + $this->setScheme($component[static::SCHEME]); | |
| 100 | + $this->setUserInfo($component[static::USERNAME], $component[static::PASSWORD]); | |
| 101 | + $this->setHost($component[static::HOST]); | |
| 102 | + $this->setPort($component[static::PORT]); | |
| 103 | + $this->setPath($component[static::PATH]); | |
| 104 | + $this->setQuery($component[static::QUERY]); | |
| 105 | + $this->setFragment($component[static::FRAGMENT]); | |
| 106 | + } | |
| 107 | + | |
| 108 | + /** | |
| 109 | + * Returns a string representation of the URI object. | |
| 110 | + * | |
| 111 | + * @return string a string representation of the URI object. | |
| 112 | + */ | |
| 113 | + public function __toString() | |
| 114 | +    { | |
| 115 | + return $this->getUri(); | |
| 116 | + } | |
| 117 | + | |
| 118 | + /** | |
| 119 | + * Returns the URI with the given start and stop component. | |
| 120 | + * | |
| 121 | + * @param string $start = self::SCHEME | |
| 122 | + * @param string $end = self::FRAGMENT | |
| 123 | + * @return string the URI. | |
| 124 | + */ | |
| 125 | + public function getUri($start = self::SCHEME, $end = self::FRAGMENT) | |
| 126 | +    { | |
| 127 | + $result = ''; | |
| 128 | + | |
| 129 | +        switch ($start) { | |
| 130 | + default: | |
| 131 | + case static::SCHEME: | |
| 132 | + $scheme = $this->getScheme(); | |
| 133 | + | |
| 134 | +                if ($scheme) { | |
| 135 | + $result .= $scheme . static::DELIMITER_SCHEME; | |
| 136 | + } | |
| 137 | + | |
| 138 | +                if ($end === static::SCHEME) { | |
| 139 | + break; | |
| 140 | + } | |
| 141 | 141 | |
| 142 | - // no break | |
| 142 | + // no break | |
| 143 | 143 | |
| 144 | - case static::AUTHORITY: | |
| 145 | - case static::USERNAME: | |
| 146 | - $username = $this->getUserInfo(); | |
| 144 | + case static::AUTHORITY: | |
| 145 | + case static::USERNAME: | |
| 146 | + $username = $this->getUserInfo(); | |
| 147 | 147 | |
| 148 | -				if ($username && $this->getHost()) { | |
| 149 | - $result .= static::DELIMITER_AUTHORITY . $username . static::DELIMITER_USER; | |
| 150 | - } | |
| 148 | +                if ($username && $this->getHost()) { | |
| 149 | + $result .= static::DELIMITER_AUTHORITY . $username . static::DELIMITER_USER; | |
| 150 | + } | |
| 151 | 151 | |
| 152 | -				if ($end === static::USERNAME) { | |
| 153 | - break; | |
| 154 | - } | |
| 155 | - | |
| 156 | - // no break | |
| 157 | - | |
| 158 | - case static::HOST: | |
| 159 | - $host = $this->getHost(); | |
| 160 | - | |
| 161 | -				if ($host && ($result === '' || !$this->getUserInfo())) { | |
| 162 | - $result .= static::DELIMITER_AUTHORITY; | |
| 163 | - } | |
| 164 | - | |
| 165 | - $result .= $host; | |
| 166 | - | |
| 167 | -				if ($end === static::HOST) { | |
| 168 | - break; | |
| 169 | - } | |
| 170 | - | |
| 171 | - // no break | |
| 172 | - | |
| 173 | - case static::PORT: | |
| 174 | - $port = $this->getPort(); | |
| 175 | - | |
| 176 | -				if ($port !== null && $this->getHost()) { | |
| 177 | - $result .= static::DELIMITER_PORT . $port; | |
| 178 | - } | |
| 179 | - | |
| 180 | -				if ($end === static::PORT || $end === static::AUTHORITY) { | |
| 181 | - break; | |
| 182 | - } | |
| 183 | - | |
| 184 | - // no break | |
| 185 | - | |
| 186 | - case static::PATH: | |
| 187 | - case static::DIRECTORY: | |
| 188 | - $directory = $this->getDirectory(); | |
| 189 | - | |
| 190 | -				if ($result !== '' && $directory !== '' && substr($directory, 0, 1) !== static::DELIMITER_PATH) { | |
| 191 | - $result .= static::DELIMITER_PATH; | |
| 192 | - } | |
| 193 | - | |
| 194 | - $result .= $directory; | |
| 195 | - | |
| 196 | -				if ($end === static::DIRECTORY) { | |
| 197 | - break; | |
| 198 | - } | |
| 199 | - | |
| 200 | - // no break | |
| 201 | - | |
| 202 | - case static::FILE: | |
| 203 | - $file = $this->getFile(); | |
| 204 | - | |
| 205 | -				if ($result !== '' && substr($result, -1) !== static::DELIMITER_PATH && $file !== '') { | |
| 206 | - $result .= static::DELIMITER_PATH; | |
| 207 | - } | |
| 208 | - | |
| 209 | - $result .= $this->getFile(); | |
| 210 | - | |
| 211 | -				if ($end === static::FILE || $end === static::PATH) { | |
| 212 | - break; | |
| 213 | - } | |
| 214 | - | |
| 215 | - // no break | |
| 216 | - | |
| 217 | - case static::QUERY: | |
| 218 | - $query = $this->getQuery(); | |
| 219 | - | |
| 220 | -				if ($query) { | |
| 221 | - $result .= static::DELIMITER_QUERY . $query; | |
| 222 | - } | |
| 223 | - | |
| 224 | -				if ($end === static::QUERY) { | |
| 225 | - break; | |
| 226 | - } | |
| 152 | +                if ($end === static::USERNAME) { | |
| 153 | + break; | |
| 154 | + } | |
| 155 | + | |
| 156 | + // no break | |
| 157 | + | |
| 158 | + case static::HOST: | |
| 159 | + $host = $this->getHost(); | |
| 160 | + | |
| 161 | +                if ($host && ($result === '' || !$this->getUserInfo())) { | |
| 162 | + $result .= static::DELIMITER_AUTHORITY; | |
| 163 | + } | |
| 164 | + | |
| 165 | + $result .= $host; | |
| 166 | + | |
| 167 | +                if ($end === static::HOST) { | |
| 168 | + break; | |
| 169 | + } | |
| 170 | + | |
| 171 | + // no break | |
| 172 | + | |
| 173 | + case static::PORT: | |
| 174 | + $port = $this->getPort(); | |
| 175 | + | |
| 176 | +                if ($port !== null && $this->getHost()) { | |
| 177 | + $result .= static::DELIMITER_PORT . $port; | |
| 178 | + } | |
| 179 | + | |
| 180 | +                if ($end === static::PORT || $end === static::AUTHORITY) { | |
| 181 | + break; | |
| 182 | + } | |
| 183 | + | |
| 184 | + // no break | |
| 185 | + | |
| 186 | + case static::PATH: | |
| 187 | + case static::DIRECTORY: | |
| 188 | + $directory = $this->getDirectory(); | |
| 189 | + | |
| 190 | +                if ($result !== '' && $directory !== '' && substr($directory, 0, 1) !== static::DELIMITER_PATH) { | |
| 191 | + $result .= static::DELIMITER_PATH; | |
| 192 | + } | |
| 193 | + | |
| 194 | + $result .= $directory; | |
| 195 | + | |
| 196 | +                if ($end === static::DIRECTORY) { | |
| 197 | + break; | |
| 198 | + } | |
| 199 | + | |
| 200 | + // no break | |
| 201 | + | |
| 202 | + case static::FILE: | |
| 203 | + $file = $this->getFile(); | |
| 204 | + | |
| 205 | +                if ($result !== '' && substr($result, -1) !== static::DELIMITER_PATH && $file !== '') { | |
| 206 | + $result .= static::DELIMITER_PATH; | |
| 207 | + } | |
| 208 | + | |
| 209 | + $result .= $this->getFile(); | |
| 210 | + | |
| 211 | +                if ($end === static::FILE || $end === static::PATH) { | |
| 212 | + break; | |
| 213 | + } | |
| 214 | + | |
| 215 | + // no break | |
| 216 | + | |
| 217 | + case static::QUERY: | |
| 218 | + $query = $this->getQuery(); | |
| 219 | + | |
| 220 | +                if ($query) { | |
| 221 | + $result .= static::DELIMITER_QUERY . $query; | |
| 222 | + } | |
| 223 | + | |
| 224 | +                if ($end === static::QUERY) { | |
| 225 | + break; | |
| 226 | + } | |
| 227 | 227 | |
| 228 | - // no break | |
| 229 | - | |
| 230 | - case static::FRAGMENT: | |
| 231 | - $fragment = $this->getFragment(); | |
| 228 | + // no break | |
| 229 | + | |
| 230 | + case static::FRAGMENT: | |
| 231 | + $fragment = $this->getFragment(); | |
| 232 | 232 | |
| 233 | -				if ($fragment) { | |
| 234 | - $result .= static::DELIMITER_FRAGMENT . $fragment; | |
| 235 | - } | |
| 236 | - | |
| 237 | - // no break | |
| 238 | - } | |
| 239 | - | |
| 240 | - return $result; | |
| 241 | - } | |
| 242 | - | |
| 243 | - /** | |
| 244 | -	 * {@inheritdoc} | |
| 245 | - */ | |
| 246 | - public function getScheme() | |
| 247 | -	{ | |
| 248 | - return $this->scheme; | |
| 249 | - } | |
| 250 | - | |
| 251 | - /** | |
| 252 | - * Set the scheme. | |
| 253 | - * | |
| 254 | - * @param string $scheme | |
| 255 | - * @return $this | |
| 256 | - */ | |
| 257 | - private function setScheme($scheme) | |
| 258 | -	{ | |
| 259 | - $this->scheme = strtolower($scheme); | |
| 260 | - | |
| 261 | - return $this; | |
| 262 | - } | |
| 263 | - | |
| 264 | - /** | |
| 265 | -	 * {@inheritdoc} | |
| 266 | - */ | |
| 267 | - public function withScheme($scheme) | |
| 268 | -	{ | |
| 269 | - $result = clone $this; | |
| 270 | - | |
| 271 | - return $result->setScheme($scheme); | |
| 272 | - } | |
| 273 | - | |
| 274 | - /** | |
| 275 | -	 * {@inheritdoc} | |
| 276 | - */ | |
| 277 | - public function getAuthority() | |
| 278 | -	{ | |
| 279 | -		if (!$this->getHost()) { | |
| 280 | - return ''; | |
| 281 | - } | |
| 282 | - | |
| 283 | - return substr($this->getUri(self::USERNAME, self::PORT), 2); | |
| 284 | - } | |
| 285 | - | |
| 286 | - /** | |
| 287 | -	 * {@inheritdoc} | |
| 288 | - */ | |
| 289 | - public function getUserInfo() | |
| 290 | -	{ | |
| 291 | - $result = $this->username; | |
| 292 | - | |
| 293 | -		if ($this->password !== null) { | |
| 294 | - $result .= static::DELIMITER_PASSWORD . $this->password; | |
| 295 | - } | |
| 296 | - | |
| 297 | - return $result; | |
| 298 | - } | |
| 299 | - | |
| 300 | - /** | |
| 301 | - * Set the user info. | |
| 302 | - * | |
| 303 | - * @param string $username | |
| 304 | - * @param string|null $password = null | |
| 305 | - * @return $this | |
| 306 | - */ | |
| 307 | - private function setUserInfo($username, $password = null) | |
| 308 | -	{ | |
| 309 | - $this->username = $username; | |
| 310 | - $this->password = $password; | |
| 311 | - | |
| 312 | - return $this; | |
| 313 | - } | |
| 314 | - | |
| 315 | - /** | |
| 316 | -	 * {@inheritdoc} | |
| 317 | - */ | |
| 318 | - public function withUserInfo($username, $password = null) | |
| 319 | -	{ | |
| 320 | - $result = clone $this; | |
| 321 | - | |
| 322 | - return $result->setUserInfo($username, $password); | |
| 323 | - } | |
| 324 | - | |
| 325 | - /** | |
| 326 | -	 * {@inheritdoc} | |
| 327 | - */ | |
| 328 | - public function getHost() | |
| 329 | -	{ | |
| 330 | - return $this->host; | |
| 331 | - } | |
| 332 | - | |
| 333 | - /** | |
| 334 | - * Set the host. | |
| 335 | - * | |
| 336 | - * @param string $host | |
| 337 | - * @return $this | |
| 338 | - */ | |
| 339 | - private function setHost($host) | |
| 340 | -	{ | |
| 341 | - $this->host = strtolower($host); | |
| 342 | - | |
| 343 | - return $this; | |
| 344 | - } | |
| 345 | - | |
| 346 | - /** | |
| 347 | -	 * {@inheritdoc} | |
| 348 | - */ | |
| 349 | - public function withHost($host) | |
| 350 | -	{ | |
| 351 | - $result = clone $this; | |
| 352 | - | |
| 353 | - return $result->setHost($host); | |
| 354 | - } | |
| 355 | - | |
| 356 | - /** | |
| 357 | -	 * {@inheritdoc} | |
| 358 | - */ | |
| 359 | - public function getPort() | |
| 360 | -	{ | |
| 361 | - return $this->port; | |
| 362 | - } | |
| 363 | - | |
| 364 | - /** | |
| 365 | - * Set the port. | |
| 366 | - * | |
| 367 | - * @param int|null $port | |
| 368 | - * @return $this | |
| 369 | - * @throws \InvalidArgumentException | |
| 370 | - */ | |
| 371 | - private function setPort($port = null) | |
| 372 | -	{ | |
| 373 | -		if ($port !== null && (1 > $port || 0xffff < $port)) { | |
| 374 | -			throw new \InvalidArgumentException('Invalid port'); | |
| 375 | - } | |
| 376 | - | |
| 377 | - $this->port = $port; | |
| 378 | - | |
| 379 | - return $this; | |
| 380 | - } | |
| 381 | - | |
| 382 | - /** | |
| 383 | -	 * {@inheritdoc} | |
| 384 | - */ | |
| 385 | - public function withPort($port) | |
| 386 | -	{ | |
| 387 | - $result = clone $this; | |
| 388 | - | |
| 389 | - return $result->setPort($port); | |
| 390 | - } | |
| 391 | - | |
| 392 | - /** | |
| 393 | -	 * {@inheritdoc} | |
| 394 | - */ | |
| 395 | - public function getPath() | |
| 396 | -	{ | |
| 397 | - $result = $this->getDirectory(); | |
| 398 | - | |
| 399 | -		if ($result !== '' && substr($result, -1) !== static::DELIMITER_PATH && $this->getFile()) { | |
| 400 | - $result .= static::DELIMITER_PATH; | |
| 401 | - } | |
| 402 | - | |
| 403 | - return $result . $this->getFile(); | |
| 404 | - } | |
| 405 | - | |
| 406 | - /** | |
| 407 | - * Set the path. | |
| 408 | - * | |
| 409 | - * @param string $path | |
| 410 | - * @return $this | |
| 411 | - */ | |
| 412 | - private function setPath($path) | |
| 413 | -	{ | |
| 414 | - $directory = dirname($path); | |
| 415 | - $file = basename($path); | |
| 416 | - | |
| 417 | - // If dirname is '.'. Then remove it. | |
| 418 | -		if ($directory === '.') { | |
| 419 | - $directory = ''; | |
| 420 | - } | |
| 421 | - | |
| 422 | - // If the path ends with '/'. Then there is no file. | |
| 423 | -		if (substr($path, -1) === static::DELIMITER_PATH) { | |
| 424 | - $directory = $path; | |
| 425 | - $file = ''; | |
| 426 | - } | |
| 427 | - | |
| 428 | - // If the dirname and basename are both set. Then add the missing '/'. | |
| 429 | -		if (substr($directory, -1) !== static::DELIMITER_PATH && $directory !== '' && $file !== '') { | |
| 430 | - $directory .= static::DELIMITER_PATH; | |
| 431 | - } | |
| 432 | - | |
| 433 | - $this->setDirectory($directory); | |
| 434 | - $this->setFile($file); | |
| 435 | - | |
| 436 | - return $this; | |
| 437 | - } | |
| 438 | - | |
| 439 | - /** | |
| 440 | -	 * {@inheritdoc} | |
| 441 | - */ | |
| 442 | - public function withPath($path) | |
| 443 | -	{ | |
| 444 | - $result = clone $this; | |
| 445 | - | |
| 446 | - return $result->setPath($path); | |
| 447 | - } | |
| 448 | - | |
| 449 | - /** | |
| 450 | - * Returns the URI segements | |
| 451 | - * | |
| 452 | - * @return string[] the URI segments | |
| 453 | - */ | |
| 454 | - public function getSegments() | |
| 455 | -	{ | |
| 456 | - // array_values reindexes the array and array_diff removes the empty elements. | |
| 457 | - return array_values(array_diff(explode(static::DELIMITER_PATH, $this->getPath()), [''])); | |
| 458 | - } | |
| 459 | - | |
| 460 | - /** | |
| 461 | - * Returns the segment at the given index or null if the segment at the given index doesn't exists. | |
| 462 | - * | |
| 463 | - * @param int $index | |
| 464 | - * @return string|null the segment at the given index or null if the segment at the given index doesn't exists | |
| 465 | - */ | |
| 466 | - public function getSegment($index) | |
| 467 | -	{ | |
| 468 | - $result = $this->getSegments(); | |
| 469 | - | |
| 470 | - return isset($result[$index]) ? $result[$index] : null; | |
| 471 | - } | |
| 472 | - | |
| 473 | - /** | |
| 474 | - * Returns the directory. | |
| 475 | - * | |
| 476 | - * @return string the directory. | |
| 477 | - */ | |
| 478 | - public function getDirectory() | |
| 479 | -	{ | |
| 480 | - return $this->directory; | |
| 481 | - } | |
| 482 | - | |
| 483 | - /** | |
| 484 | - * Set the directory. | |
| 485 | - * | |
| 486 | - * @param string $directory | |
| 487 | - * @return $this | |
| 488 | - */ | |
| 489 | - private function setDirectory($directory) | |
| 490 | -	{ | |
| 491 | - $this->directory = $directory; | |
| 492 | - | |
| 493 | - return $this; | |
| 494 | - } | |
| 495 | - | |
| 496 | - /** | |
| 497 | - * Return an instance with the specified directory. | |
| 498 | - * | |
| 499 | - * @param string $directory | |
| 500 | - * @return self | |
| 501 | - */ | |
| 502 | - public function withDirectory($directory) | |
| 503 | -	{ | |
| 504 | - $result = clone $this; | |
| 505 | - | |
| 506 | - return $result->setDirectory($directory); | |
| 507 | - } | |
| 508 | - | |
| 509 | - /** | |
| 510 | - * Returns the file. | |
| 511 | - * | |
| 512 | - * @return string the file. | |
| 513 | - */ | |
| 514 | - public function getFile() | |
| 515 | -	{ | |
| 516 | - return $this->file; | |
| 517 | - } | |
| 518 | - | |
| 519 | - /** | |
| 520 | - * Set the file. | |
| 521 | - * | |
| 522 | - * @param string $file | |
| 523 | - * @return $this | |
| 524 | - */ | |
| 525 | - private function setFile($file) | |
| 526 | -	{ | |
| 527 | - $this->file = $file; | |
| 528 | - | |
| 529 | - return $this; | |
| 530 | - } | |
| 531 | - | |
| 532 | - /** | |
| 533 | - * Return an instance with the specified file. | |
| 534 | - * | |
| 535 | - * @param string $file | |
| 536 | - * @return self | |
| 537 | - */ | |
| 538 | - public function withFile($file) | |
| 539 | -	{ | |
| 540 | - $result = clone $this; | |
| 541 | - | |
| 542 | - return $result->setFile($file); | |
| 543 | - } | |
| 544 | - | |
| 545 | - /** | |
| 546 | -	 * {@inheritdoc} | |
| 547 | - */ | |
| 548 | - public function getQuery() | |
| 549 | -	{ | |
| 550 | - return http_build_query($this->query); | |
| 551 | - } | |
| 552 | - | |
| 553 | - /** | |
| 554 | - * Set the query. | |
| 555 | - * | |
| 556 | - * @param string $query | |
| 557 | - * @return $this | |
| 558 | - */ | |
| 559 | - private function setQuery($query) | |
| 560 | -	{ | |
| 561 | - $this->query = []; | |
| 562 | - | |
| 563 | - parse_str($query, $this->query); | |
| 564 | - | |
| 565 | - return $this; | |
| 566 | - } | |
| 567 | - | |
| 568 | - /** | |
| 569 | -	 * {@inheritdoc} | |
| 570 | - */ | |
| 571 | - public function withQuery($query) | |
| 572 | -	{ | |
| 573 | - $result = clone $this; | |
| 574 | - | |
| 575 | - return $result->setQuery($query); | |
| 576 | - } | |
| 577 | - | |
| 578 | - /** | |
| 579 | - * Returns the value to which the specified key is mapped, or null if the query map contains no mapping for the key. | |
| 580 | - * | |
| 581 | - * @param string $key | |
| 582 | - * @return string the value to which the specified key is mapped, or null if the query map contains no mapping for the key. | |
| 583 | - */ | |
| 584 | - public function getQueryValue($key) | |
| 585 | -	{ | |
| 586 | - return isset($this->query[$key]) ? $this->query[$key] : null; | |
| 587 | - } | |
| 588 | - | |
| 589 | - /** | |
| 590 | - * Associates the specified value with the specified key in the query map. | |
| 591 | - * | |
| 592 | - * @param string $key | |
| 593 | - * @param string $value | |
| 594 | - * @return $this | |
| 595 | - */ | |
| 596 | - private function setQueryValue($key, $value) | |
| 597 | -	{ | |
| 598 | - $this->query[$key] = $value; | |
| 599 | - | |
| 600 | - return $this; | |
| 601 | - } | |
| 602 | - | |
| 603 | - /** | |
| 604 | - * Return an instance with the specified query value. | |
| 605 | - * | |
| 606 | - * @param string $key | |
| 607 | - * @param string $value | |
| 608 | - * @return self | |
| 609 | - */ | |
| 610 | - public function withQueryValue($key, $value) | |
| 611 | -	{ | |
| 612 | - $result = clone $this; | |
| 613 | - | |
| 614 | - return $result->setQueryValue($key, $value); | |
| 615 | - } | |
| 616 | - | |
| 617 | - /** | |
| 618 | -	 * {@inheritdoc} | |
| 619 | - */ | |
| 620 | - public function getFragment() | |
| 621 | -	{ | |
| 622 | - return $this->fragment; | |
| 623 | - } | |
| 624 | - | |
| 625 | - /** | |
| 626 | - * Set the fragment. | |
| 627 | - * | |
| 628 | - * @param string $fragment | |
| 629 | - * @return $this | |
| 630 | - */ | |
| 631 | - private function setFragment($fragment) | |
| 632 | -	{ | |
| 633 | - $this->fragment = $fragment; | |
| 634 | - | |
| 635 | - return $this; | |
| 636 | - } | |
| 637 | - | |
| 638 | - /** | |
| 639 | -	 * {@inheritdoc} | |
| 640 | - */ | |
| 641 | - public function withFragment($fragment) | |
| 642 | -	{ | |
| 643 | - $result = clone $this; | |
| 644 | - | |
| 645 | - return $result->setFragment($fragment); | |
| 646 | - } | |
| 647 | - | |
| 648 | - /** | |
| 649 | - * Returns an instance with the decoded URI. | |
| 650 | - * | |
| 651 | - * @return self | |
| 652 | - */ | |
| 653 | - public function decode() | |
| 654 | -	{ | |
| 655 | - return new URI(html_entity_decode($this)); | |
| 656 | - } | |
| 657 | - | |
| 658 | - /** | |
| 659 | - * Returns an instance with the encoded URI. | |
| 660 | - * | |
| 661 | - * @return self | |
| 662 | - */ | |
| 663 | - public function encode() | |
| 664 | -	{ | |
| 665 | - return new URI(htmlentities($this)); | |
| 666 | - } | |
| 233 | +                if ($fragment) { | |
| 234 | + $result .= static::DELIMITER_FRAGMENT . $fragment; | |
| 235 | + } | |
| 236 | + | |
| 237 | + // no break | |
| 238 | + } | |
| 239 | + | |
| 240 | + return $result; | |
| 241 | + } | |
| 242 | + | |
| 243 | + /** | |
| 244 | +     * {@inheritdoc} | |
| 245 | + */ | |
| 246 | + public function getScheme() | |
| 247 | +    { | |
| 248 | + return $this->scheme; | |
| 249 | + } | |
| 250 | + | |
| 251 | + /** | |
| 252 | + * Set the scheme. | |
| 253 | + * | |
| 254 | + * @param string $scheme | |
| 255 | + * @return $this | |
| 256 | + */ | |
| 257 | + private function setScheme($scheme) | |
| 258 | +    { | |
| 259 | + $this->scheme = strtolower($scheme); | |
| 260 | + | |
| 261 | + return $this; | |
| 262 | + } | |
| 263 | + | |
| 264 | + /** | |
| 265 | +     * {@inheritdoc} | |
| 266 | + */ | |
| 267 | + public function withScheme($scheme) | |
| 268 | +    { | |
| 269 | + $result = clone $this; | |
| 270 | + | |
| 271 | + return $result->setScheme($scheme); | |
| 272 | + } | |
| 273 | + | |
| 274 | + /** | |
| 275 | +     * {@inheritdoc} | |
| 276 | + */ | |
| 277 | + public function getAuthority() | |
| 278 | +    { | |
| 279 | +        if (!$this->getHost()) { | |
| 280 | + return ''; | |
| 281 | + } | |
| 282 | + | |
| 283 | + return substr($this->getUri(self::USERNAME, self::PORT), 2); | |
| 284 | + } | |
| 285 | + | |
| 286 | + /** | |
| 287 | +     * {@inheritdoc} | |
| 288 | + */ | |
| 289 | + public function getUserInfo() | |
| 290 | +    { | |
| 291 | + $result = $this->username; | |
| 292 | + | |
| 293 | +        if ($this->password !== null) { | |
| 294 | + $result .= static::DELIMITER_PASSWORD . $this->password; | |
| 295 | + } | |
| 296 | + | |
| 297 | + return $result; | |
| 298 | + } | |
| 299 | + | |
| 300 | + /** | |
| 301 | + * Set the user info. | |
| 302 | + * | |
| 303 | + * @param string $username | |
| 304 | + * @param string|null $password = null | |
| 305 | + * @return $this | |
| 306 | + */ | |
| 307 | + private function setUserInfo($username, $password = null) | |
| 308 | +    { | |
| 309 | + $this->username = $username; | |
| 310 | + $this->password = $password; | |
| 311 | + | |
| 312 | + return $this; | |
| 313 | + } | |
| 314 | + | |
| 315 | + /** | |
| 316 | +     * {@inheritdoc} | |
| 317 | + */ | |
| 318 | + public function withUserInfo($username, $password = null) | |
| 319 | +    { | |
| 320 | + $result = clone $this; | |
| 321 | + | |
| 322 | + return $result->setUserInfo($username, $password); | |
| 323 | + } | |
| 324 | + | |
| 325 | + /** | |
| 326 | +     * {@inheritdoc} | |
| 327 | + */ | |
| 328 | + public function getHost() | |
| 329 | +    { | |
| 330 | + return $this->host; | |
| 331 | + } | |
| 332 | + | |
| 333 | + /** | |
| 334 | + * Set the host. | |
| 335 | + * | |
| 336 | + * @param string $host | |
| 337 | + * @return $this | |
| 338 | + */ | |
| 339 | + private function setHost($host) | |
| 340 | +    { | |
| 341 | + $this->host = strtolower($host); | |
| 342 | + | |
| 343 | + return $this; | |
| 344 | + } | |
| 345 | + | |
| 346 | + /** | |
| 347 | +     * {@inheritdoc} | |
| 348 | + */ | |
| 349 | + public function withHost($host) | |
| 350 | +    { | |
| 351 | + $result = clone $this; | |
| 352 | + | |
| 353 | + return $result->setHost($host); | |
| 354 | + } | |
| 355 | + | |
| 356 | + /** | |
| 357 | +     * {@inheritdoc} | |
| 358 | + */ | |
| 359 | + public function getPort() | |
| 360 | +    { | |
| 361 | + return $this->port; | |
| 362 | + } | |
| 363 | + | |
| 364 | + /** | |
| 365 | + * Set the port. | |
| 366 | + * | |
| 367 | + * @param int|null $port | |
| 368 | + * @return $this | |
| 369 | + * @throws \InvalidArgumentException | |
| 370 | + */ | |
| 371 | + private function setPort($port = null) | |
| 372 | +    { | |
| 373 | +        if ($port !== null && (1 > $port || 0xffff < $port)) { | |
| 374 | +            throw new \InvalidArgumentException('Invalid port'); | |
| 375 | + } | |
| 376 | + | |
| 377 | + $this->port = $port; | |
| 378 | + | |
| 379 | + return $this; | |
| 380 | + } | |
| 381 | + | |
| 382 | + /** | |
| 383 | +     * {@inheritdoc} | |
| 384 | + */ | |
| 385 | + public function withPort($port) | |
| 386 | +    { | |
| 387 | + $result = clone $this; | |
| 388 | + | |
| 389 | + return $result->setPort($port); | |
| 390 | + } | |
| 391 | + | |
| 392 | + /** | |
| 393 | +     * {@inheritdoc} | |
| 394 | + */ | |
| 395 | + public function getPath() | |
| 396 | +    { | |
| 397 | + $result = $this->getDirectory(); | |
| 398 | + | |
| 399 | +        if ($result !== '' && substr($result, -1) !== static::DELIMITER_PATH && $this->getFile()) { | |
| 400 | + $result .= static::DELIMITER_PATH; | |
| 401 | + } | |
| 402 | + | |
| 403 | + return $result . $this->getFile(); | |
| 404 | + } | |
| 405 | + | |
| 406 | + /** | |
| 407 | + * Set the path. | |
| 408 | + * | |
| 409 | + * @param string $path | |
| 410 | + * @return $this | |
| 411 | + */ | |
| 412 | + private function setPath($path) | |
| 413 | +    { | |
| 414 | + $directory = dirname($path); | |
| 415 | + $file = basename($path); | |
| 416 | + | |
| 417 | + // If dirname is '.'. Then remove it. | |
| 418 | +        if ($directory === '.') { | |
| 419 | + $directory = ''; | |
| 420 | + } | |
| 421 | + | |
| 422 | + // If the path ends with '/'. Then there is no file. | |
| 423 | +        if (substr($path, -1) === static::DELIMITER_PATH) { | |
| 424 | + $directory = $path; | |
| 425 | + $file = ''; | |
| 426 | + } | |
| 427 | + | |
| 428 | + // If the dirname and basename are both set. Then add the missing '/'. | |
| 429 | +        if (substr($directory, -1) !== static::DELIMITER_PATH && $directory !== '' && $file !== '') { | |
| 430 | + $directory .= static::DELIMITER_PATH; | |
| 431 | + } | |
| 432 | + | |
| 433 | + $this->setDirectory($directory); | |
| 434 | + $this->setFile($file); | |
| 435 | + | |
| 436 | + return $this; | |
| 437 | + } | |
| 438 | + | |
| 439 | + /** | |
| 440 | +     * {@inheritdoc} | |
| 441 | + */ | |
| 442 | + public function withPath($path) | |
| 443 | +    { | |
| 444 | + $result = clone $this; | |
| 445 | + | |
| 446 | + return $result->setPath($path); | |
| 447 | + } | |
| 448 | + | |
| 449 | + /** | |
| 450 | + * Returns the URI segements | |
| 451 | + * | |
| 452 | + * @return string[] the URI segments | |
| 453 | + */ | |
| 454 | + public function getSegments() | |
| 455 | +    { | |
| 456 | + // array_values reindexes the array and array_diff removes the empty elements. | |
| 457 | + return array_values(array_diff(explode(static::DELIMITER_PATH, $this->getPath()), [''])); | |
| 458 | + } | |
| 459 | + | |
| 460 | + /** | |
| 461 | + * Returns the segment at the given index or null if the segment at the given index doesn't exists. | |
| 462 | + * | |
| 463 | + * @param int $index | |
| 464 | + * @return string|null the segment at the given index or null if the segment at the given index doesn't exists | |
| 465 | + */ | |
| 466 | + public function getSegment($index) | |
| 467 | +    { | |
| 468 | + $result = $this->getSegments(); | |
| 469 | + | |
| 470 | + return isset($result[$index]) ? $result[$index] : null; | |
| 471 | + } | |
| 472 | + | |
| 473 | + /** | |
| 474 | + * Returns the directory. | |
| 475 | + * | |
| 476 | + * @return string the directory. | |
| 477 | + */ | |
| 478 | + public function getDirectory() | |
| 479 | +    { | |
| 480 | + return $this->directory; | |
| 481 | + } | |
| 482 | + | |
| 483 | + /** | |
| 484 | + * Set the directory. | |
| 485 | + * | |
| 486 | + * @param string $directory | |
| 487 | + * @return $this | |
| 488 | + */ | |
| 489 | + private function setDirectory($directory) | |
| 490 | +    { | |
| 491 | + $this->directory = $directory; | |
| 492 | + | |
| 493 | + return $this; | |
| 494 | + } | |
| 495 | + | |
| 496 | + /** | |
| 497 | + * Return an instance with the specified directory. | |
| 498 | + * | |
| 499 | + * @param string $directory | |
| 500 | + * @return self | |
| 501 | + */ | |
| 502 | + public function withDirectory($directory) | |
| 503 | +    { | |
| 504 | + $result = clone $this; | |
| 505 | + | |
| 506 | + return $result->setDirectory($directory); | |
| 507 | + } | |
| 508 | + | |
| 509 | + /** | |
| 510 | + * Returns the file. | |
| 511 | + * | |
| 512 | + * @return string the file. | |
| 513 | + */ | |
| 514 | + public function getFile() | |
| 515 | +    { | |
| 516 | + return $this->file; | |
| 517 | + } | |
| 518 | + | |
| 519 | + /** | |
| 520 | + * Set the file. | |
| 521 | + * | |
| 522 | + * @param string $file | |
| 523 | + * @return $this | |
| 524 | + */ | |
| 525 | + private function setFile($file) | |
| 526 | +    { | |
| 527 | + $this->file = $file; | |
| 528 | + | |
| 529 | + return $this; | |
| 530 | + } | |
| 531 | + | |
| 532 | + /** | |
| 533 | + * Return an instance with the specified file. | |
| 534 | + * | |
| 535 | + * @param string $file | |
| 536 | + * @return self | |
| 537 | + */ | |
| 538 | + public function withFile($file) | |
| 539 | +    { | |
| 540 | + $result = clone $this; | |
| 541 | + | |
| 542 | + return $result->setFile($file); | |
| 543 | + } | |
| 544 | + | |
| 545 | + /** | |
| 546 | +     * {@inheritdoc} | |
| 547 | + */ | |
| 548 | + public function getQuery() | |
| 549 | +    { | |
| 550 | + return http_build_query($this->query); | |
| 551 | + } | |
| 552 | + | |
| 553 | + /** | |
| 554 | + * Set the query. | |
| 555 | + * | |
| 556 | + * @param string $query | |
| 557 | + * @return $this | |
| 558 | + */ | |
| 559 | + private function setQuery($query) | |
| 560 | +    { | |
| 561 | + $this->query = []; | |
| 562 | + | |
| 563 | + parse_str($query, $this->query); | |
| 564 | + | |
| 565 | + return $this; | |
| 566 | + } | |
| 567 | + | |
| 568 | + /** | |
| 569 | +     * {@inheritdoc} | |
| 570 | + */ | |
| 571 | + public function withQuery($query) | |
| 572 | +    { | |
| 573 | + $result = clone $this; | |
| 574 | + | |
| 575 | + return $result->setQuery($query); | |
| 576 | + } | |
| 577 | + | |
| 578 | + /** | |
| 579 | + * Returns the value to which the specified key is mapped, or null if the query map contains no mapping for the key. | |
| 580 | + * | |
| 581 | + * @param string $key | |
| 582 | + * @return string the value to which the specified key is mapped, or null if the query map contains no mapping for the key. | |
| 583 | + */ | |
| 584 | + public function getQueryValue($key) | |
| 585 | +    { | |
| 586 | + return isset($this->query[$key]) ? $this->query[$key] : null; | |
| 587 | + } | |
| 588 | + | |
| 589 | + /** | |
| 590 | + * Associates the specified value with the specified key in the query map. | |
| 591 | + * | |
| 592 | + * @param string $key | |
| 593 | + * @param string $value | |
| 594 | + * @return $this | |
| 595 | + */ | |
| 596 | + private function setQueryValue($key, $value) | |
| 597 | +    { | |
| 598 | + $this->query[$key] = $value; | |
| 599 | + | |
| 600 | + return $this; | |
| 601 | + } | |
| 602 | + | |
| 603 | + /** | |
| 604 | + * Return an instance with the specified query value. | |
| 605 | + * | |
| 606 | + * @param string $key | |
| 607 | + * @param string $value | |
| 608 | + * @return self | |
| 609 | + */ | |
| 610 | + public function withQueryValue($key, $value) | |
| 611 | +    { | |
| 612 | + $result = clone $this; | |
| 613 | + | |
| 614 | + return $result->setQueryValue($key, $value); | |
| 615 | + } | |
| 616 | + | |
| 617 | + /** | |
| 618 | +     * {@inheritdoc} | |
| 619 | + */ | |
| 620 | + public function getFragment() | |
| 621 | +    { | |
| 622 | + return $this->fragment; | |
| 623 | + } | |
| 624 | + | |
| 625 | + /** | |
| 626 | + * Set the fragment. | |
| 627 | + * | |
| 628 | + * @param string $fragment | |
| 629 | + * @return $this | |
| 630 | + */ | |
| 631 | + private function setFragment($fragment) | |
| 632 | +    { | |
| 633 | + $this->fragment = $fragment; | |
| 634 | + | |
| 635 | + return $this; | |
| 636 | + } | |
| 637 | + | |
| 638 | + /** | |
| 639 | +     * {@inheritdoc} | |
| 640 | + */ | |
| 641 | + public function withFragment($fragment) | |
| 642 | +    { | |
| 643 | + $result = clone $this; | |
| 644 | + | |
| 645 | + return $result->setFragment($fragment); | |
| 646 | + } | |
| 647 | + | |
| 648 | + /** | |
| 649 | + * Returns an instance with the decoded URI. | |
| 650 | + * | |
| 651 | + * @return self | |
| 652 | + */ | |
| 653 | + public function decode() | |
| 654 | +    { | |
| 655 | + return new URI(html_entity_decode($this)); | |
| 656 | + } | |
| 657 | + | |
| 658 | + /** | |
| 659 | + * Returns an instance with the encoded URI. | |
| 660 | + * | |
| 661 | + * @return self | |
| 662 | + */ | |
| 663 | + public function encode() | |
| 664 | +    { | |
| 665 | + return new URI(htmlentities($this)); | |
| 666 | + } | |
| 667 | 667 | } | 
| @@ -132,7 +132,7 @@ discard block | ||
| 132 | 132 | $scheme = $this->getScheme(); | 
| 133 | 133 | |
| 134 | 134 |  				if ($scheme) { | 
| 135 | - $result .= $scheme . static::DELIMITER_SCHEME; | |
| 135 | + $result .= $scheme.static::DELIMITER_SCHEME; | |
| 136 | 136 | } | 
| 137 | 137 | |
| 138 | 138 |  				if ($end === static::SCHEME) { | 
| @@ -146,7 +146,7 @@ discard block | ||
| 146 | 146 | $username = $this->getUserInfo(); | 
| 147 | 147 | |
| 148 | 148 |  				if ($username && $this->getHost()) { | 
| 149 | - $result .= static::DELIMITER_AUTHORITY . $username . static::DELIMITER_USER; | |
| 149 | + $result .= static::DELIMITER_AUTHORITY.$username.static::DELIMITER_USER; | |
| 150 | 150 | } | 
| 151 | 151 | |
| 152 | 152 |  				if ($end === static::USERNAME) { | 
| @@ -174,7 +174,7 @@ discard block | ||
| 174 | 174 | $port = $this->getPort(); | 
| 175 | 175 | |
| 176 | 176 |  				if ($port !== null && $this->getHost()) { | 
| 177 | - $result .= static::DELIMITER_PORT . $port; | |
| 177 | + $result .= static::DELIMITER_PORT.$port; | |
| 178 | 178 | } | 
| 179 | 179 | |
| 180 | 180 |  				if ($end === static::PORT || $end === static::AUTHORITY) { | 
| @@ -218,7 +218,7 @@ discard block | ||
| 218 | 218 | $query = $this->getQuery(); | 
| 219 | 219 | |
| 220 | 220 |  				if ($query) { | 
| 221 | - $result .= static::DELIMITER_QUERY . $query; | |
| 221 | + $result .= static::DELIMITER_QUERY.$query; | |
| 222 | 222 | } | 
| 223 | 223 | |
| 224 | 224 |  				if ($end === static::QUERY) { | 
| @@ -231,7 +231,7 @@ discard block | ||
| 231 | 231 | $fragment = $this->getFragment(); | 
| 232 | 232 | |
| 233 | 233 |  				if ($fragment) { | 
| 234 | - $result .= static::DELIMITER_FRAGMENT . $fragment; | |
| 234 | + $result .= static::DELIMITER_FRAGMENT.$fragment; | |
| 235 | 235 | } | 
| 236 | 236 | |
| 237 | 237 | // no break | 
| @@ -291,7 +291,7 @@ discard block | ||
| 291 | 291 | $result = $this->username; | 
| 292 | 292 | |
| 293 | 293 |  		if ($this->password !== null) { | 
| 294 | - $result .= static::DELIMITER_PASSWORD . $this->password; | |
| 294 | + $result .= static::DELIMITER_PASSWORD.$this->password; | |
| 295 | 295 | } | 
| 296 | 296 | |
| 297 | 297 | return $result; | 
| @@ -400,7 +400,7 @@ discard block | ||
| 400 | 400 | $result .= static::DELIMITER_PATH; | 
| 401 | 401 | } | 
| 402 | 402 | |
| 403 | - return $result . $this->getFile(); | |
| 403 | + return $result.$this->getFile(); | |
| 404 | 404 | } | 
| 405 | 405 | |
| 406 | 406 | /** | 
| @@ -20,86 +20,86 @@ | ||
| 20 | 20 | */ | 
| 21 | 21 | class UploadedFile implements UploadedFileInterface | 
| 22 | 22 |  { | 
| 23 | - /** @var string The name */ | |
| 24 | - private $name; | |
| 23 | + /** @var string The name */ | |
| 24 | + private $name; | |
| 25 | 25 | |
| 26 | - /** @var string The type. */ | |
| 27 | - private $type; | |
| 26 | + /** @var string The type. */ | |
| 27 | + private $type; | |
| 28 | 28 | |
| 29 | - /** @var string The tmp name. */ | |
| 30 | - private $tmpName; | |
| 29 | + /** @var string The tmp name. */ | |
| 30 | + private $tmpName; | |
| 31 | 31 | |
| 32 | - /** @var int The error. */ | |
| 33 | - private $error; | |
| 32 | + /** @var int The error. */ | |
| 33 | + private $error; | |
| 34 | 34 | |
| 35 | - /** @var int The size. */ | |
| 36 | - private $size; | |
| 35 | + /** @var int The size. */ | |
| 36 | + private $size; | |
| 37 | 37 | |
| 38 | - /** | |
| 39 | - * Construct a Stream object with the given name, type, tmp name, error and size. | |
| 40 | - * | |
| 41 | - * @param string $name | |
| 42 | - * @param string $type | |
| 43 | - * @param string $tmpName | |
| 44 | - * @param int $error | |
| 45 | - * @param int $size | |
| 46 | - */ | |
| 47 | - public function __construct($name, $type, $tmpName, $error, $size) | |
| 48 | -	{ | |
| 49 | - $this->name = $name; | |
| 50 | - $this->type = $type; | |
| 51 | - $this->tmpName = $tmpName; | |
| 52 | - $this->error = $error; | |
| 53 | - $this->size = $size; | |
| 54 | - } | |
| 38 | + /** | |
| 39 | + * Construct a Stream object with the given name, type, tmp name, error and size. | |
| 40 | + * | |
| 41 | + * @param string $name | |
| 42 | + * @param string $type | |
| 43 | + * @param string $tmpName | |
| 44 | + * @param int $error | |
| 45 | + * @param int $size | |
| 46 | + */ | |
| 47 | + public function __construct($name, $type, $tmpName, $error, $size) | |
| 48 | +    { | |
| 49 | + $this->name = $name; | |
| 50 | + $this->type = $type; | |
| 51 | + $this->tmpName = $tmpName; | |
| 52 | + $this->error = $error; | |
| 53 | + $this->size = $size; | |
| 54 | + } | |
| 55 | 55 | |
| 56 | - /** | |
| 57 | -	 * {@inheritdoc} | |
| 58 | - */ | |
| 59 | - public function getStream() | |
| 60 | -	{ | |
| 61 | - return new Stream(fopen($this->tmpName, 'r')); | |
| 62 | - } | |
| 56 | + /** | |
| 57 | +     * {@inheritdoc} | |
| 58 | + */ | |
| 59 | + public function getStream() | |
| 60 | +    { | |
| 61 | + return new Stream(fopen($this->tmpName, 'r')); | |
| 62 | + } | |
| 63 | 63 | |
| 64 | - /** | |
| 65 | -	 * {@inheritdoc} | |
| 66 | - */ | |
| 67 | - public function moveTo($targetPath) | |
| 68 | -	{ | |
| 69 | -		if ($this->getError() != UPLOAD_ERR_OK || !is_uploaded_file($this->tmpName) || !move_uploaded_file($this->tmpName, $targetPath)) { | |
| 70 | -			throw new \RuntimeException('Can\'t move the file'); | |
| 71 | - } | |
| 72 | - } | |
| 64 | + /** | |
| 65 | +     * {@inheritdoc} | |
| 66 | + */ | |
| 67 | + public function moveTo($targetPath) | |
| 68 | +    { | |
| 69 | +        if ($this->getError() != UPLOAD_ERR_OK || !is_uploaded_file($this->tmpName) || !move_uploaded_file($this->tmpName, $targetPath)) { | |
| 70 | +            throw new \RuntimeException('Can\'t move the file'); | |
| 71 | + } | |
| 72 | + } | |
| 73 | 73 | |
| 74 | - /** | |
| 75 | -	 * {@inheritdoc} | |
| 76 | - */ | |
| 77 | - public function getSize() | |
| 78 | -	{ | |
| 79 | - return $this->size; | |
| 80 | - } | |
| 74 | + /** | |
| 75 | +     * {@inheritdoc} | |
| 76 | + */ | |
| 77 | + public function getSize() | |
| 78 | +    { | |
| 79 | + return $this->size; | |
| 80 | + } | |
| 81 | 81 | |
| 82 | - /** | |
| 83 | -	 * {@inheritdoc} | |
| 84 | - */ | |
| 85 | - public function getError() | |
| 86 | -	{ | |
| 87 | - return $this->error; | |
| 88 | - } | |
| 82 | + /** | |
| 83 | +     * {@inheritdoc} | |
| 84 | + */ | |
| 85 | + public function getError() | |
| 86 | +    { | |
| 87 | + return $this->error; | |
| 88 | + } | |
| 89 | 89 | |
| 90 | - /** | |
| 91 | -	 * {@inheritdoc} | |
| 92 | - */ | |
| 93 | - public function getClientFilename() | |
| 94 | -	{ | |
| 95 | - return $this->name; | |
| 96 | - } | |
| 90 | + /** | |
| 91 | +     * {@inheritdoc} | |
| 92 | + */ | |
| 93 | + public function getClientFilename() | |
| 94 | +    { | |
| 95 | + return $this->name; | |
| 96 | + } | |
| 97 | 97 | |
| 98 | - /** | |
| 99 | -	 * {@inheritdoc} | |
| 100 | - */ | |
| 101 | - public function getClientMediaType() | |
| 102 | -	{ | |
| 103 | - return $this->type; | |
| 104 | - } | |
| 98 | + /** | |
| 99 | +     * {@inheritdoc} | |
| 100 | + */ | |
| 101 | + public function getClientMediaType() | |
| 102 | +    { | |
| 103 | + return $this->type; | |
| 104 | + } | |
| 105 | 105 | } | 
| @@ -19,61 +19,61 @@ | ||
| 19 | 19 | */ | 
| 20 | 20 | class StatusCode extends Enum | 
| 21 | 21 |  { | 
| 22 | - const INFORMATIONAL_CONTINUE = 100; | |
| 23 | - const INFORMATIONAL_SWITCHING_PROTOCOL = 101; | |
| 24 | - const INFORMATIONAL_PROGRESSING = 102; | |
| 25 | - const SUCCESFULL_OK = 200; | |
| 26 | - const SUCCESFULL_CREATED = 201; | |
| 27 | - const SUCCESFULL_ACCEPTED = 202; | |
| 28 | - const SUCCESFULL_NON_AUTORITATIVE_INFORMATION = 203; | |
| 29 | - const SUCCESFULL_NO_CONTENT = 204; | |
| 30 | - const SUCCESFULL_RESET_CONTENT = 205; | |
| 31 | - const SUCCESFULL_PARTIAL_CONTENT = 206; | |
| 32 | - const MULTI_STATUS = 207; | |
| 33 | - const ALREADY_REPORTED = 208; | |
| 34 | - const REDIRECTION_MULTIPLE_CHOICES = 300; | |
| 35 | - const REDIRECTION_MOVED_PERMANENTLY = 301; | |
| 36 | - const REDIRECTION_FOUND = 302; | |
| 37 | - const REDIRECTION_SEE_OTHER = 303; | |
| 38 | - const REDIRECTION_NOT_MODIFIED = 304; | |
| 39 | - const REDIRECTION_USE_PROXY = 305; | |
| 40 | - const SWITCH_PROXY = 306; | |
| 41 | - const REDIRECTION_TEMPORARY_REDIRECT = 307; | |
| 42 | - const ERROR_CLIENT_BAD_REQUEST = 400; | |
| 43 | - const ERROR_CLIENT_UNAUTHORIZED = 401; | |
| 44 | - const ERROR_CLIENT_PAYMENT_REQUIRED = 402; | |
| 45 | - const ERROR_CLIENT_FORBIDDEN = 403; | |
| 46 | - const ERROR_CLIENT_NOT_FOUND = 404; | |
| 47 | - const ERROR_CLIENT_METHOD_NOT_ALLOWED = 405; | |
| 48 | - const ERROR_CLIENT_NOT_ACCEPTABLE = 406; | |
| 49 | - const ERROR_CLIENT_PROXY_AUTHENTICATION_REQUIRED = 407; | |
| 50 | - const ERROR_CLIENT_REQUEST_TIME_OUT = 408; | |
| 51 | - const ERROR_CLIENT_CONFLICT = 409; | |
| 52 | - const ERROR_CLIENT_GONE = 410; | |
| 53 | - const ERROR_CLIENT_LENGTH_REQUIRED = 411; | |
| 54 | - const ERROR_CLIENT_PRECONDITION_FAILED = 412; | |
| 55 | - const ERROR_CLIENT_REQUEST_ENTITY_TOO_LARGE = 413; | |
| 56 | - const ERROR_CLIENT_REQUEST_URI_TOO_LONG = 414; | |
| 57 | - const ERROR_CLIENT_UNSUPPORTED_MEDIA = 415; | |
| 58 | - const ERROR_CLIENT_REQUEST_RANGE_NOT_SATISFIABLE = 416; | |
| 59 | - const ERROR_CLIENT_EXPECTATION_FAILED = 417; | |
| 60 | - const I_AM_A_TEAPOT = 418; | |
| 61 | - const UNPROCESSABLE_ENTITY = 422; | |
| 62 | - const LOCKED = 423; | |
| 63 | - const FAILED_DEPENDENCY = 424; | |
| 64 | - const UNORDERED_COLLECTION = 425; | |
| 65 | - const UPGRADE_REQUIRED = 426; | |
| 66 | - const PRECONDITION_REQUIRED = 428; | |
| 67 | - const TOO_MANY_REQUESTS = 429; | |
| 68 | - const REQUEST_HEADER_FIELDS_TOO_LARGE = 431; | |
| 69 | - const ERROR_SERVER_INTERNAL_ERROR_SERVER = 500; | |
| 70 | - const ERROR_SERVER_NOT_IMPLEMENTED = 501; | |
| 71 | - const ERROR_SERVER_BAD_GATEWAY = 502; | |
| 72 | - const ERROR_SERVER_SERVICE_UNAVAILABLE = 503; | |
| 73 | - const ERROR_SERVER_GATEWAY_TIMEOUT = 504; | |
| 74 | - const ERROR_SERVER_HTTP_VERSION_NOT_SUPPORTED = 505; | |
| 75 | - const VARIANT_ALSO_NEGOTIATES = 506; | |
| 76 | - const INSUFFICIENT_STORAGE = 507; | |
| 77 | - const LOOP_DETECTED = 508; | |
| 78 | - const NETWORK_AUTHENTICATION_REQUIRED = 511; | |
| 22 | + const INFORMATIONAL_CONTINUE = 100; | |
| 23 | + const INFORMATIONAL_SWITCHING_PROTOCOL = 101; | |
| 24 | + const INFORMATIONAL_PROGRESSING = 102; | |
| 25 | + const SUCCESFULL_OK = 200; | |
| 26 | + const SUCCESFULL_CREATED = 201; | |
| 27 | + const SUCCESFULL_ACCEPTED = 202; | |
| 28 | + const SUCCESFULL_NON_AUTORITATIVE_INFORMATION = 203; | |
| 29 | + const SUCCESFULL_NO_CONTENT = 204; | |
| 30 | + const SUCCESFULL_RESET_CONTENT = 205; | |
| 31 | + const SUCCESFULL_PARTIAL_CONTENT = 206; | |
| 32 | + const MULTI_STATUS = 207; | |
| 33 | + const ALREADY_REPORTED = 208; | |
| 34 | + const REDIRECTION_MULTIPLE_CHOICES = 300; | |
| 35 | + const REDIRECTION_MOVED_PERMANENTLY = 301; | |
| 36 | + const REDIRECTION_FOUND = 302; | |
| 37 | + const REDIRECTION_SEE_OTHER = 303; | |
| 38 | + const REDIRECTION_NOT_MODIFIED = 304; | |
| 39 | + const REDIRECTION_USE_PROXY = 305; | |
| 40 | + const SWITCH_PROXY = 306; | |
| 41 | + const REDIRECTION_TEMPORARY_REDIRECT = 307; | |
| 42 | + const ERROR_CLIENT_BAD_REQUEST = 400; | |
| 43 | + const ERROR_CLIENT_UNAUTHORIZED = 401; | |
| 44 | + const ERROR_CLIENT_PAYMENT_REQUIRED = 402; | |
| 45 | + const ERROR_CLIENT_FORBIDDEN = 403; | |
| 46 | + const ERROR_CLIENT_NOT_FOUND = 404; | |
| 47 | + const ERROR_CLIENT_METHOD_NOT_ALLOWED = 405; | |
| 48 | + const ERROR_CLIENT_NOT_ACCEPTABLE = 406; | |
| 49 | + const ERROR_CLIENT_PROXY_AUTHENTICATION_REQUIRED = 407; | |
| 50 | + const ERROR_CLIENT_REQUEST_TIME_OUT = 408; | |
| 51 | + const ERROR_CLIENT_CONFLICT = 409; | |
| 52 | + const ERROR_CLIENT_GONE = 410; | |
| 53 | + const ERROR_CLIENT_LENGTH_REQUIRED = 411; | |
| 54 | + const ERROR_CLIENT_PRECONDITION_FAILED = 412; | |
| 55 | + const ERROR_CLIENT_REQUEST_ENTITY_TOO_LARGE = 413; | |
| 56 | + const ERROR_CLIENT_REQUEST_URI_TOO_LONG = 414; | |
| 57 | + const ERROR_CLIENT_UNSUPPORTED_MEDIA = 415; | |
| 58 | + const ERROR_CLIENT_REQUEST_RANGE_NOT_SATISFIABLE = 416; | |
| 59 | + const ERROR_CLIENT_EXPECTATION_FAILED = 417; | |
| 60 | + const I_AM_A_TEAPOT = 418; | |
| 61 | + const UNPROCESSABLE_ENTITY = 422; | |
| 62 | + const LOCKED = 423; | |
| 63 | + const FAILED_DEPENDENCY = 424; | |
| 64 | + const UNORDERED_COLLECTION = 425; | |
| 65 | + const UPGRADE_REQUIRED = 426; | |
| 66 | + const PRECONDITION_REQUIRED = 428; | |
| 67 | + const TOO_MANY_REQUESTS = 429; | |
| 68 | + const REQUEST_HEADER_FIELDS_TOO_LARGE = 431; | |
| 69 | + const ERROR_SERVER_INTERNAL_ERROR_SERVER = 500; | |
| 70 | + const ERROR_SERVER_NOT_IMPLEMENTED = 501; | |
| 71 | + const ERROR_SERVER_BAD_GATEWAY = 502; | |
| 72 | + const ERROR_SERVER_SERVICE_UNAVAILABLE = 503; | |
| 73 | + const ERROR_SERVER_GATEWAY_TIMEOUT = 504; | |
| 74 | + const ERROR_SERVER_HTTP_VERSION_NOT_SUPPORTED = 505; | |
| 75 | + const VARIANT_ALSO_NEGOTIATES = 506; | |
| 76 | + const INSUFFICIENT_STORAGE = 507; | |
| 77 | + const LOOP_DETECTED = 508; | |
| 78 | + const NETWORK_AUTHENTICATION_REQUIRED = 511; | |
| 79 | 79 | } | 
| @@ -20,223 +20,223 @@ | ||
| 20 | 20 | */ | 
| 21 | 21 | class Stream implements StreamInterface | 
| 22 | 22 |  { | 
| 23 | - /** @var resource The resource. */ | |
| 24 | - private $resource; | |
| 25 | - | |
| 26 | - /** @var array The metadata. */ | |
| 27 | - private $metadata; | |
| 28 | - | |
| 29 | - /** @var string[] The read modes. */ | |
| 30 | - private static $readModes = ['r', 'w+', 'r+', 'x+', 'c+', 'rb', 'w+b', 'r+b', 'x+b', 'c+b', 'rt', 'w+t', 'r+t', 'x+t', 'c+t', 'a+']; | |
| 31 | - | |
| 32 | - /** @var string[] The write modes. */ | |
| 33 | - private static $writeModes = ['w', 'w+', 'rw', 'r+', 'x+', 'c+', 'wb', 'w+b', 'r+b', 'x+b', 'c+b', 'w+t', 'r+t', 'x+t', 'c+t', 'a', 'a+']; | |
| 34 | - | |
| 35 | - /** | |
| 36 | - * Construct a Stream object with the given resource. | |
| 37 | - * | |
| 38 | - * @param resource $resource | |
| 39 | - */ | |
| 40 | - public function __construct($resource) | |
| 41 | -	{ | |
| 42 | -		if (!is_resource($resource)) { | |
| 43 | -			throw new \InvalidArgumentException('Invalid resource'); | |
| 44 | - } | |
| 45 | - | |
| 46 | - $this->resource = $resource; | |
| 47 | - $this->metadata = stream_get_meta_data($resource); | |
| 48 | - } | |
| 49 | - | |
| 50 | - /** | |
| 51 | - * Destruct the Stream object. | |
| 52 | - */ | |
| 53 | - public function __destruct() | |
| 54 | -	{ | |
| 55 | - $this->close(); | |
| 56 | - } | |
| 57 | - | |
| 58 | - /** | |
| 59 | -	 * {@inheritdoc} | |
| 60 | - */ | |
| 61 | - public function __toString() | |
| 62 | -	{ | |
| 63 | -		try { | |
| 64 | - $this->seek(0); | |
| 65 | - | |
| 66 | - return $this->getContents(); | |
| 67 | -		} catch (\Exception $e) { | |
| 68 | - return ''; | |
| 69 | - } | |
| 70 | - } | |
| 71 | - | |
| 72 | - /** | |
| 73 | -	 * {@inheritdoc} | |
| 74 | - */ | |
| 75 | - public function close() | |
| 76 | -	{ | |
| 77 | -		if (isset($this->resource)) { | |
| 78 | -			if (is_resource($this->resource)) { | |
| 79 | - fclose($this->resource); | |
| 80 | - } | |
| 81 | - | |
| 82 | - $this->detach(); | |
| 83 | - } | |
| 84 | - } | |
| 85 | - | |
| 86 | - /** | |
| 87 | -	 * {@inheritdoc} | |
| 88 | - */ | |
| 89 | - public function detach() | |
| 90 | -	{ | |
| 91 | -		if (!isset($this->resource)) { | |
| 92 | - return null; | |
| 93 | - } | |
| 94 | - | |
| 95 | - $result = $this->resource; | |
| 96 | - unset($this->resource); | |
| 97 | - | |
| 98 | - return $result; | |
| 99 | - } | |
| 100 | - | |
| 101 | - /** | |
| 102 | -	 * {@inheritdoc} | |
| 103 | - */ | |
| 104 | - public function getSize() | |
| 105 | -	{ | |
| 106 | -		if (!isset($this->resource)) { | |
| 107 | - return null; | |
| 108 | - } | |
| 109 | - | |
| 110 | -		if ($this->getMetadata('uri')) { | |
| 111 | -			clearstatcache(true, $this->getMetadata('uri')); | |
| 112 | - } | |
| 113 | - | |
| 114 | - $stats = fstat($this->resource); | |
| 115 | - | |
| 116 | - return isset($stats['size']) ? $stats['size'] : null; | |
| 117 | - } | |
| 118 | - | |
| 119 | - /** | |
| 120 | -	 * {@inheritdoc} | |
| 121 | - */ | |
| 122 | - public function tell() | |
| 123 | -	{ | |
| 124 | - $result = ftell($this->resource); | |
| 125 | - | |
| 126 | -		if ($result === false) { | |
| 127 | -			throw new \RuntimeException('Error while getting the position of the pointer'); | |
| 128 | - } | |
| 129 | - | |
| 130 | - return $result; | |
| 131 | - } | |
| 132 | - | |
| 133 | - /** | |
| 134 | -	 * {@inheritdoc} | |
| 135 | - */ | |
| 136 | - public function eof() | |
| 137 | -	{ | |
| 138 | - return isset($this->resource) && feof($this->resource); | |
| 139 | - } | |
| 140 | - | |
| 141 | - /** | |
| 142 | -	 * {@inheritdoc} | |
| 143 | - */ | |
| 144 | - public function isSeekable() | |
| 145 | -	{ | |
| 146 | -		return isset($this->resource) && $this->getMetadata('seekable'); | |
| 147 | - } | |
| 148 | - | |
| 149 | - /** | |
| 150 | -	 * {@inheritdoc} | |
| 151 | - */ | |
| 152 | - public function seek($offset, $whence = SEEK_SET) | |
| 153 | -	{ | |
| 154 | -		if (!$this->isSeekable()) { | |
| 155 | -			throw new \RuntimeException('Stream is not seekable'); | |
| 156 | - } | |
| 157 | - | |
| 158 | -		if (fseek($this->resource, $offset, $whence) === false) { | |
| 159 | -			throw new \RuntimeException('Error while seeking the stream'); | |
| 160 | - } | |
| 161 | - } | |
| 162 | - | |
| 163 | - /** | |
| 164 | -	 * {@inheritdoc} | |
| 165 | - */ | |
| 166 | - public function rewind() | |
| 167 | -	{ | |
| 168 | - $this->seek(0); | |
| 169 | - } | |
| 170 | - | |
| 171 | - /** | |
| 172 | -	 * {@inheritdoc} | |
| 173 | - */ | |
| 174 | - public function isWritable() | |
| 175 | -	{ | |
| 176 | -		return isset($this->resource) && in_array($this->getMetadata('mode'), self::$writeModes); | |
| 177 | - } | |
| 178 | - | |
| 179 | - /** | |
| 180 | -	 * {@inheritdoc} | |
| 181 | - */ | |
| 182 | - public function write($string) | |
| 183 | -	{ | |
| 184 | -		if (!$this->isWritable()) { | |
| 185 | -			throw new \RuntimeException('Stream is not writable'); | |
| 186 | - } | |
| 187 | - | |
| 188 | - $result = fwrite($this->resource, $string); | |
| 189 | - | |
| 190 | -		if ($result === false) { | |
| 191 | -			throw new \RuntimeException('Error while writing the stream'); | |
| 192 | - } | |
| 193 | - | |
| 194 | - return $result; | |
| 195 | - } | |
| 196 | - | |
| 197 | - /** | |
| 198 | -	 * {@inheritdoc} | |
| 199 | - */ | |
| 200 | - public function isReadable() | |
| 201 | -	{ | |
| 202 | -		return isset($this->resource) && in_array($this->getMetadata('mode'), self::$readModes); | |
| 203 | - } | |
| 204 | - | |
| 205 | - /** | |
| 206 | -	 * {@inheritdoc} | |
| 207 | - */ | |
| 208 | - public function read($length) | |
| 209 | -	{ | |
| 210 | -		if (!$this->isReadable()) { | |
| 211 | -			throw new \RuntimeException('Stream is not readable'); | |
| 212 | - } | |
| 213 | - | |
| 214 | - $result = stream_get_contents($this->resource, $length); | |
| 215 | - | |
| 216 | -		if ($result === false) { | |
| 217 | -			throw new \RuntimeException('Error while reading the stream'); | |
| 218 | - } | |
| 219 | - | |
| 220 | - return $result; | |
| 221 | - } | |
| 222 | - | |
| 223 | - /** | |
| 224 | -	 * {@inheritdoc} | |
| 225 | - */ | |
| 226 | - public function getContents() | |
| 227 | -	{ | |
| 228 | - return $this->read(-1); | |
| 229 | - } | |
| 230 | - | |
| 231 | - /** | |
| 232 | -	 * {@inheritdoc} | |
| 233 | - */ | |
| 234 | - public function getMetadata($key = null) | |
| 235 | -	{ | |
| 236 | -		if ($key === null) { | |
| 237 | - return $this->metadata; | |
| 238 | - } | |
| 239 | - | |
| 240 | - return isset($this->metadata[$key]) ? $this->metadata[$key] : null; | |
| 241 | - } | |
| 23 | + /** @var resource The resource. */ | |
| 24 | + private $resource; | |
| 25 | + | |
| 26 | + /** @var array The metadata. */ | |
| 27 | + private $metadata; | |
| 28 | + | |
| 29 | + /** @var string[] The read modes. */ | |
| 30 | + private static $readModes = ['r', 'w+', 'r+', 'x+', 'c+', 'rb', 'w+b', 'r+b', 'x+b', 'c+b', 'rt', 'w+t', 'r+t', 'x+t', 'c+t', 'a+']; | |
| 31 | + | |
| 32 | + /** @var string[] The write modes. */ | |
| 33 | + private static $writeModes = ['w', 'w+', 'rw', 'r+', 'x+', 'c+', 'wb', 'w+b', 'r+b', 'x+b', 'c+b', 'w+t', 'r+t', 'x+t', 'c+t', 'a', 'a+']; | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * Construct a Stream object with the given resource. | |
| 37 | + * | |
| 38 | + * @param resource $resource | |
| 39 | + */ | |
| 40 | + public function __construct($resource) | |
| 41 | +    { | |
| 42 | +        if (!is_resource($resource)) { | |
| 43 | +            throw new \InvalidArgumentException('Invalid resource'); | |
| 44 | + } | |
| 45 | + | |
| 46 | + $this->resource = $resource; | |
| 47 | + $this->metadata = stream_get_meta_data($resource); | |
| 48 | + } | |
| 49 | + | |
| 50 | + /** | |
| 51 | + * Destruct the Stream object. | |
| 52 | + */ | |
| 53 | + public function __destruct() | |
| 54 | +    { | |
| 55 | + $this->close(); | |
| 56 | + } | |
| 57 | + | |
| 58 | + /** | |
| 59 | +     * {@inheritdoc} | |
| 60 | + */ | |
| 61 | + public function __toString() | |
| 62 | +    { | |
| 63 | +        try { | |
| 64 | + $this->seek(0); | |
| 65 | + | |
| 66 | + return $this->getContents(); | |
| 67 | +        } catch (\Exception $e) { | |
| 68 | + return ''; | |
| 69 | + } | |
| 70 | + } | |
| 71 | + | |
| 72 | + /** | |
| 73 | +     * {@inheritdoc} | |
| 74 | + */ | |
| 75 | + public function close() | |
| 76 | +    { | |
| 77 | +        if (isset($this->resource)) { | |
| 78 | +            if (is_resource($this->resource)) { | |
| 79 | + fclose($this->resource); | |
| 80 | + } | |
| 81 | + | |
| 82 | + $this->detach(); | |
| 83 | + } | |
| 84 | + } | |
| 85 | + | |
| 86 | + /** | |
| 87 | +     * {@inheritdoc} | |
| 88 | + */ | |
| 89 | + public function detach() | |
| 90 | +    { | |
| 91 | +        if (!isset($this->resource)) { | |
| 92 | + return null; | |
| 93 | + } | |
| 94 | + | |
| 95 | + $result = $this->resource; | |
| 96 | + unset($this->resource); | |
| 97 | + | |
| 98 | + return $result; | |
| 99 | + } | |
| 100 | + | |
| 101 | + /** | |
| 102 | +     * {@inheritdoc} | |
| 103 | + */ | |
| 104 | + public function getSize() | |
| 105 | +    { | |
| 106 | +        if (!isset($this->resource)) { | |
| 107 | + return null; | |
| 108 | + } | |
| 109 | + | |
| 110 | +        if ($this->getMetadata('uri')) { | |
| 111 | +            clearstatcache(true, $this->getMetadata('uri')); | |
| 112 | + } | |
| 113 | + | |
| 114 | + $stats = fstat($this->resource); | |
| 115 | + | |
| 116 | + return isset($stats['size']) ? $stats['size'] : null; | |
| 117 | + } | |
| 118 | + | |
| 119 | + /** | |
| 120 | +     * {@inheritdoc} | |
| 121 | + */ | |
| 122 | + public function tell() | |
| 123 | +    { | |
| 124 | + $result = ftell($this->resource); | |
| 125 | + | |
| 126 | +        if ($result === false) { | |
| 127 | +            throw new \RuntimeException('Error while getting the position of the pointer'); | |
| 128 | + } | |
| 129 | + | |
| 130 | + return $result; | |
| 131 | + } | |
| 132 | + | |
| 133 | + /** | |
| 134 | +     * {@inheritdoc} | |
| 135 | + */ | |
| 136 | + public function eof() | |
| 137 | +    { | |
| 138 | + return isset($this->resource) && feof($this->resource); | |
| 139 | + } | |
| 140 | + | |
| 141 | + /** | |
| 142 | +     * {@inheritdoc} | |
| 143 | + */ | |
| 144 | + public function isSeekable() | |
| 145 | +    { | |
| 146 | +        return isset($this->resource) && $this->getMetadata('seekable'); | |
| 147 | + } | |
| 148 | + | |
| 149 | + /** | |
| 150 | +     * {@inheritdoc} | |
| 151 | + */ | |
| 152 | + public function seek($offset, $whence = SEEK_SET) | |
| 153 | +    { | |
| 154 | +        if (!$this->isSeekable()) { | |
| 155 | +            throw new \RuntimeException('Stream is not seekable'); | |
| 156 | + } | |
| 157 | + | |
| 158 | +        if (fseek($this->resource, $offset, $whence) === false) { | |
| 159 | +            throw new \RuntimeException('Error while seeking the stream'); | |
| 160 | + } | |
| 161 | + } | |
| 162 | + | |
| 163 | + /** | |
| 164 | +     * {@inheritdoc} | |
| 165 | + */ | |
| 166 | + public function rewind() | |
| 167 | +    { | |
| 168 | + $this->seek(0); | |
| 169 | + } | |
| 170 | + | |
| 171 | + /** | |
| 172 | +     * {@inheritdoc} | |
| 173 | + */ | |
| 174 | + public function isWritable() | |
| 175 | +    { | |
| 176 | +        return isset($this->resource) && in_array($this->getMetadata('mode'), self::$writeModes); | |
| 177 | + } | |
| 178 | + | |
| 179 | + /** | |
| 180 | +     * {@inheritdoc} | |
| 181 | + */ | |
| 182 | + public function write($string) | |
| 183 | +    { | |
| 184 | +        if (!$this->isWritable()) { | |
| 185 | +            throw new \RuntimeException('Stream is not writable'); | |
| 186 | + } | |
| 187 | + | |
| 188 | + $result = fwrite($this->resource, $string); | |
| 189 | + | |
| 190 | +        if ($result === false) { | |
| 191 | +            throw new \RuntimeException('Error while writing the stream'); | |
| 192 | + } | |
| 193 | + | |
| 194 | + return $result; | |
| 195 | + } | |
| 196 | + | |
| 197 | + /** | |
| 198 | +     * {@inheritdoc} | |
| 199 | + */ | |
| 200 | + public function isReadable() | |
| 201 | +    { | |
| 202 | +        return isset($this->resource) && in_array($this->getMetadata('mode'), self::$readModes); | |
| 203 | + } | |
| 204 | + | |
| 205 | + /** | |
| 206 | +     * {@inheritdoc} | |
| 207 | + */ | |
| 208 | + public function read($length) | |
| 209 | +    { | |
| 210 | +        if (!$this->isReadable()) { | |
| 211 | +            throw new \RuntimeException('Stream is not readable'); | |
| 212 | + } | |
| 213 | + | |
| 214 | + $result = stream_get_contents($this->resource, $length); | |
| 215 | + | |
| 216 | +        if ($result === false) { | |
| 217 | +            throw new \RuntimeException('Error while reading the stream'); | |
| 218 | + } | |
| 219 | + | |
| 220 | + return $result; | |
| 221 | + } | |
| 222 | + | |
| 223 | + /** | |
| 224 | +     * {@inheritdoc} | |
| 225 | + */ | |
| 226 | + public function getContents() | |
| 227 | +    { | |
| 228 | + return $this->read(-1); | |
| 229 | + } | |
| 230 | + | |
| 231 | + /** | |
| 232 | +     * {@inheritdoc} | |
| 233 | + */ | |
| 234 | + public function getMetadata($key = null) | |
| 235 | +    { | |
| 236 | +        if ($key === null) { | |
| 237 | + return $this->metadata; | |
| 238 | + } | |
| 239 | + | |
| 240 | + return isset($this->metadata[$key]) ? $this->metadata[$key] : null; | |
| 241 | + } | |
| 242 | 242 | } | 
| @@ -103,10 +103,10 @@ | ||
| 103 | 103 | } | 
| 104 | 104 | |
| 105 | 105 | $scheme = isset($this->getServerParams()['HTTPS']) ? 'https://' : 'http://'; | 
| 106 | - $host = isset($this->getServerParams()['HTTP_HOST']) ? $scheme . $this->getServerParams()['HTTP_HOST'] : ''; | |
| 106 | + $host = isset($this->getServerParams()['HTTP_HOST']) ? $scheme.$this->getServerParams()['HTTP_HOST'] : ''; | |
| 107 | 107 | $path = isset($this->getServerParams()['REQUEST_URI']) ? $this->getServerParams()['REQUEST_URI'] : ''; | 
| 108 | 108 | |
| 109 | - return new URI($host . $path); | |
| 109 | + return new URI($host.$path); | |
| 110 | 110 | } | 
| 111 | 111 | |
| 112 | 112 | /** | 
| @@ -122,7 +122,6 @@ discard block | ||
| 122 | 122 | /** | 
| 123 | 123 | * Initialize the headers. | 
| 124 | 124 | * | 
| 125 | - * @param string $uri | |
| 126 | 125 | * @return array the headers. | 
| 127 | 126 | */ | 
| 128 | 127 | private function initQueryParams($serverParams) | 
| @@ -332,7 +331,7 @@ discard block | ||
| 332 | 331 | * Checks if a content type header exists with the given content type. | 
| 333 | 332 | * | 
| 334 | 333 | * @param string $contentType | 
| 335 | - * @return true if a content type header exists with the given content type. | |
| 334 | + * @return boolean if a content type header exists with the given content type. | |
| 336 | 335 | */ | 
| 337 | 336 | private function hasContentType($contentType) | 
| 338 | 337 |  	{ | 
| @@ -22,412 +22,412 @@ | ||
| 22 | 22 | */ | 
| 23 | 23 | class ServerRequest extends Request implements ServerRequestInterface | 
| 24 | 24 |  { | 
| 25 | - /** @var array The server parameters. */ | |
| 26 | - private $serverParams; | |
| 27 | - | |
| 28 | - /** @var array The cookie parameters. */ | |
| 29 | - private $cookieParams; | |
| 30 | - | |
| 31 | - /** @var array The query parameters. */ | |
| 32 | - private $queryParams; | |
| 33 | - | |
| 34 | - /** @var array The post parameters. */ | |
| 35 | - private $postParams; | |
| 36 | - | |
| 37 | - /** @var array The files parameters. */ | |
| 38 | - private $filesParams; | |
| 39 | - | |
| 40 | - /** @var array The uploaded files. */ | |
| 41 | - private $uploadedFiles; | |
| 42 | - | |
| 43 | - /** @var null|array|object The parsed body. */ | |
| 44 | - private $parsedBody; | |
| 45 | - | |
| 46 | - /** @var array The attributes. */ | |
| 47 | - private $attributes; | |
| 48 | - | |
| 49 | - /** | |
| 50 | - * Construct a Request object with the given method, uri, version, headers & body. | |
| 51 | - * | |
| 52 | - * @global array $_SERVER The server parameters. | |
| 53 | - * @global array $_COOKIE The cookie parameters. | |
| 54 | - * @global array $_GET The query parameters. | |
| 55 | - * @global array $_POST The post parameters. | |
| 56 | - * @global array $_FILES The files parameters. | |
| 57 | - * | |
| 58 | - * @param string $method = '' | |
| 59 | - * @param UriInterface|null $uri = null | |
| 60 | - * @param string $version = self::DEFAULT_VERSION | |
| 61 | - * @param array $headers = [] | |
| 62 | - * @param StreamInterface|null $body = null | |
| 63 | - */ | |
| 64 | - public function __construct($method = '', UriInterface $uri = null, $version = self::DEFAULT_VERSION, array $headers = [], StreamInterface $body = null) | |
| 65 | -	{ | |
| 66 | -		if ($body === null) { | |
| 67 | -			$body = new Stream(fopen('php://input', 'r')); | |
| 68 | - } | |
| 69 | - | |
| 70 | - $this->serverParams = $_SERVER; | |
| 71 | - $this->cookieParams = $_COOKIE; | |
| 72 | - $this->queryParams = $this->initQueryParams($this->serverParams); | |
| 73 | - $this->postParams = $_POST; | |
| 74 | - $this->filesParams = $_FILES; | |
| 75 | - $this->uploadedFiles = $this->initUploadedFiles($this->filesParams); | |
| 76 | - $this->attributes = []; | |
| 77 | - | |
| 78 | - parent::__construct($this->initMethod($method), $this->initUri($uri), $version, $this->initHeaders($headers), $body); | |
| 79 | - } | |
| 80 | - | |
| 81 | - /** | |
| 82 | - * Initialize the method. | |
| 83 | - * | |
| 84 | - * @param string $method | |
| 85 | - * @return string the method. | |
| 86 | - */ | |
| 87 | - private function initMethod($method) | |
| 88 | -	{ | |
| 89 | - return $method === '' && isset($this->getServerParams()['REQUEST_METHOD']) ? $this->getServerParams()['REQUEST_METHOD'] : $method; | |
| 90 | - } | |
| 91 | - | |
| 92 | - /** | |
| 93 | - * Initialize the URI. | |
| 94 | - * | |
| 95 | - * @param UriInterface|null $uri | |
| 96 | - * @return UriInterface the URI. | |
| 97 | - */ | |
| 98 | - private function initUri($uri) | |
| 99 | -	{ | |
| 100 | -		if ($uri !== null) { | |
| 101 | - return $uri; | |
| 102 | - } | |
| 103 | - | |
| 104 | - $scheme = isset($this->getServerParams()['HTTPS']) ? 'https://' : 'http://'; | |
| 105 | - $host = isset($this->getServerParams()['HTTP_HOST']) ? $scheme . $this->getServerParams()['HTTP_HOST'] : ''; | |
| 106 | - $path = isset($this->getServerParams()['REQUEST_URI']) ? $this->getServerParams()['REQUEST_URI'] : ''; | |
| 107 | - | |
| 108 | - return new URI($host . $path); | |
| 109 | - } | |
| 110 | - | |
| 111 | - /** | |
| 112 | - * Initialize the headers. | |
| 113 | - * | |
| 114 | - * @param array $headers | |
| 115 | - * @return array the headers. | |
| 116 | - */ | |
| 117 | - private function initHeaders($headers) | |
| 118 | -	{ | |
| 119 | - return $headers ?: getallheaders(); | |
| 120 | - } | |
| 121 | - | |
| 122 | - /** | |
| 123 | - * Initialize the headers. | |
| 124 | - * | |
| 125 | - * @param string $uri | |
| 126 | - * @return array the headers. | |
| 127 | - */ | |
| 128 | - private function initQueryParams($serverParams) | |
| 129 | -	{ | |
| 130 | -		if (!isset($serverParams['REQUEST_URI']) || !($query = parse_url($serverParams['REQUEST_URI'], \PHP_URL_QUERY))) { | |
| 131 | - return []; | |
| 132 | - } | |
| 133 | - | |
| 134 | - parse_str($query, $result); | |
| 135 | - | |
| 136 | - return $result; | |
| 137 | - } | |
| 138 | - | |
| 139 | - /** | |
| 140 | - * Initialize the uploaded files. | |
| 141 | - * | |
| 142 | - * @param array $files | |
| 143 | - * @return array the uploaded files. | |
| 144 | - */ | |
| 145 | - private function initUploadedFiles(array $files) | |
| 146 | -	{ | |
| 147 | - $result = []; | |
| 148 | - | |
| 149 | -		foreach ($files as $key => $value) { | |
| 150 | - $result[$key] = $this->parseUploadedFiles($value); | |
| 151 | - } | |
| 152 | - | |
| 153 | - return $result; | |
| 154 | - } | |
| 155 | - | |
| 156 | - /** | |
| 157 | - * Parse uploaded files. | |
| 158 | - * | |
| 159 | - * @param array $files | |
| 160 | - * @return UploadedFile|array uploaded files. | |
| 161 | - */ | |
| 162 | - private function parseUploadedFiles($files) | |
| 163 | -	{ | |
| 164 | - // Empty | |
| 165 | - $first = reset($files); | |
| 166 | - | |
| 167 | - // Single | |
| 168 | -		if (!is_array($first)) { | |
| 169 | - return $this->parseSingleUploadedFiles($files); | |
| 170 | - } | |
| 171 | - | |
| 172 | - // Multiple | |
| 173 | -		if (count(array_filter(array_keys($first), 'is_string')) === 0) { | |
| 174 | - return $this->parseMultipleUploadedFiles($files); | |
| 175 | - } | |
| 176 | - | |
| 177 | - // Namespace | |
| 178 | - return $this->initUploadedFiles($files); | |
| 179 | - } | |
| 180 | - | |
| 181 | - /** | |
| 182 | - * Parse single uploaded file. | |
| 183 | - * | |
| 184 | - * @param array $file | |
| 185 | - * @return UploadedFile single uploaded file. | |
| 186 | - */ | |
| 187 | - private function parseSingleUploadedFiles(array $file) | |
| 188 | -	{ | |
| 189 | - return new UploadedFile($file['name'], $file['type'], $file['tmp_name'], $file['error'], $file['size']); | |
| 190 | - } | |
| 191 | - | |
| 192 | - /** | |
| 193 | - * Parse multiple uploaded files. | |
| 194 | - * | |
| 195 | - * @param array $files | |
| 196 | - * @return UploadedFiles[] multiple uploaded files. | |
| 197 | - */ | |
| 198 | - private function parseMultipleUploadedFiles(array $files) | |
| 199 | -	{ | |
| 200 | - $count = count($files['name']); | |
| 201 | - $result = []; | |
| 202 | - | |
| 203 | -		for ($i = 0; $i < $count; $i++) { | |
| 204 | - $result[] = new UploadedFile($files['name'][$i], $files['type'][$i], $files['tmp_name'][$i], $files['error'][$i], $files['size'][$i]); | |
| 205 | - } | |
| 206 | - | |
| 207 | - return $result; | |
| 208 | - } | |
| 209 | - | |
| 210 | - /** | |
| 211 | -	 * {@inheritdoc} | |
| 212 | - */ | |
| 213 | - public function getServerParams() | |
| 214 | -	{ | |
| 215 | - return $this->serverParams; | |
| 216 | - } | |
| 217 | - | |
| 218 | - /** | |
| 219 | -	 * {@inheritdoc} | |
| 220 | - */ | |
| 221 | - public function getCookieParams() | |
| 222 | -	{ | |
| 223 | - return $this->cookieParams; | |
| 224 | - } | |
| 225 | - | |
| 226 | - /** | |
| 227 | - * Set the cookie params. | |
| 228 | - * | |
| 229 | - * @param array $cookieParams | |
| 230 | - * @return $this | |
| 231 | - */ | |
| 232 | - private function setCookieParams(array $cookieParams) | |
| 233 | -	{ | |
| 234 | - $this->cookieParams = $cookieParams; | |
| 235 | - | |
| 236 | - return $this; | |
| 237 | - } | |
| 238 | - | |
| 239 | - /** | |
| 240 | -	 * {@inheritdoc} | |
| 241 | - */ | |
| 242 | - public function withCookieParams(array $cookieParams) | |
| 243 | -	{ | |
| 244 | - $result = clone $this; | |
| 245 | - | |
| 246 | - return $result->setCookieParams($cookieParams); | |
| 247 | - } | |
| 248 | - | |
| 249 | - /** | |
| 250 | -	 * {@inheritdoc} | |
| 251 | - */ | |
| 252 | - public function getQueryParams() | |
| 253 | -	{ | |
| 254 | - return $this->queryParams; | |
| 255 | - } | |
| 256 | - | |
| 257 | - /** | |
| 258 | - * Set the query params. | |
| 259 | - * | |
| 260 | - * @param array $queryParams | |
| 261 | - * @return $this | |
| 262 | - */ | |
| 263 | - private function setQueryParams(array $queryParams) | |
| 264 | -	{ | |
| 265 | - $this->queryParams = $queryParams; | |
| 266 | - | |
| 267 | - return $this; | |
| 268 | - } | |
| 269 | - | |
| 270 | - /** | |
| 271 | -	 * {@inheritdoc} | |
| 272 | - */ | |
| 273 | - public function withQueryParams(array $queryParams) | |
| 274 | -	{ | |
| 275 | - $result = clone $this; | |
| 276 | - | |
| 277 | - return $result->setQueryParams($queryParams); | |
| 278 | - } | |
| 279 | - | |
| 280 | - /** | |
| 281 | -	 * {@inheritdoc} | |
| 282 | - */ | |
| 283 | - public function getUploadedFiles() | |
| 284 | -	{ | |
| 285 | - return $this->uploadedFiles; | |
| 286 | - } | |
| 287 | - | |
| 288 | - /** | |
| 289 | - * Set the uploaded files. | |
| 290 | - * | |
| 291 | - * @param array $uploadedFiles | |
| 292 | - * @return $this | |
| 293 | - */ | |
| 294 | - private function setUploadedFiles(array $uploadedFiles) | |
| 295 | -	{ | |
| 296 | - $this->uploadedFiles = $uploadedFiles; | |
| 297 | - | |
| 298 | - return $this; | |
| 299 | - } | |
| 300 | - | |
| 301 | - /** | |
| 302 | -	 * {@inheritdoc} | |
| 303 | - */ | |
| 304 | - public function withUploadedFiles(array $uploadedFiles) | |
| 305 | -	{ | |
| 306 | - $result = clone $this; | |
| 307 | - | |
| 308 | - return $result->setUploadedFiles($uploadedFiles); | |
| 309 | - } | |
| 310 | - | |
| 311 | - /** | |
| 312 | -	 * {@inheritdoc} | |
| 313 | - */ | |
| 314 | - public function getParsedBody() | |
| 315 | -	{ | |
| 316 | -		if ($this->parsedBody !== null) { | |
| 317 | - return $this->parsedBody; | |
| 318 | - } | |
| 319 | - | |
| 320 | -		if ($this->getMethod() === 'POST' && ($this->hasContentType('application/x-www-form-urlencoded') || $this->hasContentType('multipart/form-data'))) { | |
| 321 | - return $this->postParams; | |
| 322 | - } | |
| 323 | - | |
| 324 | -		if ($this->hasContentType('application/json')) { | |
| 325 | - return json_decode((string) $this->getBody()); | |
| 326 | - } | |
| 327 | - | |
| 328 | - return null; | |
| 329 | - } | |
| 330 | - | |
| 331 | - /** | |
| 332 | - * Checks if a content type header exists with the given content type. | |
| 333 | - * | |
| 334 | - * @param string $contentType | |
| 335 | - * @return true if a content type header exists with the given content type. | |
| 336 | - */ | |
| 337 | - private function hasContentType($contentType) | |
| 338 | -	{ | |
| 339 | -		foreach ($this->getHeader('Content-Type') as $key => $value) { | |
| 340 | -			if (substr($value, 0, strlen($contentType)) == $contentType) { | |
| 341 | - return true; | |
| 342 | - } | |
| 343 | - } | |
| 344 | - | |
| 345 | - return false; | |
| 346 | - } | |
| 347 | - | |
| 348 | - /** | |
| 349 | - * Set the parsed body. | |
| 350 | - * | |
| 351 | - * @param null|array|object $parsedBody | |
| 352 | - * @return $this | |
| 353 | - */ | |
| 354 | - private function setParsedBody($parsedBody) | |
| 355 | -	{ | |
| 356 | - $this->parsedBody = $parsedBody; | |
| 357 | - | |
| 358 | - return $this; | |
| 359 | - } | |
| 360 | - | |
| 361 | - /** | |
| 362 | -	 * {@inheritdoc} | |
| 363 | - */ | |
| 364 | - public function withParsedBody($parsedBody) | |
| 365 | -	{ | |
| 366 | - $result = clone $this; | |
| 367 | - | |
| 368 | - return $result->setParsedBody($parsedBody); | |
| 369 | - } | |
| 370 | - | |
| 371 | - /** | |
| 372 | -	 * {@inheritdoc} | |
| 373 | - */ | |
| 374 | - public function getAttributes() | |
| 375 | -	{ | |
| 376 | - return $this->attributes; | |
| 377 | - } | |
| 378 | - | |
| 379 | - /** | |
| 380 | -	 * {@inheritdoc} | |
| 381 | - */ | |
| 382 | - public function getAttribute($name, $default = null) | |
| 383 | -	{ | |
| 384 | - return isset($this->attributes[$name]) ? $this->attributes[$name] : $default; | |
| 385 | - } | |
| 386 | - | |
| 387 | - /** | |
| 388 | - * Set the attribute. | |
| 389 | - * | |
| 390 | - * @param string $name | |
| 391 | - * @param mixed $value | |
| 392 | - * @return $this | |
| 393 | - */ | |
| 394 | - private function setAttribute($name, $value) | |
| 395 | -	{ | |
| 396 | - $this->attributes[$name] = $value; | |
| 397 | - | |
| 398 | - return $this; | |
| 399 | - } | |
| 400 | - | |
| 401 | - /** | |
| 402 | -	 * {@inheritdoc} | |
| 403 | - */ | |
| 404 | - public function withAttribute($name, $value) | |
| 405 | -	{ | |
| 406 | - $result = clone $this; | |
| 407 | - | |
| 408 | - return $result->setAttribute($name, $value); | |
| 409 | - } | |
| 410 | - | |
| 411 | - /** | |
| 412 | - * Remove the attribute. | |
| 413 | - * | |
| 414 | - * @param string $name | |
| 415 | - * @return $this | |
| 416 | - */ | |
| 417 | - private function removeAttribute($name) | |
| 418 | -	{ | |
| 419 | - unset($this->attributes[$name]); | |
| 420 | - | |
| 421 | - return $this; | |
| 422 | - } | |
| 423 | - | |
| 424 | - /** | |
| 425 | -	 * {@inheritdoc} | |
| 426 | - */ | |
| 427 | - public function withoutAttribute($name) | |
| 428 | -	{ | |
| 429 | - $result = clone $this; | |
| 430 | - | |
| 431 | - return $result->removeAttribute($name); | |
| 432 | - } | |
| 25 | + /** @var array The server parameters. */ | |
| 26 | + private $serverParams; | |
| 27 | + | |
| 28 | + /** @var array The cookie parameters. */ | |
| 29 | + private $cookieParams; | |
| 30 | + | |
| 31 | + /** @var array The query parameters. */ | |
| 32 | + private $queryParams; | |
| 33 | + | |
| 34 | + /** @var array The post parameters. */ | |
| 35 | + private $postParams; | |
| 36 | + | |
| 37 | + /** @var array The files parameters. */ | |
| 38 | + private $filesParams; | |
| 39 | + | |
| 40 | + /** @var array The uploaded files. */ | |
| 41 | + private $uploadedFiles; | |
| 42 | + | |
| 43 | + /** @var null|array|object The parsed body. */ | |
| 44 | + private $parsedBody; | |
| 45 | + | |
| 46 | + /** @var array The attributes. */ | |
| 47 | + private $attributes; | |
| 48 | + | |
| 49 | + /** | |
| 50 | + * Construct a Request object with the given method, uri, version, headers & body. | |
| 51 | + * | |
| 52 | + * @global array $_SERVER The server parameters. | |
| 53 | + * @global array $_COOKIE The cookie parameters. | |
| 54 | + * @global array $_GET The query parameters. | |
| 55 | + * @global array $_POST The post parameters. | |
| 56 | + * @global array $_FILES The files parameters. | |
| 57 | + * | |
| 58 | + * @param string $method = '' | |
| 59 | + * @param UriInterface|null $uri = null | |
| 60 | + * @param string $version = self::DEFAULT_VERSION | |
| 61 | + * @param array $headers = [] | |
| 62 | + * @param StreamInterface|null $body = null | |
| 63 | + */ | |
| 64 | + public function __construct($method = '', UriInterface $uri = null, $version = self::DEFAULT_VERSION, array $headers = [], StreamInterface $body = null) | |
| 65 | +    { | |
| 66 | +        if ($body === null) { | |
| 67 | +            $body = new Stream(fopen('php://input', 'r')); | |
| 68 | + } | |
| 69 | + | |
| 70 | + $this->serverParams = $_SERVER; | |
| 71 | + $this->cookieParams = $_COOKIE; | |
| 72 | + $this->queryParams = $this->initQueryParams($this->serverParams); | |
| 73 | + $this->postParams = $_POST; | |
| 74 | + $this->filesParams = $_FILES; | |
| 75 | + $this->uploadedFiles = $this->initUploadedFiles($this->filesParams); | |
| 76 | + $this->attributes = []; | |
| 77 | + | |
| 78 | + parent::__construct($this->initMethod($method), $this->initUri($uri), $version, $this->initHeaders($headers), $body); | |
| 79 | + } | |
| 80 | + | |
| 81 | + /** | |
| 82 | + * Initialize the method. | |
| 83 | + * | |
| 84 | + * @param string $method | |
| 85 | + * @return string the method. | |
| 86 | + */ | |
| 87 | + private function initMethod($method) | |
| 88 | +    { | |
| 89 | + return $method === '' && isset($this->getServerParams()['REQUEST_METHOD']) ? $this->getServerParams()['REQUEST_METHOD'] : $method; | |
| 90 | + } | |
| 91 | + | |
| 92 | + /** | |
| 93 | + * Initialize the URI. | |
| 94 | + * | |
| 95 | + * @param UriInterface|null $uri | |
| 96 | + * @return UriInterface the URI. | |
| 97 | + */ | |
| 98 | + private function initUri($uri) | |
| 99 | +    { | |
| 100 | +        if ($uri !== null) { | |
| 101 | + return $uri; | |
| 102 | + } | |
| 103 | + | |
| 104 | + $scheme = isset($this->getServerParams()['HTTPS']) ? 'https://' : 'http://'; | |
| 105 | + $host = isset($this->getServerParams()['HTTP_HOST']) ? $scheme . $this->getServerParams()['HTTP_HOST'] : ''; | |
| 106 | + $path = isset($this->getServerParams()['REQUEST_URI']) ? $this->getServerParams()['REQUEST_URI'] : ''; | |
| 107 | + | |
| 108 | + return new URI($host . $path); | |
| 109 | + } | |
| 110 | + | |
| 111 | + /** | |
| 112 | + * Initialize the headers. | |
| 113 | + * | |
| 114 | + * @param array $headers | |
| 115 | + * @return array the headers. | |
| 116 | + */ | |
| 117 | + private function initHeaders($headers) | |
| 118 | +    { | |
| 119 | + return $headers ?: getallheaders(); | |
| 120 | + } | |
| 121 | + | |
| 122 | + /** | |
| 123 | + * Initialize the headers. | |
| 124 | + * | |
| 125 | + * @param string $uri | |
| 126 | + * @return array the headers. | |
| 127 | + */ | |
| 128 | + private function initQueryParams($serverParams) | |
| 129 | +    { | |
| 130 | +        if (!isset($serverParams['REQUEST_URI']) || !($query = parse_url($serverParams['REQUEST_URI'], \PHP_URL_QUERY))) { | |
| 131 | + return []; | |
| 132 | + } | |
| 133 | + | |
| 134 | + parse_str($query, $result); | |
| 135 | + | |
| 136 | + return $result; | |
| 137 | + } | |
| 138 | + | |
| 139 | + /** | |
| 140 | + * Initialize the uploaded files. | |
| 141 | + * | |
| 142 | + * @param array $files | |
| 143 | + * @return array the uploaded files. | |
| 144 | + */ | |
| 145 | + private function initUploadedFiles(array $files) | |
| 146 | +    { | |
| 147 | + $result = []; | |
| 148 | + | |
| 149 | +        foreach ($files as $key => $value) { | |
| 150 | + $result[$key] = $this->parseUploadedFiles($value); | |
| 151 | + } | |
| 152 | + | |
| 153 | + return $result; | |
| 154 | + } | |
| 155 | + | |
| 156 | + /** | |
| 157 | + * Parse uploaded files. | |
| 158 | + * | |
| 159 | + * @param array $files | |
| 160 | + * @return UploadedFile|array uploaded files. | |
| 161 | + */ | |
| 162 | + private function parseUploadedFiles($files) | |
| 163 | +    { | |
| 164 | + // Empty | |
| 165 | + $first = reset($files); | |
| 166 | + | |
| 167 | + // Single | |
| 168 | +        if (!is_array($first)) { | |
| 169 | + return $this->parseSingleUploadedFiles($files); | |
| 170 | + } | |
| 171 | + | |
| 172 | + // Multiple | |
| 173 | +        if (count(array_filter(array_keys($first), 'is_string')) === 0) { | |
| 174 | + return $this->parseMultipleUploadedFiles($files); | |
| 175 | + } | |
| 176 | + | |
| 177 | + // Namespace | |
| 178 | + return $this->initUploadedFiles($files); | |
| 179 | + } | |
| 180 | + | |
| 181 | + /** | |
| 182 | + * Parse single uploaded file. | |
| 183 | + * | |
| 184 | + * @param array $file | |
| 185 | + * @return UploadedFile single uploaded file. | |
| 186 | + */ | |
| 187 | + private function parseSingleUploadedFiles(array $file) | |
| 188 | +    { | |
| 189 | + return new UploadedFile($file['name'], $file['type'], $file['tmp_name'], $file['error'], $file['size']); | |
| 190 | + } | |
| 191 | + | |
| 192 | + /** | |
| 193 | + * Parse multiple uploaded files. | |
| 194 | + * | |
| 195 | + * @param array $files | |
| 196 | + * @return UploadedFiles[] multiple uploaded files. | |
| 197 | + */ | |
| 198 | + private function parseMultipleUploadedFiles(array $files) | |
| 199 | +    { | |
| 200 | + $count = count($files['name']); | |
| 201 | + $result = []; | |
| 202 | + | |
| 203 | +        for ($i = 0; $i < $count; $i++) { | |
| 204 | + $result[] = new UploadedFile($files['name'][$i], $files['type'][$i], $files['tmp_name'][$i], $files['error'][$i], $files['size'][$i]); | |
| 205 | + } | |
| 206 | + | |
| 207 | + return $result; | |
| 208 | + } | |
| 209 | + | |
| 210 | + /** | |
| 211 | +     * {@inheritdoc} | |
| 212 | + */ | |
| 213 | + public function getServerParams() | |
| 214 | +    { | |
| 215 | + return $this->serverParams; | |
| 216 | + } | |
| 217 | + | |
| 218 | + /** | |
| 219 | +     * {@inheritdoc} | |
| 220 | + */ | |
| 221 | + public function getCookieParams() | |
| 222 | +    { | |
| 223 | + return $this->cookieParams; | |
| 224 | + } | |
| 225 | + | |
| 226 | + /** | |
| 227 | + * Set the cookie params. | |
| 228 | + * | |
| 229 | + * @param array $cookieParams | |
| 230 | + * @return $this | |
| 231 | + */ | |
| 232 | + private function setCookieParams(array $cookieParams) | |
| 233 | +    { | |
| 234 | + $this->cookieParams = $cookieParams; | |
| 235 | + | |
| 236 | + return $this; | |
| 237 | + } | |
| 238 | + | |
| 239 | + /** | |
| 240 | +     * {@inheritdoc} | |
| 241 | + */ | |
| 242 | + public function withCookieParams(array $cookieParams) | |
| 243 | +    { | |
| 244 | + $result = clone $this; | |
| 245 | + | |
| 246 | + return $result->setCookieParams($cookieParams); | |
| 247 | + } | |
| 248 | + | |
| 249 | + /** | |
| 250 | +     * {@inheritdoc} | |
| 251 | + */ | |
| 252 | + public function getQueryParams() | |
| 253 | +    { | |
| 254 | + return $this->queryParams; | |
| 255 | + } | |
| 256 | + | |
| 257 | + /** | |
| 258 | + * Set the query params. | |
| 259 | + * | |
| 260 | + * @param array $queryParams | |
| 261 | + * @return $this | |
| 262 | + */ | |
| 263 | + private function setQueryParams(array $queryParams) | |
| 264 | +    { | |
| 265 | + $this->queryParams = $queryParams; | |
| 266 | + | |
| 267 | + return $this; | |
| 268 | + } | |
| 269 | + | |
| 270 | + /** | |
| 271 | +     * {@inheritdoc} | |
| 272 | + */ | |
| 273 | + public function withQueryParams(array $queryParams) | |
| 274 | +    { | |
| 275 | + $result = clone $this; | |
| 276 | + | |
| 277 | + return $result->setQueryParams($queryParams); | |
| 278 | + } | |
| 279 | + | |
| 280 | + /** | |
| 281 | +     * {@inheritdoc} | |
| 282 | + */ | |
| 283 | + public function getUploadedFiles() | |
| 284 | +    { | |
| 285 | + return $this->uploadedFiles; | |
| 286 | + } | |
| 287 | + | |
| 288 | + /** | |
| 289 | + * Set the uploaded files. | |
| 290 | + * | |
| 291 | + * @param array $uploadedFiles | |
| 292 | + * @return $this | |
| 293 | + */ | |
| 294 | + private function setUploadedFiles(array $uploadedFiles) | |
| 295 | +    { | |
| 296 | + $this->uploadedFiles = $uploadedFiles; | |
| 297 | + | |
| 298 | + return $this; | |
| 299 | + } | |
| 300 | + | |
| 301 | + /** | |
| 302 | +     * {@inheritdoc} | |
| 303 | + */ | |
| 304 | + public function withUploadedFiles(array $uploadedFiles) | |
| 305 | +    { | |
| 306 | + $result = clone $this; | |
| 307 | + | |
| 308 | + return $result->setUploadedFiles($uploadedFiles); | |
| 309 | + } | |
| 310 | + | |
| 311 | + /** | |
| 312 | +     * {@inheritdoc} | |
| 313 | + */ | |
| 314 | + public function getParsedBody() | |
| 315 | +    { | |
| 316 | +        if ($this->parsedBody !== null) { | |
| 317 | + return $this->parsedBody; | |
| 318 | + } | |
| 319 | + | |
| 320 | +        if ($this->getMethod() === 'POST' && ($this->hasContentType('application/x-www-form-urlencoded') || $this->hasContentType('multipart/form-data'))) { | |
| 321 | + return $this->postParams; | |
| 322 | + } | |
| 323 | + | |
| 324 | +        if ($this->hasContentType('application/json')) { | |
| 325 | + return json_decode((string) $this->getBody()); | |
| 326 | + } | |
| 327 | + | |
| 328 | + return null; | |
| 329 | + } | |
| 330 | + | |
| 331 | + /** | |
| 332 | + * Checks if a content type header exists with the given content type. | |
| 333 | + * | |
| 334 | + * @param string $contentType | |
| 335 | + * @return true if a content type header exists with the given content type. | |
| 336 | + */ | |
| 337 | + private function hasContentType($contentType) | |
| 338 | +    { | |
| 339 | +        foreach ($this->getHeader('Content-Type') as $key => $value) { | |
| 340 | +            if (substr($value, 0, strlen($contentType)) == $contentType) { | |
| 341 | + return true; | |
| 342 | + } | |
| 343 | + } | |
| 344 | + | |
| 345 | + return false; | |
| 346 | + } | |
| 347 | + | |
| 348 | + /** | |
| 349 | + * Set the parsed body. | |
| 350 | + * | |
| 351 | + * @param null|array|object $parsedBody | |
| 352 | + * @return $this | |
| 353 | + */ | |
| 354 | + private function setParsedBody($parsedBody) | |
| 355 | +    { | |
| 356 | + $this->parsedBody = $parsedBody; | |
| 357 | + | |
| 358 | + return $this; | |
| 359 | + } | |
| 360 | + | |
| 361 | + /** | |
| 362 | +     * {@inheritdoc} | |
| 363 | + */ | |
| 364 | + public function withParsedBody($parsedBody) | |
| 365 | +    { | |
| 366 | + $result = clone $this; | |
| 367 | + | |
| 368 | + return $result->setParsedBody($parsedBody); | |
| 369 | + } | |
| 370 | + | |
| 371 | + /** | |
| 372 | +     * {@inheritdoc} | |
| 373 | + */ | |
| 374 | + public function getAttributes() | |
| 375 | +    { | |
| 376 | + return $this->attributes; | |
| 377 | + } | |
| 378 | + | |
| 379 | + /** | |
| 380 | +     * {@inheritdoc} | |
| 381 | + */ | |
| 382 | + public function getAttribute($name, $default = null) | |
| 383 | +    { | |
| 384 | + return isset($this->attributes[$name]) ? $this->attributes[$name] : $default; | |
| 385 | + } | |
| 386 | + | |
| 387 | + /** | |
| 388 | + * Set the attribute. | |
| 389 | + * | |
| 390 | + * @param string $name | |
| 391 | + * @param mixed $value | |
| 392 | + * @return $this | |
| 393 | + */ | |
| 394 | + private function setAttribute($name, $value) | |
| 395 | +    { | |
| 396 | + $this->attributes[$name] = $value; | |
| 397 | + | |
| 398 | + return $this; | |
| 399 | + } | |
| 400 | + | |
| 401 | + /** | |
| 402 | +     * {@inheritdoc} | |
| 403 | + */ | |
| 404 | + public function withAttribute($name, $value) | |
| 405 | +    { | |
| 406 | + $result = clone $this; | |
| 407 | + | |
| 408 | + return $result->setAttribute($name, $value); | |
| 409 | + } | |
| 410 | + | |
| 411 | + /** | |
| 412 | + * Remove the attribute. | |
| 413 | + * | |
| 414 | + * @param string $name | |
| 415 | + * @return $this | |
| 416 | + */ | |
| 417 | + private function removeAttribute($name) | |
| 418 | +    { | |
| 419 | + unset($this->attributes[$name]); | |
| 420 | + | |
| 421 | + return $this; | |
| 422 | + } | |
| 423 | + | |
| 424 | + /** | |
| 425 | +     * {@inheritdoc} | |
| 426 | + */ | |
| 427 | + public function withoutAttribute($name) | |
| 428 | +    { | |
| 429 | + $result = clone $this; | |
| 430 | + | |
| 431 | + return $result->removeAttribute($name); | |
| 432 | + } | |
| 433 | 433 | } |