Skip to content

Commit e6884e5

Browse files
committed
Create PathSerializer
1 parent b3cca74 commit e6884e5

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Redmine\Serializer;
4+
5+
/**
6+
* PathSerializer
7+
*/
8+
final class PathSerializer
9+
{
10+
public static function create(string $path, array $queryParams = []): self
11+
{
12+
$serializer = new self();
13+
$serializer->path = $path;
14+
$serializer->queryParams = $queryParams;
15+
16+
return $serializer;
17+
}
18+
19+
private string $path;
20+
21+
private array $queryParams;
22+
23+
private function __construct()
24+
{
25+
// use factory method instead
26+
}
27+
28+
public function getPath(): string
29+
{
30+
$queryString = '';
31+
32+
if (! empty($this->queryParams)) {
33+
$queryString = '?' . \http_build_query($this->queryParams);
34+
}
35+
36+
return $this->path . $queryString;
37+
}
38+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Redmine\Tests\Unit\Serializer;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Redmine\Serializer\PathSerializer;
9+
10+
class PathSerializerTest extends TestCase
11+
{
12+
public function getPathData()
13+
{
14+
return [
15+
[
16+
'foo.json',
17+
[],
18+
'foo.json'
19+
],
20+
[
21+
'foo.xml?format=xml',
22+
[],
23+
'foo.xml?format=xml'
24+
],
25+
[
26+
'foo.xml',
27+
[
28+
'format' => 'xml',
29+
],
30+
'foo.xml?format=xml'
31+
],
32+
];
33+
}
34+
35+
/**
36+
* @test
37+
*
38+
* @dataProvider getPathData
39+
*/
40+
public function getPathShouldReturn(string $path, array $params, string $expected)
41+
{
42+
$serializer = PathSerializer::create($path, $params);
43+
44+
$this->assertSame($expected, $serializer->getPath());
45+
}
46+
}

0 commit comments

Comments
 (0)