Skip to content

Commit

Permalink
Merge pull request #49 from MaSpeng/php-8-1-compatibility
Browse files Browse the repository at this point in the history
PHP 8.1 Compatibility
  • Loading branch information
freekmurze authored Dec 29, 2021
2 parents 5f0a81b + 36d4a7f commit 4b9b58a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/Helpers/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
7 changes: 4 additions & 3 deletions src/QueryParameterBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Spatie\Url\Helpers\Arr;

class QueryParameterBag
class QueryParameterBag implements \Stringable
{
public function __construct(
protected array $parameters = [],
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
27 changes: 14 additions & 13 deletions src/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -234,7 +235,7 @@ public function withHost($host): self
return $url;
}

public function withPort($port): self
public function withPort($port): static
{
$url = clone $this;

Expand All @@ -243,7 +244,7 @@ public function withPort($port): self
return $url;
}

public function withPath($path): self
public function withPath($path): static
{
$url = clone $this;

Expand All @@ -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, '/');

Expand All @@ -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, '/');

Expand All @@ -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;

Expand All @@ -287,7 +288,7 @@ public function withQuery($query): self
return $url;
}

public function withFragment($fragment): self
public function withFragment($fragment): static
{
$url = clone $this;

Expand Down

0 comments on commit 4b9b58a

Please sign in to comment.