Skip to content

Commit

Permalink
Add new method Url::toPsr7()
Browse files Browse the repository at this point in the history
To conveniently convert a `Url` instance into a PSR-7 compatible one.
  • Loading branch information
otsch committed Oct 21, 2023
1 parent 78d3f06 commit edea556
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions src/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Crwlr\Url\Psr\Uri;
use Exception;
use InvalidArgumentException;
use Psr\Http\Message\UriInterface;

/**
* Class Url
Expand Down Expand Up @@ -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
*
Expand Down
11 changes: 11 additions & 0 deletions tests/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down Expand Up @@ -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:[email protected]:8080/some/path?some=query#fragment');
Expand Down

0 comments on commit edea556

Please sign in to comment.