1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Rest\ServerBundle\Translator; |
5
|
|
|
|
6
|
|
|
use Innmind\Rest\ServerBundle\Exception\UnexpectedValueException; |
7
|
|
|
use Innmind\Rest\Server\{ |
8
|
|
|
Definition\Locator, |
9
|
|
|
Reference, |
10
|
|
|
Identity\Identity, |
11
|
|
|
Link\Parameter |
12
|
|
|
}; |
13
|
|
|
use Innmind\Http\Header\{ |
14
|
|
|
Link, |
15
|
|
|
LinkValue, |
16
|
|
|
Parameter as HttpParameter |
17
|
|
|
}; |
18
|
|
|
use Innmind\Immutable\{ |
19
|
|
|
MapInterface, |
20
|
|
|
Map |
21
|
|
|
}; |
22
|
|
|
use Symfony\Component\Routing\RouterInterface; |
23
|
|
|
|
24
|
|
|
final class LinkTranslator |
25
|
|
|
{ |
26
|
|
|
private $locate; |
27
|
|
|
private $router; |
28
|
|
|
|
29
|
16 |
|
public function __construct(Locator $locator, RouterInterface $router) |
30
|
|
|
{ |
31
|
16 |
|
$this->locate = $locator; |
32
|
16 |
|
$this->router = $router; |
33
|
16 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return MapInterface<Reference, MapInterface<string, Parameter>> |
37
|
|
|
*/ |
38
|
8 |
|
public function translate(Link $link): MapInterface |
39
|
|
|
{ |
40
|
|
|
return $link |
41
|
8 |
|
->values() |
42
|
8 |
|
->reduce( |
43
|
8 |
|
new Map(Reference::class, MapInterface::class), |
44
|
8 |
|
function(Map $carry, LinkValue $link): Map { |
45
|
8 |
|
list($reference, $parameters) = $this->translateLinkValue($link); |
46
|
|
|
|
47
|
6 |
|
return $carry->put($reference, $parameters); |
48
|
8 |
|
} |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return array<Reference, MapInterface<string, Parameter>> |
|
|
|
|
54
|
|
|
*/ |
55
|
8 |
|
private function translateLinkValue(LinkValue $link): array |
56
|
|
|
{ |
57
|
8 |
|
$infos = $this->router->match((string) $link->url()); |
58
|
|
|
|
59
|
|
|
if ( |
60
|
8 |
|
!isset($infos['_innmind_resource']) || |
61
|
8 |
|
!isset($infos['identity']) |
62
|
|
|
) { |
63
|
2 |
|
throw new UnexpectedValueException; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return [ |
67
|
6 |
|
new Reference( |
68
|
6 |
|
($this->locate)($infos['_innmind_resource']), |
69
|
6 |
|
new Identity($infos['identity']) |
70
|
|
|
), |
71
|
|
|
$this |
72
|
6 |
|
->translateParameters($link->parameters()) |
73
|
6 |
|
->put('rel', new Parameter\Parameter('rel', $link->relationship())) |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return MapInterface<string, Parameter> |
|
|
|
|
79
|
|
|
*/ |
80
|
6 |
|
private function translateParameters(MapInterface $parameters): MapInterface |
81
|
|
|
{ |
82
|
6 |
|
return $parameters->reduce( |
83
|
6 |
|
new Map('string', Parameter::class), |
84
|
6 |
|
function(Map $carry, string $name, HttpParameter $param): Map { |
85
|
2 |
|
return $carry->put( |
86
|
2 |
|
$name, |
87
|
2 |
|
new Parameter\Parameter($name, $param->value()) |
88
|
|
|
); |
89
|
6 |
|
} |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.