diff --git a/src/Helpers/Arr.php b/src/Helpers/Arr.php index c09de17..c58ccf1 100644 --- a/src/Helpers/Arr.php +++ b/src/Helpers/Arr.php @@ -13,9 +13,9 @@ public static function map(array $items, callable $callback): array return array_combine($keys, $items); } - public static function mapToAssoc(array $items, callable $callback): mixed + public static function mapToAssoc(array $items, callable $callback): array { - return array_reduce($items, function (array $assoc, $item) use ($callback) { + return array_reduce($items, function (array $assoc, $item) use ($callback): array { [$key, $value] = $callback($item); $assoc[$key] = $value; diff --git a/src/QueryParameterBag.php b/src/QueryParameterBag.php index 4804ac6..6482320 100644 --- a/src/QueryParameterBag.php +++ b/src/QueryParameterBag.php @@ -4,7 +4,7 @@ use Spatie\Url\Helpers\Arr; -class QueryParameterBag +class QueryParameterBag implements \Stringable { public function __construct( protected array $parameters = [], @@ -18,7 +18,8 @@ public static function fromString(string $query = ''): static return new static(); } - return new static(Arr::mapToAssoc(explode('&', $query), function (string $keyValue) { + return new static(Arr::mapToAssoc(explode('&', $query), function (string $keyValue): array + { $parts = explode('=', $keyValue, 2); return count($parts) === 2 @@ -60,7 +61,7 @@ public function __toString(): string { $keyValuePairs = Arr::map( $this->parameters, - fn ($value, $key) => "{$key}=".rawurlencode($value) + fn ($value, $key): string => "{$key}=".rawurlencode($value) ); return implode('&', $keyValuePairs); diff --git a/src/Url.php b/src/Url.php index a332e55..cc0e78d 100644 --- a/src/Url.php +++ b/src/Url.php @@ -5,8 +5,9 @@ use Psr\Http\Message\UriInterface; use Spatie\Macroable\Macroable; use Spatie\Url\Exceptions\InvalidArgument; +use Stringable; -class Url implements UriInterface +class Url implements UriInterface, Stringable { use Macroable; @@ -122,7 +123,7 @@ public function getQuery(): string return (string) $this->query; } - public function getQueryParameter(string $key, $default = null): mixed + public function getQueryParameter(string $key, mixed $default = null): mixed { return $this->query->get($key, $default); } @@ -137,7 +138,7 @@ public function getAllQueryParameters(): array return $this->query->all(); } - public function withQueryParameter(string $key, string $value): self + public function withQueryParameter(string $key, string $value): static { $url = clone $this; $url->query->unset($key); @@ -147,7 +148,7 @@ public function withQueryParameter(string $key, string $value): self return $url; } - public function withoutQueryParameter(string $key): self + public function withoutQueryParameter(string $key): static { $url = clone $this; $url->query->unset($key); @@ -195,7 +196,7 @@ public function getLastSegment(): mixed return end($segments) ?? null; } - public function withScheme($scheme): self + public function withScheme($scheme): static { $url = clone $this; @@ -215,7 +216,7 @@ protected function sanitizeScheme(string $scheme): string return $scheme; } - public function withUserInfo($user, $password = null): self + public function withUserInfo($user, $password = null): static { $url = clone $this; @@ -225,7 +226,7 @@ public function withUserInfo($user, $password = null): self return $url; } - public function withHost($host): self + public function withHost($host): static { $url = clone $this; @@ -234,7 +235,7 @@ public function withHost($host): self return $url; } - public function withPort($port): self + public function withPort($port): static { $url = clone $this; @@ -243,7 +244,7 @@ public function withPort($port): self return $url; } - public function withPath($path): self + public function withPath($path): static { $url = clone $this; @@ -256,7 +257,7 @@ public function withPath($path): self return $url; } - public function withDirname(string $dirname): self + public function withDirname(string $dirname): static { $dirname = trim($dirname, '/'); @@ -267,7 +268,7 @@ public function withDirname(string $dirname): self return $this->withPath($dirname.'/'.$this->getBasename()); } - public function withBasename(string $basename): self + public function withBasename(string $basename): static { $basename = trim($basename, '/'); @@ -278,7 +279,7 @@ public function withBasename(string $basename): self return $this->withPath($this->getDirname().'/'.$basename); } - public function withQuery($query): self + public function withQuery($query): static { $url = clone $this; @@ -287,7 +288,7 @@ public function withQuery($query): self return $url; } - public function withFragment($fragment): self + public function withFragment($fragment): static { $url = clone $this;