From 0d1e682ad93c0dcef7c2c9ffae53c9f72765dc69 Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Sun, 22 Nov 2020 15:26:58 +0100 Subject: [PATCH] Adding HttpFactory class --- CHANGELOG.md | 4 ++-- composer.json | 8 +++++--- src/HttpFactory.php | 25 +++++++++++++++++++++++++ tests/HttpFactoryTest.php | 30 ++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 src/HttpFactory.php create mode 100644 tests/HttpFactoryTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 91b75e49..e127aab8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,11 +2,11 @@ All Notable changes to `League\Uri` will be documented in this file -## 6.3.1 - 2020-11-23 +## 6.4.0 - 2020-11-23 ### Added -- None +- `HttpFactory` a class that implements PSR-17 UriFactoryInterface. The package needs to be present for the class to work. ### Fixed diff --git a/composer.json b/composer.json index 29eb50f8..50cfedaa 100644 --- a/composer.json +++ b/composer.json @@ -44,7 +44,7 @@ } ], "require": { - "php": "^7.2 || ^8.0", + "php": ">=7.2", "ext-json": "*", "psr/http-message": "^1.0", "league/uri-interfaces": "^2.1" @@ -54,7 +54,8 @@ "phpunit/phpunit" : "^8.0 || ^9.0", "phpstan/phpstan": "^0.12", "phpstan/phpstan-strict-rules": "^0.12", - "phpstan/phpstan-phpunit": "^0.12" + "phpstan/phpstan-phpunit": "^0.12", + "psr/http-factory": "^1.0" }, "autoload": { "psr-4": { @@ -95,7 +96,8 @@ "suggest": { "league/uri-components" : "Needed to easily manipulate URI objects", "ext-intl" : "Needed to improve host validation", - "ext-fileinfo": "Needed to create Data URI from a filepath" + "ext-fileinfo": "Needed to create Data URI from a filepath", + "psr/http-factory": "Needed to use the URI factory" }, "extra": { "branch-alias": { diff --git a/src/HttpFactory.php b/src/HttpFactory.php new file mode 100644 index 00000000..fc3bcfab --- /dev/null +++ b/src/HttpFactory.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace League\Uri; + +use Psr\Http\Message\UriFactoryInterface; +use Psr\Http\Message\UriInterface; + +final class HttpFactory implements UriFactoryInterface +{ + public function createUri(string $uri = ''): UriInterface + { + return Http::createFromString($uri); + } +} diff --git a/tests/HttpFactoryTest.php b/tests/HttpFactoryTest.php new file mode 100644 index 00000000..f7f4cbeb --- /dev/null +++ b/tests/HttpFactoryTest.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace LeagueTest\Uri; + +use League\Uri\Http; +use League\Uri\HttpFactory; +use PHPUnit\Framework\TestCase; + +final class HttpFactoryTest extends TestCase +{ + public function testCreateUri(): void + { + $factory = new HttpFactory(); + $uri = $factory->createUri('https://nyholm.tech/foo'); + + self::assertInstanceOf(Http::class, $uri); + self::assertEquals('https://nyholm.tech/foo', $uri->__toString()); + } +}