Skip to content

Commit 08894ff

Browse files
committed
Added UriHelper::mapReplace method
1 parent 7ca9b02 commit 08894ff

File tree

4 files changed

+77
-61
lines changed

4 files changed

+77
-61
lines changed

composer.lock

+12-61
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpunit.xml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<phpunit
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd"
4+
colors="true">
5+
<testsuites>
6+
<testsuite name="Default">
7+
<directory>./test</directory>
8+
</testsuite>
9+
</testsuites>
10+
</phpunit>

src/tools/UriHelper.php

+31
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Francerz\Http\Tools;
44

5+
use LogicException;
56
use Psr\Http\Message\UriFactoryInterface;
67
use Psr\Http\Message\UriInterface;
78

@@ -126,4 +127,34 @@ public static function getCurrent(UriFactoryInterface $uriFactory) : UriInterfac
126127
$uri.= '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
127128
return $uriFactory->createUri($uri);
128129
}
130+
131+
private static function mapReplaceString(string $uri, array $replaces, bool $encode_values = true) : string
132+
{
133+
$match = preg_match_all('/\{([a-zA-Z0-9\-\_]+)\}/S', $uri, $matches);
134+
if (!$match) {
135+
return $uri;
136+
}
137+
$matches = array_unique($matches[1]);
138+
foreach ($matches as $match) {
139+
if (!array_key_exists($match, $replaces)) {
140+
continue;
141+
}
142+
$val = $encode_values ? urlencode($replaces[$match]) : $replaces[$match];
143+
$uri = str_replace('{'.$match.'}', $val, $uri);
144+
}
145+
return $uri;
146+
}
147+
148+
public static function mapReplace(UriFactoryInterface $uriFactory, $uri, array $replaces, bool $encode_values = true) : UriInterface
149+
{
150+
$uriStr = $uri;
151+
if ($uri instanceof UriInterface) {
152+
$uriStr = (string) $uri;
153+
}
154+
if (!is_string($uriStr)) {
155+
throw new LogicException(__METHOD__.' $uri argument must be string or UriInterface object');
156+
}
157+
$uriStr = static::mapReplaceString($uri, $replaces, $encode_values);
158+
return $uriFactory->createUri($uriStr);
159+
}
129160
}

test/UriHelperTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
use Francerz\Http\Tools\UriHelper;
4+
use PHPUnit\Framework\TestCase;
5+
6+
class UriHelperTest extends TestCase
7+
{
8+
private static function callPrivateStatic($obj, string $method, ...$args)
9+
{
10+
$ref = new ReflectionClass($obj);
11+
$method = $ref->getMethod($method);
12+
$method->setAccessible(true);
13+
return $method->invokeArgs(null, $args);
14+
}
15+
public function testUriMapping()
16+
{
17+
$uri = static::callPrivateStatic(UriHelper::class, 'mapReplaceString',
18+
'https://example.com/collection/{id}/{id2}',
19+
['id'=>20, 'id2'=>30]
20+
);
21+
22+
$this->assertEquals('https://example.com/collection/20/30', $uri);
23+
}
24+
}

0 commit comments

Comments
 (0)