From 5baf6d592450b7b30f5f509d2224467cffb5bc71 Mon Sep 17 00:00:00 2001 From: inhere Date: Mon, 4 Feb 2019 16:01:02 +0800 Subject: [PATCH] update: upgrade php version require is 7.1 --- src/Body.php | 2 +- src/Component/Collection.php | 17 ++++-- src/Component/Environment.php | 28 ++++----- src/Cookies.php | 18 +++--- src/Headers.php | 35 ++++++----- src/HttpFactory.php | 41 +++++++------ src/HttpUtil.php | 6 +- src/Message.php | 7 +-- src/Request.php | 17 +++--- src/Request/ExtendedRequest.php | 12 ++-- src/Request/RequestBody.php | 2 +- src/Response.php | 40 ++++++------- src/Response/InjectContentTypeTrait.php | 2 +- src/Response/JsonResponse.php | 19 +++--- src/Response/TextResponse.php | 15 ++--- src/ServerRequest.php | 78 ++++++++++++------------ src/Stream.php | 60 ++++++++++--------- src/Stream/FileStream.php | 30 ++++++++++ src/Stream/InputStream.php | 4 +- src/Stream/MemoryStream.php | 4 +- src/Stream/TempStream.php | 6 +- src/Traits/CookiesTrait.php | 6 +- src/Traits/ExtendedRequestTrait.php | 32 +++++----- src/Traits/ExtendedResponseTrait.php | 8 +-- src/Traits/MessageTrait.php | 22 ++++--- src/Traits/RequestHeadersTrait.php | 6 +- src/Traits/RequestTrait.php | 28 ++++----- src/UploadedFile.php | 29 +++++---- src/Uri.php | 79 ++++++++++++------------- 29 files changed, 341 insertions(+), 312 deletions(-) create mode 100644 src/Stream/FileStream.php diff --git a/src/Body.php b/src/Body.php index c32d0a6..10f5cf8 100644 --- a/src/Body.php +++ b/src/Body.php @@ -8,7 +8,7 @@ namespace PhpComp\Http\Message; -use PhpComp\Http\Message\Component\TempStream; +use PhpComp\Http\Message\Stream\TempStream; /** * Body diff --git a/src/Component/Collection.php b/src/Component/Collection.php index f8244f6..2241505 100644 --- a/src/Component/Collection.php +++ b/src/Component/Collection.php @@ -54,7 +54,7 @@ public function replace(array $items): void /** * @param string $name - * @param null $default + * @param null $default * @return mixed|null */ public function get(string $name, $default = null) @@ -64,7 +64,7 @@ public function get(string $name, $default = null) /** * @param string $name - * @param mixed $value + * @param mixed $value * @return mixed|null */ public function add($name, $value) @@ -80,13 +80,12 @@ public function add($name, $value) /** * @param string $name - * @param mixed $value + * @param mixed $value * @return mixed|null */ public function set($name, $value) { $this[$name] = $value; - return $this; } @@ -98,6 +97,14 @@ public function all(): array return $this->getArrayCopy(); } + /** + * @return array + */ + public function toArray(): array + { + return $this->getArrayCopy(); + } + /** * @param string $key * @return bool @@ -111,8 +118,8 @@ public function remove($key) { if (isset($this[$key])) { $val = $this[$key]; - unset($this[$key]); + unset($this[$key]); return $val; } diff --git a/src/Component/Environment.php b/src/Component/Environment.php index ed1b33e..e21aa4c 100644 --- a/src/Component/Environment.php +++ b/src/Component/Environment.php @@ -24,21 +24,21 @@ class Environment extends Collection public static function mock(array $userData = []): self { $data = \array_merge([ - 'SERVER_PROTOCOL' => 'HTTP/1.1', - 'REQUEST_METHOD' => 'GET', - 'SCRIPT_NAME' => '', - 'REQUEST_URI' => '', - 'QUERY_STRING' => '', - 'SERVER_NAME' => 'localhost', - 'SERVER_PORT' => 80, - 'HTTP_HOST' => 'localhost', - 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', + 'SERVER_PROTOCOL' => 'HTTP/1.1', + 'REQUEST_METHOD' => 'GET', + 'SCRIPT_NAME' => '', + 'REQUEST_URI' => '', + 'QUERY_STRING' => '', + 'SERVER_NAME' => 'localhost', + 'SERVER_PORT' => 80, + 'HTTP_HOST' => 'localhost', + 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.8', - 'HTTP_ACCEPT_CHARSET' => 'utf-8;q=0.7,*;q=0.3', - 'HTTP_USER_AGENT' => 'MY Framework', - 'REMOTE_ADDR' => '127.0.0.1', - 'REQUEST_TIME' => \time(), - 'REQUEST_TIME_FLOAT' => \microtime(true), + 'HTTP_ACCEPT_CHARSET' => 'utf-8;q=0.7,*;q=0.3', + 'HTTP_USER_AGENT' => 'MY Framework', + 'REMOTE_ADDR' => '127.0.0.1', + 'REQUEST_TIME' => \time(), + 'REQUEST_TIME_FLOAT' => \microtime(true), ], $userData); return new static($data); diff --git a/src/Cookies.php b/src/Cookies.php index 7bc9a4c..35cc5e2 100644 --- a/src/Cookies.php +++ b/src/Cookies.php @@ -29,12 +29,12 @@ class Cookies extends Collection * @var array */ protected $defaults = [ - 'value' => '', - 'domain' => null, + 'value' => '', + 'domain' => null, 'hostOnly' => null, - 'path' => null, - 'expires' => null, - 'secure' => false, + 'path' => null, + 'expires' => null, + 'secure' => false, 'httpOnly' => false ]; @@ -49,7 +49,7 @@ public function setDefaults(array $settings): void /** * Set cookie - * @param string $name Cookie name + * @param string $name Cookie name * @param string|array $value Cookie value, or cookie properties * @return $this */ @@ -66,7 +66,7 @@ public function set($name, $value): self /** * @param string $name - * @param mixed $value + * @param mixed $value * @return self */ public function add($name, $value): self @@ -95,7 +95,7 @@ public function toHeaders(): array /** * Convert to `Set-Cookie` header * @param string $name Cookie name - * @param array $properties Cookie properties + * @param array $properties Cookie properties * @return string */ protected function toHeaderLine(string $name, array $properties): string @@ -180,7 +180,7 @@ public static function parseFromRawHeader($cookieText): array $cookie = \explode('=', $cookie, 2); if (\count($cookie) === 2) { - $key = \urldecode($cookie[0]); + $key = \urldecode($cookie[0]); $value = \urldecode($cookie[1]); if (!isset($cookies[$key])) { diff --git a/src/Headers.php b/src/Headers.php index d39a939..c733d1c 100644 --- a/src/Headers.php +++ b/src/Headers.php @@ -29,15 +29,14 @@ class Headers extends Collection * @var array */ protected static $special = [ - 'CONTENT_TYPE' => 1, - 'CONTENT_LENGTH' => 1, - 'PHP_AUTH_USER' => 1, - 'PHP_AUTH_PW' => 1, + 'CONTENT_TYPE' => 1, + 'CONTENT_LENGTH' => 1, + 'PHP_AUTH_USER' => 1, + 'PHP_AUTH_PW' => 1, 'PHP_AUTH_DIGEST' => 1, - 'AUTH_TYPE' => 1, + 'AUTH_TYPE' => 1, ]; - /** * Return array of HTTP header names and values. * This method returns the _original_ header name @@ -59,14 +58,14 @@ public function all(): array * Set HTTP header value * This method sets a header value. It replaces * any values that may already exist for the header name. - * @param string $key The case-insensitive header name + * @param string $key The case-insensitive header name * @param string|array $value The header value * @return mixed */ public function set($key, $value) { return parent::set($this->normalizeKey($key), [ - 'value' => (array)$value, + 'value' => (array)$value, 'originalKey' => $key ]); } @@ -75,9 +74,9 @@ public function set($key, $value) * Get HTTP header value * * @param string $key The case-insensitive header name - * @param mixed $default The default value if key does not exist + * @param mixed $default The default value if key does not exist * - * @return string[]|null + * @return string[]|mixed|null */ public function get(string $key, $default = null) { @@ -89,7 +88,7 @@ public function get(string $key, $default = null) } /** - * @param $name + * @param $name * @param null $default * @return null|string */ @@ -104,7 +103,7 @@ public function getLine($name, $default = null): ?string /** * @param string $key - * @param mixed $value + * @param mixed $value * @return mixed|null */ public function add($key, $value) @@ -114,7 +113,7 @@ public function add($key, $value) } return parent::add($this->normalizeKey($key), [ - 'value' => (array)$value, + 'value' => (array)$value, 'originalKey' => $key ]); } @@ -165,7 +164,7 @@ public function getAcceptLanguages(): array } $value = \str_replace(' ', '', $value); - $ls = \explode(',', $value); + $ls = \explode(',', $value); } return $ls; @@ -186,7 +185,7 @@ public function getAcceptEncodes(): array } $value = \str_replace(' ', '', $value); - $ens = \explode(',', $value); + $ens = \explode(',', $value); } return $ens; @@ -201,8 +200,8 @@ public function toHeaderLines(bool $join = false) $output = []; foreach ($this as $name => $info) { - $name = \ucwords($name, '-'); - $value = \implode(',', $info['value']); + $name = \ucwords($name, '-'); + $value = \implode(',', $info['value']); $output[] = "$name: $value\r\n"; } @@ -217,7 +216,7 @@ public function getLines(): array $output = []; foreach ($this as $name => $info) { - $name = \ucwords($name, '-'); + $name = \ucwords($name, '-'); $output[$name] = \implode(',', $info['value']); } diff --git a/src/HttpFactory.php b/src/HttpFactory.php index 1cac6d0..10f6cd9 100644 --- a/src/HttpFactory.php +++ b/src/HttpFactory.php @@ -29,12 +29,12 @@ class HttpFactory * @var array */ protected static $special = [ - 'CONTENT_TYPE' => 1, - 'CONTENT_LENGTH' => 1, - 'PHP_AUTH_USER' => 1, - 'PHP_AUTH_PW' => 1, + 'CONTENT_TYPE' => 1, + 'CONTENT_LENGTH' => 1, + 'PHP_AUTH_USER' => 1, + 'PHP_AUTH_PW' => 1, 'PHP_AUTH_DIGEST' => 1, - 'AUTH_TYPE' => 1, + 'AUTH_TYPE' => 1, ]; /** @@ -43,7 +43,7 @@ class HttpFactory /** * Create a new request. - * @param string $method + * @param string $method * @param UriInterface|string $uri * @return RequestInterface * @throws \RuntimeException @@ -79,7 +79,7 @@ public static function createResponse(int $code = 200): ResponseInterface /** * Create a new server request. - * @param string $method + * @param string $method * @param UriInterface|string $uri * @return ServerRequestInterface * @throws \RuntimeException @@ -108,11 +108,11 @@ public static function createServerRequestFromArray($server, string $class = nul $env = self::ensureIsCollection($server); $uri = static::createUriFromArray($env); - $body = new RequestBody(); - $method = $env->get('REQUEST_METHOD', 'GET'); - $headers = static::createHeadersFromArray($env); - $cookies = Cookies::parseFromRawHeader($headers->get('Cookie', [])); - $serverParams = $env->all(); + $body = new RequestBody(); + $method = $env->get('REQUEST_METHOD', 'GET'); + $headers = static::createHeadersFromArray($env); + $cookies = Cookies::parseFromRawHeader($headers->get('Cookie', [])); + $serverParams = $env->all(); $uploadedFiles = UploadedFile::createFromFILES(); $class = $class ?: ServerRequest::class; @@ -191,10 +191,10 @@ public static function createStreamFromResource($resource): StreamInterface * @see http://php.net/manual/features.file-upload.post-method.php * @see http://php.net/manual/features.file-upload.errors.php * @param string|resource $file - * @param integer $size in bytes - * @param integer $error PHP file upload error - * @param string $clientFilename - * @param string $clientMediaType + * @param integer $size in bytes + * @param integer $error PHP file upload error + * @param string $clientFilename + * @param string $clientMediaType * @return UploadedFileInterface * @throws \InvalidArgumentException If the file resource is not readable. */ @@ -204,8 +204,7 @@ public static function createUploadedFile( int $error = \UPLOAD_ERR_OK, string $clientFilename = null, string $clientMediaType = null - ): UploadedFileInterface - { + ): UploadedFileInterface { return new UploadedFile($file, $clientFilename, $clientMediaType, $size, $error); } @@ -235,7 +234,7 @@ public static function createUriFromArray($env): Uri // Scheme $isSecure = $env->get('HTTPS'); - $scheme = (empty($isSecure) || $isSecure === 'off') ? 'http' : 'https'; + $scheme = (empty($isSecure) || $isSecure === 'off') ? 'http' : 'https'; // Authority: Username and password $username = $env->get('PHP_AUTH_USER', ''); @@ -298,8 +297,8 @@ public static function createUriFromArray($env): Uri public static function createHeadersFromArray($env): Headers { $data = []; - $env = self::ensureIsCollection($env); - $env = self::determineAuthorization($env); + $env = self::ensureIsCollection($env); + $env = self::determineAuthorization($env); foreach ($env as $key => $value) { $key = \strtoupper($key); diff --git a/src/HttpUtil.php b/src/HttpUtil.php index b9c54a9..1306fb2 100644 --- a/src/HttpUtil.php +++ b/src/HttpUtil.php @@ -21,13 +21,13 @@ class HttpUtil /** * Send the response the client * @param ResponseInterface $response - * @param array $options + * @param array $options * @throws \RuntimeException */ public static function respond(ResponseInterface $response, array $options = []): void { $options = \array_merge([ - 'chunkSize' => 4096, + 'chunkSize' => 4096, 'addContentLengthHeader' => false, ], $options); @@ -57,7 +57,7 @@ public static function respond(ResponseInterface $response, array $options = []) $body->rewind(); } - $chunkSize = $options['chunkSize']; + $chunkSize = $options['chunkSize']; $contentLength = $response->getHeaderLine('Content-Length'); if (!$contentLength) { diff --git a/src/Message.php b/src/Message.php index d250484..2c43eb4 100644 --- a/src/Message.php +++ b/src/Message.php @@ -13,7 +13,6 @@ /** * Class Message - * @package Sws\parts * * @property Cookies $cookies * @@ -29,10 +28,10 @@ class Message implements MessageInterface /** * BaseMessage constructor. - * @param string $protocol - * @param string $protocolVersion + * @param string $protocol + * @param string $protocolVersion * @param array|Headers $headers - * @param string $body + * @param string $body * @throws \InvalidArgumentException */ public function __construct(string $protocol = 'http', string $protocolVersion = '1.1', $headers = null, $body = '') diff --git a/src/Request.php b/src/Request.php index af47e8e..091fa6a 100644 --- a/src/Request.php +++ b/src/Request.php @@ -18,7 +18,7 @@ /** * Class Request - * @property string $method + * @property string $method * @property-read string $origin * * @method Body getBody() @@ -31,11 +31,11 @@ class Request implements RequestInterface /** * Request constructor. - * @param string $method - * @param UriInterface $uri - * @param string $protocol - * @param string $protocolVersion - * @param array|Headers $headers + * @param string $method + * @param UriInterface $uri + * @param string $protocol + * @param string $protocolVersion + * @param array|Headers $headers * @param StreamInterface $body * @throws \RuntimeException * @throws \InvalidArgumentException @@ -47,8 +47,7 @@ public function __construct( StreamInterface $body = null, string $protocol = 'HTTP', string $protocolVersion = '1.1' - ) - { + ) { $this->initialize($protocol, $protocolVersion, $headers, $body ?: new RequestBody()); $this->initializeRequest($uri, $method); @@ -61,7 +60,7 @@ public function __construct( public function __clone() { $this->headers = clone $this->headers; - $this->body = clone $this->body; + $this->body = clone $this->body; } /** diff --git a/src/Request/ExtendedRequest.php b/src/Request/ExtendedRequest.php index b5fc610..0c881c5 100644 --- a/src/Request/ExtendedRequest.php +++ b/src/Request/ExtendedRequest.php @@ -20,8 +20,8 @@ class ExtendedRequest extends ServerRequest use ExtendedRequestTrait; /** - * @param string $name - * @param null|mixed $default + * @param string $name + * @param null|mixed $default * @param null|string $filter * @return mixed */ @@ -34,7 +34,7 @@ public function getQuery($name, $default = null, $filter = null) /** * @param string $name - * @param mixed $default + * @param mixed $default * @param string $filter * @return mixed */ @@ -49,7 +49,7 @@ public function get($name = null, $default = null, $filter = null) /** * @param string $name - * @param mixed $default + * @param mixed $default * @param string $filter * @return mixed * @throws \RuntimeException @@ -65,7 +65,7 @@ public function post($name = null, $default = null, $filter = null) /** * @param string $name - * @param mixed $default + * @param mixed $default * @param string $filter * @return mixed * @throws \RuntimeException @@ -81,7 +81,7 @@ public function json($name = null, $default = null, $filter = null) /** * @param string $name - * @param mixed $default + * @param mixed $default * @param string $filter * @return mixed */ diff --git a/src/Request/RequestBody.php b/src/Request/RequestBody.php index fee54ab..b5f2f84 100644 --- a/src/Request/RequestBody.php +++ b/src/Request/RequestBody.php @@ -26,7 +26,7 @@ class RequestBody extends Stream public function __construct(string $content = null) { $stream = \fopen('php://temp', 'wb+'); - \stream_copy_to_stream(fopen('php://input', 'rb'), $stream); + \stream_copy_to_stream(\fopen('php://input', 'rb'), $stream); \rewind($stream); parent::__construct($stream); diff --git a/src/Response.php b/src/Response.php index 11ebb53..4a9538f 100644 --- a/src/Response.php +++ b/src/Response.php @@ -18,7 +18,7 @@ * Class Response * response for handshake * @package PhpComp\Http\Message - * @property int $status + * @property int $status * @property string $statusMsg * * @link https://github.com/php-fig/http-message/blob/master/src/MessageInterface.php @@ -122,12 +122,12 @@ class Response implements ResponseInterface ]; /** - * @param int $status - * @param null $headers - * @param array $cookies + * @param int $status + * @param null $headers + * @param array $cookies * @param StreamInterface|null $body - * @param string $protocol - * @param string $protocolVersion + * @param string $protocol + * @param string $protocolVersion * @return Response * @throws \InvalidArgumentException */ @@ -138,19 +138,18 @@ public static function make( StreamInterface $body = null, string $protocol = 'HTTP', string $protocolVersion = '1.1' - ): Response - { + ): Response { return new static($status, $headers, $cookies, $body, $protocol, $protocolVersion); } /** * Request constructor. - * @param int $status - * @param array|Headers $headers - * @param array $cookies + * @param int $status + * @param array|Headers $headers + * @param array $cookies * @param StreamInterface $body - * @param string $protocol - * @param string $protocolVersion + * @param string $protocol + * @param string $protocolVersion * @throws \InvalidArgumentException */ public function __construct( @@ -160,8 +159,7 @@ public function __construct( StreamInterface $body = null, string $protocol = 'HTTP', string $protocolVersion = '1.1' - ) - { + ) { $this->setCookies($cookies); $this->initialize($protocol, $protocolVersion, $headers, $body ?: new Body()); @@ -223,8 +221,8 @@ public function toString(): string * Note: This method is not part of the PSR-7 standard. * This method prepares the response object to return an HTTP Json response to the client. * @param mixed $data The data - * @param int $status The HTTP status code. - * @param int $encodingOptions Json encoding options + * @param int $status The HTTP status code. + * @param int $encodingOptions Json encoding options * @throws \InvalidArgumentException * @throws \RuntimeException * @return self|mixed @@ -251,7 +249,7 @@ public function json($data, int $status = null, int $encodingOptions = 0): self /** * @param string $url - * @param int $status + * @param int $status * @return $this|mixed * @throws \InvalidArgumentException */ @@ -276,7 +274,7 @@ public function redirect(string $url, $status = 302): self * updated status and reason phrase. * @link http://tools.ietf.org/html/rfc7231#section-6 * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml - * @param int $code The 3-digit integer result code to set. + * @param int $code The 3-digit integer result code to set. * @param string $reasonPhrase The reason phrase to use with the * provided status code; if none is provided, implementations MAY * use the defaults as suggested in the HTTP specification. @@ -291,7 +289,7 @@ public function withStatus($code, $reasonPhrase = '') throw new \InvalidArgumentException('ReasonPhrase must be a string'); } - $clone = clone $this; + $clone = clone $this; $clone->status = $code; if ($reasonPhrase === '' && isset(static::$messages[$code])) { @@ -308,7 +306,7 @@ public function withStatus($code, $reasonPhrase = '') } /** - * @param int $code + * @param int $code * @param string $reasonPhrase * @return Response * @throws \InvalidArgumentException diff --git a/src/Response/InjectContentTypeTrait.php b/src/Response/InjectContentTypeTrait.php index 7860ee2..fde666e 100644 --- a/src/Response/InjectContentTypeTrait.php +++ b/src/Response/InjectContentTypeTrait.php @@ -19,7 +19,7 @@ trait InjectContentTypeTrait * Inject the provided Content-Type, if none is already present. * * @param string $contentType - * @param array $headers + * @param array $headers * @return array Headers with injected Content-Type */ private function injectContentType(string $contentType, array $headers): array diff --git a/src/Response/JsonResponse.php b/src/Response/JsonResponse.php index 275b1e9..6419c8f 100644 --- a/src/Response/JsonResponse.php +++ b/src/Response/JsonResponse.php @@ -8,9 +8,9 @@ namespace PhpComp\Http\Message\Response; -use PhpComp\Http\Message\Component\TempStream; use PhpComp\Http\Message\Response; use PhpComp\Http\Message\Stream; +use PhpComp\Http\Message\Stream\TempStream; /** * JSON response. @@ -61,9 +61,9 @@ class JsonResponse extends Response * - JSON_UNESCAPED_SLASHES * * @param mixed $data Data to convert to JSON. - * @param int $status Integer status code for the response; 200 by default. + * @param int $status Integer status code for the response; 200 by default. * @param array $headers Array of headers to use at initialization. - * @param int $encodingOptions JSON encoding options to use. + * @param int $encodingOptions JSON encoding options to use. * @throws \RuntimeException * @throws \InvalidArgumentException if unable to encode the $data to JSON. */ @@ -72,16 +72,15 @@ public function __construct( int $status = 200, array $headers = [], $encodingOptions = self::DEFAULT_JSON_FLAGS - ) - { + ) { $this->setPayload($data); $this->encodingOptions = $encodingOptions; - $json = $this->jsonEncode($data, $this->encodingOptions); - $body = $this->createBodyFromJson($json); + $json = $this->jsonEncode($data, $this->encodingOptions); + $body = $this->createBodyFromJson($json); $headers = $this->injectContentType('application/json', $headers); - parent::__construct($body, $status, $headers); + parent::__construct($status, $headers, [], $body); } /** @@ -124,7 +123,7 @@ public function getEncodingOptions(): int */ public function withEncodingOptions($encodingOptions): self { - $new = clone $this; + $new = clone $this; $new->encodingOptions = $encodingOptions; return $this->updateBodyFor($new); } @@ -149,7 +148,7 @@ private function createBodyFromJson($json): Stream * Encode the provided data to JSON. * * @param mixed $data - * @param int $encodingOptions + * @param int $encodingOptions * @return string * @throws \InvalidArgumentException if unable to encode the $data to JSON. */ diff --git a/src/Response/TextResponse.php b/src/Response/TextResponse.php index f7b373d..d7128bf 100644 --- a/src/Response/TextResponse.php +++ b/src/Response/TextResponse.php @@ -8,8 +8,8 @@ namespace PhpComp\Http\Message\Response; -use PhpComp\Http\Message\Component\TempStream; use PhpComp\Http\Message\Response; +use PhpComp\Http\Message\Stream\TempStream; use Psr\Http\Message\StreamInterface; /** @@ -33,17 +33,18 @@ class TextResponse extends Response * status of 200. * * @param string|StreamInterface $text String or stream for the message body. - * @param int $status Integer status code for the response; 200 by default. - * @param array $headers Array of headers to use at initialization. + * @param int $status Integer status code for the response; 200 by default. + * @param array $headers Array of headers to use at initialization. * @throws \RuntimeException * @throws \InvalidArgumentException if $text is neither a string or stream. */ - public function __construct($text, $status = 200, array $headers = []) + public function __construct($text, int $status = 200, array $headers = []) { parent::__construct( - $this->createBody($text), $status, - $this->injectContentType('text/plain; charset=utf-8', $headers) + $this->injectContentType('text/plain; charset=utf-8', $headers), + [], + $this->createBody($text) ); } @@ -62,7 +63,7 @@ private function createBody($text): StreamInterface } if (!\is_string($text)) { - throw new \InvalidArgumentException(sprintf( + throw new \InvalidArgumentException(\sprintf( 'Invalid content (%s) provided to %s', (\is_object($text) ? \get_class($text) : \gettype($text)), __CLASS__ diff --git a/src/ServerRequest.php b/src/ServerRequest.php index 036a046..f771298 100644 --- a/src/ServerRequest.php +++ b/src/ServerRequest.php @@ -90,7 +90,7 @@ public static function makeByParseRawData(string $rawData) $cookies = []; if (isset($headers['Cookie'])) { $cookieData = $headers['Cookie']; - $cookies = Cookies::parseFromRawHeader($cookieData); + $cookies = Cookies::parseFromRawHeader($cookieData); } $port = 80; @@ -99,12 +99,12 @@ public static function makeByParseRawData(string $rawData) [$host, $port] = strpos($val, ':') ? explode(':', $val) : [$val, 80]; } - $path = $uri; + $path = $uri; $query = $fragment = ''; if (\strlen($uri) > 1) { - $parts = parse_url($uri); - $path = $parts['path'] ?? ''; - $query = $parts['query'] ?? ''; + $parts = parse_url($uri); + $path = $parts['path'] ?? ''; + $query = $parts['query'] ?? ''; $fragment = $parts['fragment'] ?? ''; } @@ -115,15 +115,15 @@ public static function makeByParseRawData(string $rawData) /** * Request constructor. - * @param string $method - * @param UriInterface $uri - * @param string $protocol - * @param string $protocolVersion - * @param array|Headers $headers - * @param array $cookies - * @param array $serverParams + * @param string $method + * @param UriInterface $uri + * @param string $protocol + * @param string $protocolVersion + * @param array|Headers $headers + * @param array $cookies + * @param array $serverParams * @param StreamInterface $body - * @param array $uploadedFiles + * @param array $uploadedFiles * @throws \RuntimeException * @throws \InvalidArgumentException */ @@ -137,15 +137,14 @@ public function __construct( array $uploadedFiles = [], string $protocol = 'HTTP', string $protocolVersion = '1.1' - ) - { + ) { $this->setCookies($cookies); $this->initialize($protocol, $protocolVersion, $headers, $body ?: new RequestBody()); $this->initializeRequest($uri, $method); - $this->serverParams = $serverParams; + $this->serverParams = $serverParams; $this->uploadedFiles = $uploadedFiles; - $this->attributes = new Collection(); + $this->attributes = new Collection(); if (isset($serverParams['SERVER_PROTOCOL'])) { $this->protocolVersion = str_replace('HTTP/', '', $serverParams['SERVER_PROTOCOL']); @@ -160,9 +159,9 @@ public function __construct( public function __clone() { - $this->headers = clone $this->headers; + $this->headers = clone $this->headers; $this->attributes = clone $this->attributes; - $this->body = clone $this->body; + $this->body = clone $this->body; } /** @@ -192,9 +191,9 @@ protected function registerDataParsers(): void }); $xmlParser = function ($input) { - $backup = \libxml_disable_entity_loader(); + $backup = \libxml_disable_entity_loader(); $backup_errors = \libxml_use_internal_errors(true); - $result = \simplexml_load_string($input); + $result = \simplexml_load_string($input); \libxml_disable_entity_loader($backup); \libxml_clear_errors(); \libxml_use_internal_errors($backup_errors); @@ -211,7 +210,6 @@ protected function registerDataParsers(): void $this->registerMediaTypeParser('application/x-www-form-urlencoded', function ($input) { \parse_str($input, $data); - return $data; }); } @@ -242,7 +240,7 @@ public function toString(): string /** - * @param $mediaType + * @param $mediaType * @param callable $callable */ public function registerMediaTypeParser($mediaType, callable $callable): void @@ -310,7 +308,7 @@ public function setQueryParams($values): void */ public function withQueryParams(array $query) { - $clone = clone $this; + $clone = clone $this; $clone->_queryParams = $query; return $clone; @@ -319,7 +317,7 @@ public function withQueryParams(array $query) /** * Returns GET parameter with a given name. If name isn't specified, returns an array of all GET parameters. * @param string $name the parameter name - * @param mixed $defaultValue the default parameter value if the parameter does not exist. + * @param mixed $defaultValue the default parameter value if the parameter does not exist. * @return array|mixed */ public function get($name = null, $defaultValue = null) @@ -332,7 +330,7 @@ public function get($name = null, $defaultValue = null) } /** - * @param $name + * @param $name * @param null $defaultValue * @return mixed|null */ @@ -396,7 +394,7 @@ public function getParsedBody() } if (isset($this->bodyParsers[$mediaType]) === true) { - $body = (string)$this->getBody(); + $body = (string)$this->getBody(); $parsed = $this->bodyParsers[$mediaType]($body); if (null !== $parsed && !\is_object($parsed) && !\is_array($parsed)) { @@ -445,7 +443,7 @@ public function withParsedBody($data) throw new \InvalidArgumentException('Parsed body value must be an array, an object, or null'); } - $clone = clone $this; + $clone = clone $this; $clone->bodyParsed = $data; return $clone; @@ -463,14 +461,14 @@ public function setParsedBody($data): void * Fetch parameter value from request body. * Note: This method is not part of the PSR-7 standard. * @param string $key - * @param mixed $default + * @param mixed $default * @return mixed * @throws \RuntimeException */ public function getParsedBodyParam($key, $default = null) { $postParams = $this->getParsedBody(); - $result = $default; + $result = $default; if (\is_array($postParams) && isset($postParams[$key])) { $result = $postParams[$key]; @@ -508,7 +506,7 @@ public function post($name = null, $defaultValue = null) */ public function getParams(): array { - $params = $this->getQueryParams(); + $params = $this->getQueryParams(); $postParams = $this->getParsedBody(); if ($postParams) { @@ -528,8 +526,8 @@ public function getParams(): array */ public function getParam($key, $default = null) { - $result = $default; - $getParams = $this->getQueryParams(); + $result = $default; + $getParams = $this->getQueryParams(); $postParams = $this->getParsedBody(); if (\is_array($postParams) && isset($postParams[$key])) { @@ -572,7 +570,7 @@ public function setUploadedFiles(array $uploadedFiles): self */ public function withUploadedFiles(array $uploadedFiles) { - $clone = clone $this; + $clone = clone $this; $clone->uploadedFiles = $uploadedFiles; return $clone; @@ -610,7 +608,7 @@ public function getAttributes(): array * * @see getAttributes() * @param string $name The attribute name. - * @param mixed $default Default value to return if the attribute does not exist. + * @param mixed $default Default value to return if the attribute does not exist. * @return mixed */ public function getAttribute($name, $default = null) @@ -620,7 +618,7 @@ public function getAttribute($name, $default = null) /** * @param string $name - * @param mixed $value + * @param mixed $value * @return $this */ public function setAttribute(string $name, $value): self @@ -653,7 +651,7 @@ public function setAttributes(array $values): self * * @see getAttributes() * @param string $name The attribute name. - * @param mixed $value The value of the attribute. + * @param mixed $value The value of the attribute. * @return static */ public function withAttribute($name, $value) @@ -670,7 +668,7 @@ public function withAttribute($name, $value) */ public function withAttributes(array $attributes): self { - $clone = clone $this; + $clone = clone $this; $clone->attributes = new Collection($attributes); return $clone; @@ -729,12 +727,12 @@ public function getServerParams(): array * Retrieve a server parameter. * Note: This method is not part of the PSR-7 standard. * @param string $key - * @param mixed $default + * @param mixed $default * @return mixed */ public function getServerParam(string $key, $default = null) { - $key = \strtoupper($key); + $key = \strtoupper($key); $serverParams = $this->getServerParams(); return $serverParams[$key] ?? $default; diff --git a/src/Stream.php b/src/Stream.php index 3e3cae4..7db4820 100644 --- a/src/Stream.php +++ b/src/Stream.php @@ -115,7 +115,7 @@ public function __construct($stream) */ public function getMetadata($key = null) { - $this->meta = stream_get_meta_data($this->stream); + $this->meta = \stream_get_meta_data($this->stream); if (null === $key) { return $this->meta; } @@ -166,14 +166,14 @@ protected function attach($newStream): void */ public function detach() { - $oldResource = $this->stream; - $this->stream = null; - $this->meta = null; + $oldResource = $this->stream; + $this->stream = null; + $this->meta = null; $this->readable = null; $this->writable = null; $this->seekable = null; - $this->size = null; - $this->isPipe = null; + $this->size = null; + $this->isPipe = null; return $oldResource; } @@ -209,13 +209,13 @@ public function __toString() /** * Closes the stream and any underlying resources. */ - public function close() + public function close(): void { if ($this->isAttached() === true) { if ($this->isPipe()) { - pclose($this->stream); + \pclose($this->stream); } else { - fclose($this->stream); + \fclose($this->stream); } } @@ -227,10 +227,10 @@ public function close() * * @return int|null Returns the size in bytes if known, or null if unknown. */ - public function getSize() + public function getSize(): ?int { if (!$this->size && $this->isAttached() === true) { - $stats = fstat($this->stream); + $stats = fstat($this->stream); $this->size = isset($stats['size']) && !$this->isPipe() ? $stats['size'] : null; } @@ -244,9 +244,9 @@ public function getSize() * * @throws RuntimeException on error. */ - public function tell() + public function tell(): int { - if (!$this->isAttached() || ($position = ftell($this->stream)) === false || $this->isPipe()) { + if (!$this->isAttached() || ($position = \ftell($this->stream)) === false || $this->isPipe()) { throw new RuntimeException('Could not get the position of the pointer in stream'); } @@ -258,9 +258,9 @@ public function tell() * * @return bool */ - public function eof() + public function eof(): bool { - return $this->isAttached() ? feof($this->stream) : true; + return $this->isAttached() ? \feof($this->stream) : true; } /** @@ -268,17 +268,18 @@ public function eof() * * @return bool */ - public function isReadable() + public function isReadable(): bool { if ($this->readable === null) { if ($this->isPipe()) { $this->readable = true; } else { $this->readable = false; + if ($this->isAttached()) { $meta = $this->getMetadata(); foreach (self::$modes['readable'] as $mode) { - if (strpos($meta['mode'], $mode) === 0) { + if (\strpos($meta['mode'], $mode) === 0) { $this->readable = true; break; } @@ -295,10 +296,11 @@ public function isReadable() * * @return bool */ - public function isWritable() + public function isWritable(): bool { if ($this->writable === null) { $this->writable = false; + if ($this->isAttached()) { $meta = $this->getMetadata(); foreach (self::$modes['writable'] as $mode) { @@ -318,12 +320,13 @@ public function isWritable() * * @return bool */ - public function isSeekable() + public function isSeekable(): bool { if ($this->seekable === null) { $this->seekable = false; + if ($this->isAttached()) { - $meta = $this->getMetadata(); + $meta = $this->getMetadata(); $this->seekable = !$this->isPipe() && $meta['seekable']; } } @@ -345,7 +348,7 @@ public function isSeekable() * * @throws RuntimeException on failure. */ - public function seek($offset, $whence = SEEK_SET) + public function seek($offset, $whence = \SEEK_SET) { // Note that fseek returns 0 on success! if (!$this->isSeekable() || fseek($this->stream, $offset, $whence) === -1) { @@ -384,9 +387,9 @@ public function rewind() * * @throws RuntimeException if an error occurs. */ - public function read($length) + public function read($length): string { - if (!$this->isReadable() || ($data = fread($this->stream, $length)) === false) { + if (!$this->isReadable() || ($data = \fread($this->stream, $length)) === false) { throw new RuntimeException('Could not read from stream'); } @@ -402,7 +405,7 @@ public function read($length) * * @throws RuntimeException on failure. */ - public function write($string) + public function write($string): int { if (!$this->isWritable() || ($written = fwrite($this->stream, $string)) === false) { throw new RuntimeException('Could not write to stream'); @@ -422,13 +425,13 @@ public function write($string) * @throws RuntimeException if unable to read or an error occurs while * reading. */ - public function getContents() + public function getContents(): string { - if (!$this->isReadable() || ($contents = stream_get_contents($this->stream)) === false) { + if (!$this->isReadable() || ($contents = \stream_get_contents($this->stream)) === false) { throw new RuntimeException('Could not get contents of stream'); } - return $contents; + return (string)$contents; } /** @@ -440,8 +443,9 @@ public function isPipe(): bool { if ($this->isPipe === null) { $this->isPipe = false; + if ($this->isAttached()) { - $mode = fstat($this->stream)['mode']; + $mode = \fstat($this->stream)['mode']; $this->isPipe = ($mode & self::FSTAT_MODE_S_IFIFO) !== 0; } } diff --git a/src/Stream/FileStream.php b/src/Stream/FileStream.php new file mode 100644 index 0000000..b735a3b --- /dev/null +++ b/src/Stream/FileStream.php @@ -0,0 +1,30 @@ +cookies = new Cookies($cookies); return $clone; } /** - * @param string $name + * @param string $name * @param string|array $value * @return $this */ diff --git a/src/Traits/ExtendedRequestTrait.php b/src/Traits/ExtendedRequestTrait.php index f0a9f24..d1eda68 100644 --- a/src/Traits/ExtendedRequestTrait.php +++ b/src/Traits/ExtendedRequestTrait.php @@ -73,37 +73,37 @@ trait ExtendedRequestTrait */ protected static $filters = [ // return raw - 'raw' => '', + 'raw' => '', // (int)$var - 'int' => 'int', + 'int' => 'int', 'integer' => 'int', // (float)$var - 'float' => 'float', + 'float' => 'float', // (bool)$var - 'bool' => 'bool', + 'bool' => 'bool', // (bool)$var 'boolean' => 'bool', // (string)$var - 'string' => 'string', + 'string' => 'string', // (array)$var - 'array' => 'array', + 'array' => 'array', // trim($var) 'trimmed' => 'trim', // safe data - 'safe' => 'htmlspecialchars', - 'escape' => 'htmlspecialchars', + 'safe' => 'htmlspecialchars', + 'escape' => 'htmlspecialchars', // abs((int)$var) - 'number' => 'int|abs', + 'number' => 'int|abs', // will use filter_var($var ,FILTER_SANITIZE_EMAIL) - 'email' => ['filter_var', FILTER_SANITIZE_EMAIL], + 'email' => ['filter_var', FILTER_SANITIZE_EMAIL], // will use filter_var($var ,FILTER_SANITIZE_URL) - 'url' => ['filter_var', FILTER_SANITIZE_URL], + 'url' => ['filter_var', FILTER_SANITIZE_URL], // will use filter_var($var ,FILTER_SANITIZE_ENCODED, $settings); 'encoded' => ['filter_var', FILTER_SANITIZE_ENCODED], @@ -135,7 +135,7 @@ public function getUploadedFile(string $name): UploadedFile * 'password', * 'status' => 'int' * ] - * @param bool $onlyValue + * @param bool $onlyValue * @return array * @throws \InvalidArgumentException */ @@ -202,14 +202,14 @@ public function getReferrer(string $default = '/'): string /** * @param string $name - * @param array $arguments + * @param array $arguments * @return mixed * @throws \BadMethodCallException */ public function __call($name, array $arguments) { if ($arguments && 0 === \strpos($name, 'get')) { - $filter = \substr($name, 3); + $filter = \substr($name, 3); $default = $arguments[1] ?? null; return $this->get($arguments[0], $default, \lcfirst($filter)); @@ -219,7 +219,7 @@ public function __call($name, array $arguments) } /** - * @param mixed $value + * @param mixed $value * @param string|callable $filter * @return mixed|null * @throws \InvalidArgumentException @@ -292,7 +292,7 @@ public function filtering($value, $filter = null) } /** - * @param mixed $value + * @param mixed $value * @param string $filter * @return mixed * @throws \InvalidArgumentException diff --git a/src/Traits/ExtendedResponseTrait.php b/src/Traits/ExtendedResponseTrait.php index b971ede..f9e747c 100644 --- a/src/Traits/ExtendedResponseTrait.php +++ b/src/Traits/ExtendedResponseTrait.php @@ -37,7 +37,7 @@ trait ExtendedResponseTrait * This method prepares the response object to return an HTTP Redirect * response to the client. * @param string|UriInterface $url The redirect destination. - * @param int|null $status The redirect HTTP status code. + * @param int|null $status The redirect HTTP status code. * @return ResponseInterface * @throws \InvalidArgumentException */ @@ -63,8 +63,8 @@ public function withRedirect(string $url, int $status = null): ResponseInterface * This method prepares the response object to return an HTTP Json * response to the client. * @param mixed $data The data - * @param int $status The HTTP status code. - * @param int $encodingOptions Json encoding options + * @param int $status The HTTP status code. + * @param int $encodingOptions Json encoding options * @throws \InvalidArgumentException * @throws \RuntimeException * @return ResponseInterface @@ -92,7 +92,7 @@ public function withJson($data, int $status = null, int $encodingOptions = 0): R /** * @param string $fallbackUrl - * @param int $status + * @param int $status * @return ResponseInterface * @throws \InvalidArgumentException */ diff --git a/src/Traits/MessageTrait.php b/src/Traits/MessageTrait.php index 696f781..bd7b6d2 100644 --- a/src/Traits/MessageTrait.php +++ b/src/Traits/MessageTrait.php @@ -55,9 +55,9 @@ trait MessageTrait /** * BaseMessage constructor. - * @param string $protocol - * @param string $protocolVersion - * @param array|Headers $headers + * @param string $protocol + * @param string $protocolVersion + * @param array|Headers $headers * @param string|resource|StreamInterface $body * @throws \InvalidArgumentException */ @@ -67,7 +67,7 @@ public function initialize( $headers = null, $body = 'php://memory' ): void { - $this->protocol = $protocol ?: 'http'; + $this->protocol = $protocol ?: 'http'; $this->protocolVersion = $protocolVersion ?: '1.1'; if ($headers) { @@ -137,7 +137,7 @@ public function withProtocolVersion($version) ); } - $clone = clone $this; + $clone = clone $this; $clone->protocolVersion = $version; return $clone; @@ -176,7 +176,7 @@ public function getHeaderLine($name): string /** * @param string $name - * @param $value + * @param $value * @return $this */ public function setHeader(string $name, $value): self @@ -189,7 +189,7 @@ public function setHeader(string $name, $value): self /** * PSR 7 method * @param string $name - * @param $value + * @param $value * @return self */ public function withHeader($name, $value): self @@ -216,7 +216,7 @@ public function withoutHeader($name): self /** * PSR 7 method * @param string $name - * @param $value + * @param $value * @return self */ public function withAddedHeader($name, $value): self @@ -260,7 +260,7 @@ public function setHeaders(array $headers): self /** * @param string|resource|StreamInterface|bool $body - * @param string $mode + * @param string $mode * @return StreamInterface * @throws \InvalidArgumentException */ @@ -321,7 +321,7 @@ public function setBody(StreamInterface $body): self public function withBody(StreamInterface $body) { // TODO: Test for invalid body? - $clone = clone $this; + $clone = clone $this; $clone->body = $body; return $clone; @@ -335,7 +335,6 @@ public function withBody(StreamInterface $body) public function addContent(string $content): self { $this->body->write($content); - return $this; } @@ -347,7 +346,6 @@ public function addContent(string $content): self public function write(string $content): self { $this->body->write($content); - return $this; } } diff --git a/src/Traits/RequestHeadersTrait.php b/src/Traits/RequestHeadersTrait.php index 6016fa2..163af2c 100644 --- a/src/Traits/RequestHeadersTrait.php +++ b/src/Traits/RequestHeadersTrait.php @@ -90,15 +90,15 @@ public function getMediaType(): string */ public function getMediaTypeParams(): array { - $contentType = $this->getContentType(); + $contentType = $this->getContentType(); $contentTypeParams = []; if ($contentType) { - $contentTypeParts = \preg_split('/\s*[;,]\s*/', $contentType); + $contentTypeParts = \preg_split('/\s*[;,]\s*/', $contentType); $contentTypePartsLength = \count($contentTypeParts); for ($i = 1; $i < $contentTypePartsLength; $i++) { - $paramParts = \explode('=', $contentTypeParts[$i]); + $paramParts = \explode('=', $contentTypeParts[$i]); $contentTypeParams[\strtolower($paramParts[0])] = $paramParts[1]; } } diff --git a/src/Traits/RequestTrait.php b/src/Traits/RequestTrait.php index 64dac4c..8f6f6f4 100644 --- a/src/Traits/RequestTrait.php +++ b/src/Traits/RequestTrait.php @@ -46,19 +46,19 @@ trait RequestTrait */ private $validMethods = [ 'CONNECT' => 1, - 'DELETE' => 1, - 'GET' => 1, - 'HEAD' => 1, + 'DELETE' => 1, + 'GET' => 1, + 'HEAD' => 1, 'OPTIONS' => 1, - 'PATCH' => 1, - 'POST' => 1, - 'PUT' => 1, - 'TRACE' => 1, + 'PATCH' => 1, + 'POST' => 1, + 'PUT' => 1, + 'TRACE' => 1, ]; /** * @param string|UriInterface|null $uri - * @param string|null $method + * @param string|null $method * @throws \InvalidArgumentException */ protected function initializeRequest($uri = null, $method = null): void @@ -231,9 +231,9 @@ public function withMethod($method): self { $method = (string)$this->filterMethod($method); - $clone = clone $this; + $clone = clone $this; $clone->originalMethod = $method; - $clone->method = $method; + $clone->method = $method; return $clone; } @@ -287,7 +287,7 @@ public function getRequestTarget(): string return '/'; } - $path = $this->uri->getPath(); + $path = $this->uri->getPath(); $query = $this->uri->getQuery(); if ($query) { @@ -312,7 +312,7 @@ public function withRequestTarget($requestTarget): self ); } - $clone = clone $this; + $clone = clone $this; $clone->requestTarget = $requestTarget; return $clone; @@ -336,12 +336,12 @@ public function setUri(Uri $uri): void /** * @param UriInterface $uri - * @param bool $preserveHost + * @param bool $preserveHost * @return self */ public function withUri(UriInterface $uri, $preserveHost = false): self { - $clone = clone $this; + $clone = clone $this; $clone->uri = $uri; if (!$preserveHost) { diff --git a/src/UploadedFile.php b/src/UploadedFile.php index 3ca021b..95cfa81 100644 --- a/src/UploadedFile.php +++ b/src/UploadedFile.php @@ -111,11 +111,11 @@ public static function parseUploadedFiles(array $uploadedFiles): array $subArray = []; foreach ($uploadedFile['error'] as $fileIdx => $error) { // normalise subarray and re-parse to move the input's keyname up a level - $subArray[$fileIdx]['name'] = $uploadedFile['name'][$fileIdx]; - $subArray[$fileIdx]['type'] = $uploadedFile['type'][$fileIdx]; + $subArray[$fileIdx]['name'] = $uploadedFile['name'][$fileIdx]; + $subArray[$fileIdx]['type'] = $uploadedFile['type'][$fileIdx]; $subArray[$fileIdx]['tmp_name'] = $uploadedFile['tmp_name'][$fileIdx]; - $subArray[$fileIdx]['error'] = $uploadedFile['error'][$fileIdx]; - $subArray[$fileIdx]['size'] = $uploadedFile['size'][$fileIdx]; + $subArray[$fileIdx]['error'] = $uploadedFile['error'][$fileIdx]; + $subArray[$fileIdx]['size'] = $uploadedFile['size'][$fileIdx]; $parsed[$field] = static::parseUploadedFiles($subArray); } @@ -127,12 +127,12 @@ public static function parseUploadedFiles(array $uploadedFiles): array /** * Construct a new UploadedFile instance. - * @param string $file The full path to the uploaded file provided by the client. + * @param string $file The full path to the uploaded file provided by the client. * @param string|null $name The file name. * @param string|null $type The file media type. - * @param int|null $size The file size in bytes. - * @param int $error The UPLOAD_ERR_XXX code representing the status of the upload. - * @param bool $sapi Indicates if the upload is in a SAPI environment. + * @param int|null $size The file size in bytes. + * @param int $error The UPLOAD_ERR_XXX code representing the status of the upload. + * @param bool $sapi Indicates if the upload is in a SAPI environment. */ public function __construct( string $file, @@ -141,14 +141,13 @@ public function __construct( int $size = null, int $error = \UPLOAD_ERR_OK, $sapi = false - ) - { - $this->file = $file; - $this->name = $name; - $this->type = $type; - $this->size = $size; + ) { + $this->file = $file; + $this->name = $name; + $this->type = $type; + $this->size = $size; $this->error = $error; - $this->sapi = $sapi; + $this->sapi = $sapi; } /** diff --git a/src/Uri.php b/src/Uri.php index b7aae68..b1bf929 100644 --- a/src/Uri.php +++ b/src/Uri.php @@ -69,7 +69,7 @@ class Uri implements UriInterface * Create new Uri. * @param string $scheme Uri scheme. * @param string $host Uri host. - * @param int $port Uri port number. + * @param int $port Uri port number. * @param string $path Uri path. * @param string $query Uri query string. * @param string $fragment Uri fragment. @@ -86,15 +86,14 @@ public function __construct( $fragment = '', $user = '', $password = '' - ) - { - $this->scheme = $scheme ? $this->filterScheme($scheme) : ''; - $this->host = $host; - $this->port = $this->filterPort($port); - $this->path = empty($path) ? '/' : $this->filterPath($path); - $this->query = $this->filterQuery($query); + ) { + $this->scheme = $scheme ? $this->filterScheme($scheme) : ''; + $this->host = $host; + $this->port = $this->filterPort($port); + $this->path = empty($path) ? '/' : $this->filterPath($path); + $this->query = $this->filterQuery($query); $this->fragment = $this->filterQuery($fragment); - $this->user = $user; + $this->user = $user; $this->password = $password; } @@ -111,14 +110,14 @@ public static function createFromString(string $uri): self throw new \InvalidArgumentException('Uri must be a string'); } - $parts = \parse_url($uri); - $scheme = $parts['scheme'] ?? ''; - $user = $parts['user'] ?? ''; - $pass = $parts['pass'] ?? ''; - $host = $parts['host'] ?? ''; - $port = $parts['port'] ?? null; - $path = $parts['path'] ?? ''; - $query = $parts['query'] ?? ''; + $parts = \parse_url($uri); + $scheme = $parts['scheme'] ?? ''; + $user = $parts['user'] ?? ''; + $pass = $parts['pass'] ?? ''; + $host = $parts['host'] ?? ''; + $port = $parts['port'] ?? null; + $path = $parts['path'] ?? ''; + $query = $parts['query'] ?? ''; $fragment = $parts['fragment'] ?? ''; return new static($scheme, $host, $port, $path, $query, $fragment, $user, $pass); @@ -166,8 +165,8 @@ public function getScheme(): string */ public function withScheme($scheme): self { - $scheme = $this->filterScheme($scheme); - $clone = clone $this; + $scheme = $this->filterScheme($scheme); + $clone = clone $this; $clone->scheme = $scheme; return $clone; @@ -177,11 +176,11 @@ public function withScheme($scheme): self * @var array */ protected static $validScheme = [ - '' => true, + '' => true, 'https' => true, - 'http' => true, - 'ws' => true, - 'wss' => true, + 'http' => true, + 'ws' => true, + 'wss' => true, ]; /** @@ -225,8 +224,8 @@ protected function filterScheme($scheme): string public function getAuthority(): string { $userInfo = $this->getUserInfo(); - $host = $this->getHost(); - $port = $this->getPort(); + $host = $this->getHost(); + $port = $this->getPort(); return ($userInfo ? $userInfo . '@' : '') . $host . ($port !== null ? ':' . $port : ''); } @@ -254,14 +253,14 @@ public function getUserInfo(): string * Password is optional, but the user information MUST include the * user; an empty string for the user is equivalent to removing user * information. - * @param string $user The user name to use for authority. + * @param string $user The user name to use for authority. * @param null|string $password The password associated with $user. * @return self A new instance with the specified user information. */ public function withUserInfo($user, $password = null): self { - $clone = clone $this; - $clone->user = $user; + $clone = clone $this; + $clone->user = $user; $clone->password = $password ?: ''; return $clone; @@ -291,7 +290,7 @@ public function getHost(): string */ public function withHost($host): self { - $clone = clone $this; + $clone = clone $this; $clone->host = $host; return $clone; @@ -328,8 +327,8 @@ public function getPort() */ public function withPort($port): self { - $port = $this->filterPort($port); - $clone = clone $this; + $port = $this->filterPort($port); + $clone = clone $this; $clone->port = $port; return $clone; @@ -411,7 +410,7 @@ public function withPath($path): self throw new \InvalidArgumentException('Uri path must be a string'); } - $clone = clone $this; + $clone = clone $this; $clone->path = $this->filterPath($path); return $clone; @@ -474,8 +473,8 @@ public function withQuery($query): self if (!\is_string($query) && !method_exists($query, '__toString')) { throw new \InvalidArgumentException('Uri query must be a string'); } - $query = ltrim((string)$query, '?'); - $clone = clone $this; + $query = ltrim((string)$query, '?'); + $clone = clone $this; $clone->query = $this->filterQuery($query); return $clone; @@ -535,8 +534,8 @@ public function withFragment($fragment): self throw new \InvalidArgumentException('Uri fragment must be a string'); } - $fragment = ltrim((string)$fragment, '#'); - $clone = clone $this; + $fragment = ltrim((string)$fragment, '#'); + $clone = clone $this; $clone->fragment = $this->filterQuery($fragment); return $clone; @@ -568,11 +567,11 @@ public function withFragment($fragment): self */ public function __toString() { - $scheme = $this->getScheme(); + $scheme = $this->getScheme(); $authority = $this->getAuthority(); // $basePath = $this->getBasePath(); - $path = $this->getPath(); - $query = $this->getQuery(); + $path = $this->getPath(); + $query = $this->getQuery(); $fragment = $this->getFragment(); $path = '/' . ltrim($path, '/'); @@ -592,7 +591,7 @@ public function __toString() */ public function getBaseUrl(): string { - $scheme = $this->getScheme(); + $scheme = $this->getScheme(); $authority = $this->getAuthority(); // $basePath = $this->getBasePath(); //