|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hateoas\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use Hateoas\Configuration\Relation; |
|
6
|
|
|
use Hateoas\Configuration\Route; |
|
7
|
|
|
use Hateoas\Expression\ExpressionEvaluator; |
|
8
|
|
|
use Hateoas\Model\Link; |
|
9
|
|
|
use Hateoas\UrlGenerator\UrlGeneratorRegistry; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @author Adrien Brault <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class LinkFactory |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var ExpressionEvaluator |
|
18
|
|
|
*/ |
|
19
|
|
|
private $expressionEvaluator; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var UrlGeneratorRegistry |
|
23
|
|
|
*/ |
|
24
|
|
|
private $urlGeneratorRegistry; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param ExpressionEvaluator $expressionEvaluator |
|
28
|
|
|
* @param UrlGeneratorRegistry $urlGeneratorRegistry |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(ExpressionEvaluator $expressionEvaluator, UrlGeneratorRegistry $urlGeneratorRegistry) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->expressionEvaluator = $expressionEvaluator; |
|
33
|
|
|
$this->urlGeneratorRegistry = $urlGeneratorRegistry; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param object $object |
|
38
|
|
|
* @param Relation $relation |
|
39
|
|
|
* |
|
40
|
|
|
* @return Link |
|
41
|
|
|
*/ |
|
42
|
|
|
public function createLink($object, Relation $relation) |
|
43
|
|
|
{ |
|
44
|
|
|
$rel = $this->expressionEvaluator->evaluate($relation->getName(), $object); |
|
45
|
|
|
$href = $relation->getHref(); |
|
46
|
|
|
|
|
47
|
|
|
if ($href instanceof Route) { |
|
48
|
|
|
if (!$this->urlGeneratorRegistry->hasGenerators()) { |
|
49
|
|
|
throw new \RuntimeException('You cannot use a route without an url generator.'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$name = $this->expressionEvaluator->evaluate($href->getName(), $object); |
|
53
|
|
|
$parameters = is_array($href->getParameters()) |
|
54
|
|
|
? $this->expressionEvaluator->evaluateArray($href->getParameters(), $object) |
|
55
|
|
|
: $this->expressionEvaluator->evaluate($href->getParameters(), $object) |
|
56
|
|
|
; |
|
57
|
|
|
$isAbsolute = $this->expressionEvaluator->evaluate($href->isAbsolute(), $object); |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
if (!is_array($parameters)) { |
|
60
|
|
|
throw new \RuntimeException( |
|
61
|
|
|
sprintf( |
|
62
|
|
|
'The route parameters should be an array, %s given. Maybe you forgot to wrap the expression in expr(...).', |
|
63
|
|
|
gettype($parameters) |
|
64
|
|
|
) |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$href = $this->urlGeneratorRegistry |
|
69
|
|
|
->get($href->getGenerator()) |
|
70
|
|
|
->generate($name, $parameters, $isAbsolute) |
|
|
|
|
|
|
71
|
|
|
; |
|
72
|
|
|
} else { |
|
73
|
|
|
$href = $this->expressionEvaluator->evaluate($href, $object); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$attributes = $this->expressionEvaluator->evaluateArray($relation->getAttributes(), $object); |
|
77
|
|
|
|
|
78
|
|
|
return new Link($rel, $href, $attributes); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: