File tree Expand file tree Collapse file tree 2 files changed +84
-0
lines changed
Expand file tree Collapse file tree 2 files changed +84
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments