Skip to content

Commit

Permalink
New method Uri::create($uri, $ref = null): Uri to create an uri wit…
Browse files Browse the repository at this point in the history
…h reference
  • Loading branch information
ElGigi committed Dec 23, 2021
1 parent 0fed37f commit 8d6e385
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 72 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. This projec
to [Semantic Versioning] (http://semver.org/). For change log format,
use [Keep a Changelog] (http://keepachangelog.com/).

## [2.1.0] - 2021-12-23

### Added

- New method `Uri::create($uri, $ref = null): Uri` to create an uri with reference

## [2.0.2] - 2021-10-07

### Fixed
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"require": {
"php": "^8.0",
"ext-fileinfo": "*",
"berlioz/helpers": "^1.5",
"psr/http-message": "^1.0",
"psr/http-factory": "^1.0"
},
Expand Down
50 changes: 40 additions & 10 deletions src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,42 @@ public function __construct(
}

/**
* Create Uri object with string
* Create Uri.
*
* @param Uri|string $uri
* @param Uri|string|null $ref
*
* @return static
*/
public static function create(Uri|string $uri, Uri|string|null $ref = null): static
{
is_string($uri) && $uri = static::createFromString($uri);

if (!empty($uri->getHost()) || null === $ref) {
return $uri;
}

is_string($ref) && $ref = static::createFromString($ref);
$userInfo = explode(':', $ref->getUserInfo(), 2);

return new static(
scheme: $ref->getScheme(),
host: $ref->getHost(),
port: $ref->getPort(),
path: b_resolve_absolute_path($ref->getPath() ?: '/', $uri->getPath() ?: '/'),
query: $uri->getQuery(),
fragment: $uri->getFragment(),
user: $userInfo[0] ?? '',
password: $userInfo[1] ?? '',
);
}

/**
* Create Uri with string
*
* @param string $str
*
* @return static
* @throws InvalidArgumentException If string cannot be parsed
*/
public static function createFromString(string $str): static
{
Expand All @@ -63,14 +93,14 @@ public static function createFromString(string $str): static
}

return new self(
$parsedUrl['scheme'] ?? '',
$parsedUrl['host'] ?? '',
$parsedUrl['port'] ?? null,
$parsedUrl['path'] ?? '/',
$parsedUrl['query'] ?? '',
$parsedUrl['fragment'] ?? '',
$parsedUrl['user'] ?? '',
$parsedUrl['pass'] ?? ''
scheme: $parsedUrl['scheme'] ?? '',
host: $parsedUrl['host'] ?? '',
port: $parsedUrl['port'] ?? null,
path: $parsedUrl['path'] ?? '/',
query: $parsedUrl['query'] ?? '',
fragment: $parsedUrl['fragment'] ?? '',
user: $parsedUrl['user'] ?? '',
password: $parsedUrl['pass'] ?? ''
);
}

Expand Down
165 changes: 103 additions & 62 deletions tests/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,58 +20,75 @@ class UriTest extends TestCase
public function uriDataProvider()
{
return [// Default values
[new Uri('http', 'www.berlioz-framework.com'),
['scheme' => 'http',
'host' => 'www.berlioz-framework.com',
'port' => null,
'path' => '/',
'query' => '',
'fragment' => '',
'userinfo' => '',
'authority' => 'www.berlioz-framework.com'],
'http://www.berlioz-framework.com/'],
// Default password parameter
[new Uri('https',
'www.berlioz-framework.com',
8080,
'/path/path/index.php',
'test=test&test2=test2',
'fragmentTest',
'elgigi'),
['scheme' => 'https',
'host' => 'www.berlioz-framework.com',
'port' => 8080,
'path' => '/path/path/index.php',
'query' => 'test=test&test2=test2',
'fragment' => 'fragmentTest',
'userinfo' => 'elgigi',
'authority' => '[email protected]:8080'],
'https://[email protected]:8080/path/path/index.php?test=test&test2=test2#fragmentTest'],
// Complete constructor
[new Uri('https',
'www.berlioz-framework.com',
8080,
'/path/path/index.php',
'test=test&test2=test2',
'fragmentTest',
'elgigi',
'password'),
['scheme' => 'https',
'host' => 'www.berlioz-framework.com',
'port' => 8080,
'path' => '/path/path/index.php',
'query' => 'test=test&test2=test2',
'fragment' => 'fragmentTest',
'userinfo' => 'elgigi:password',
'authority' => 'elgigi:[email protected]:8080'],
'https://elgigi:[email protected]:8080/path/path/index.php?test=test&test2=test2#fragmentTest']];
[
new Uri('http', 'www.berlioz-framework.com'),
[
'scheme' => 'http',
'host' => 'www.berlioz-framework.com',
'port' => null,
'path' => '/',
'query' => '',
'fragment' => '',
'userinfo' => '',
'authority' => 'www.berlioz-framework.com'
],
'http://www.berlioz-framework.com/'
],
// Default password parameter
[
new Uri(
'https',
'www.berlioz-framework.com',
8080,
'/path/path/index.php',
'test=test&test2=test2',
'fragmentTest',
'elgigi'
),
[
'scheme' => 'https',
'host' => 'www.berlioz-framework.com',
'port' => 8080,
'path' => '/path/path/index.php',
'query' => 'test=test&test2=test2',
'fragment' => 'fragmentTest',
'userinfo' => 'elgigi',
'authority' => '[email protected]:8080'
],
'https://[email protected]:8080/path/path/index.php?test=test&test2=test2#fragmentTest'
],
// Complete constructor
[
new Uri(
'https',
'www.berlioz-framework.com',
8080,
'/path/path/index.php',
'test=test&test2=test2',
'fragmentTest',
'elgigi',
'password'
),
[
'scheme' => 'https',
'host' => 'www.berlioz-framework.com',
'port' => 8080,
'path' => '/path/path/index.php',
'query' => 'test=test&test2=test2',
'fragment' => 'fragmentTest',
'userinfo' => 'elgigi:password',
'authority' => 'elgigi:[email protected]:8080'
],
'https://elgigi:[email protected]:8080/path/path/index.php?test=test&test2=test2#fragmentTest'
]
];
}

/**
* Test constructor and getters.
*
* @param \Berlioz\Http\Message\Uri $uri Uri
* @param array $uriValues Values to test
* @param \Berlioz\Http\Message\Uri $uri Uri
* @param array $uriValues Values to test
*
* @dataProvider uriDataProvider
*/
Expand All @@ -90,9 +107,9 @@ public function testConstructAndGetters(Uri $uri, array $uriValues)
/**
* Test static method "createFromString".
*
* @param \Berlioz\Http\Message\Uri $uri Uri
* @param array $uriValues Values to test
* @param string $stringUri String uri
* @param \Berlioz\Http\Message\Uri $uri Uri
* @param array $uriValues Values to test
* @param string $stringUri String uri
*
* @dataProvider uriDataProvider
*/
Expand All @@ -103,16 +120,40 @@ public function testCreateFromString(Uri $uri, array $uriValues, string $stringU
$this->testConstructAndGetters($newUri, $uriValues);
}

public function testCreate()
{
$uri = Uri::createFromString('../qux?foo#bar');
$ref = Uri::createFromString('https://elgigi:[email protected]:8080/doc/#qux');

$newUri = Uri::create($uri, $ref);

$this->testConstructAndGetters(
$newUri,
[
'scheme' => 'https',
'host' => 'getberlioz.com',
'port' => 8080,
'path' => '/qux',
'query' => 'foo',
'fragment' => 'bar',
'userinfo' => 'elgigi:password',
'authority' => 'elgigi:[email protected]:8080'
]
);
}

private function getUriToTest(): Uri
{
return new Uri('http',
'www.berlioz-framework.com',
null,
'/path/path/index.php',
'test=test&test2=test2',
'fragmentTest',
'elgigi',
'password');
return new Uri(
'http',
'www.berlioz-framework.com',
null,
'/path/path/index.php',
'test=test&test2=test2',
'fragmentTest',
'elgigi',
'password'
);
}

public function testWithScheme()
Expand Down Expand Up @@ -184,14 +225,14 @@ public function testWithFragment()
/**
* Test magic method "__toString".
*
* @param \Berlioz\Http\Message\Uri $uri Uri
* @param array $uriValues Values to test
* @param string $stringUri String uri
* @param \Berlioz\Http\Message\Uri $uri Uri
* @param array $uriValues Values to test
* @param string $stringUri String uri
*
* @dataProvider uriDataProvider
*/
public function testToString(Uri $uri, array $uriValues, string $stringUri)
{
$this->assertEquals($stringUri, (string) $uri);
$this->assertEquals($stringUri, (string)$uri);
}
}

0 comments on commit 8d6e385

Please sign in to comment.