From f87277c6f93355a9bafe2c6d0c369abec9d32891 Mon Sep 17 00:00:00 2001 From: otsch Date: Sat, 21 Oct 2023 15:33:53 +0200 Subject: [PATCH] Add new method Url::toPsr7() To conveniently convert a `Url` instance into a PSR-7 compatible one. --- CHANGELOG.md | 4 ++++ src/Url.php | 6 ++++++ tests/UrlTest.php | 11 +++++++++++ 3 files changed, 21 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a97a18b..23ffa60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [2.1.0] - 2023-10-21 +### Added +- Method `toPsr7()` to conveniently get a PSR-7 compatible instance from an `Url` instance. Basically wrapper for `new Uri($urlInstance)`. + ## [2.0.5] - 2023-09-19 ### Fixed - Issue with resolving relative URLs. diff --git a/src/Url.php b/src/Url.php index f62a9cc..72c2db4 100644 --- a/src/Url.php +++ b/src/Url.php @@ -8,6 +8,7 @@ use Crwlr\Url\Psr\Uri; use Exception; use InvalidArgumentException; +use Psr\Http\Message\UriInterface; /** * Class Url @@ -728,6 +729,11 @@ public function toString(): string return $this->url; } + public function toPsr7(): UriInterface + { + return new Uri($this); + } + /** * Populate the URL components from an array or another instance of this class * diff --git a/tests/UrlTest.php b/tests/UrlTest.php index daac985..dbe924b 100644 --- a/tests/UrlTest.php +++ b/tests/UrlTest.php @@ -4,6 +4,7 @@ use Crwlr\Url\Exceptions\InvalidUrlException; use Crwlr\Url\Psr\Uri; use Crwlr\Url\Url; +use Psr\Http\Message\UriInterface; /** @var \PHPUnit\Framework\TestCase $this */ @@ -948,6 +949,16 @@ ); }); +it('converts an Url instance into a PSR-7 compatible instance', function () { + $url = Url::parse('https://www.crwl.io/en/home'); + + expect($url) + ->not() + ->toBeInstanceOf(UriInterface::class) + ->and($url->toPsr7()) + ->toBeInstanceOf(UriInterface::class); +}); + function createDefaultUrlObject(): Url { return new Url('https://user:password@sub.sub.example.com:8080/some/path?some=query#fragment');