From 4a09e22270b979dcd5f09bdef9db3f09c4ec8aa6 Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Wed, 25 Dec 2024 22:59:36 +0100 Subject: [PATCH] Adding missing PHP Uri interface methods --- uri/CHANGELOG.md | 1 + uri/Uri.php | 26 +++++++++++++++++++++++++- uri/UriTest.php | 13 +++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/uri/CHANGELOG.md b/uri/CHANGELOG.md index def18a3b..adc2246d 100644 --- a/uri/CHANGELOG.md +++ b/uri/CHANGELOG.md @@ -32,6 +32,7 @@ All Notable changes to `League\Uri` will be documented in this file - `Uri::getUser` to be inline with PHP native URI interface - `Uri::withUser` to be inline with PHP native URI interface - `Uri::withPassword` to be inline with PHP native URI interface +- `Uri::__serialize` and `Uri::__unserialize` methods ### Fixed diff --git a/uri/Uri.php b/uri/Uri.php index 679b0363..9a640add 100644 --- a/uri/Uri.php +++ b/uri/Uri.php @@ -265,7 +265,6 @@ private function __construct( $this->authority = UriString::buildAuthority($this->toComponents()); $this->uri = UriString::buildUri($this->scheme, $this->authority, $this->path, $this->query, $this->fragment); $this->assertValidState(); - $this->origin = $this->setOrigin(); } @@ -1812,6 +1811,31 @@ private static function getSegments(string $path): array }); } + public function __serialize(): array + { + return $this->toComponents(); + } + + /** + * @param ComponentMap $data + */ + public function __unserialize(array $data): void + { + $this->scheme = $this->formatScheme($data['scheme'] ?? null); + $this->user = Encoder::encodeUser($data['user'] ?? null); + $this->pass = Encoder::encodePassword($data['pass'] ?? null); + $this->host = $this->formatHost($data['host'] ?? null); + $this->port = $this->formatPort($data['port'] ?? null); + $this->path = $this->formatPath($data['path'] ?? ''); + $this->query = Encoder::encodeQueryOrFragment($data['query'] ?? null); + $this->fragment = Encoder::encodeQueryOrFragment($data['fragment'] ?? null); + $this->userInfo = $this->formatUserInfo($this->user, $this->pass); + $this->authority = UriString::buildAuthority($this->toComponents()); + $this->uri = UriString::buildUri($this->scheme, $this->authority, $this->path, $this->query, $this->fragment); + $this->assertValidState(); + $this->origin = $this->setOrigin(); + } + /** * DEPRECATION WARNING! This method will be removed in the next major point release. * diff --git a/uri/UriTest.php b/uri/UriTest.php index fab6cc87..f5fb4faa 100644 --- a/uri/UriTest.php +++ b/uri/UriTest.php @@ -23,6 +23,9 @@ use Psr\Http\Message\UriInterface as Psr7UriInterface; use TypeError; +use function serialize; +use function unserialize; + #[CoversClass(Uri::class)] #[Group('uri')] class UriTest extends TestCase @@ -1098,4 +1101,14 @@ public function it_requires_a_user_component_to_update_the_password_component(): Uri::new('example://host/path?query')->withPassword('pass'); } + + #[Test] + public function it_can_be_serialized_by_php(): void + { + $uri = Uri::new('https://user:pass@example.com:81/path?query#fragment'); + /** @var Uri $newUri */ + $newUri = unserialize(serialize($uri)); + + self::assertTrue($uri->equals($newUri, excludeFragment: false)); + } }