Skip to content

Commit

Permalink
Adding missing PHP Uri interface methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Dec 25, 2024
1 parent 8ed65c4 commit 4a09e22
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions uri/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 25 additions & 1 deletion uri/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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.
*
Expand Down
13 changes: 13 additions & 0 deletions uri/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:[email protected]:81/path?query#fragment');
/** @var Uri $newUri */
$newUri = unserialize(serialize($uri));

self::assertTrue($uri->equals($newUri, excludeFragment: false));
}
}

0 comments on commit 4a09e22

Please sign in to comment.