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
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface as SymfonyUrlGeneratorInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Adrien Brault <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class LinkFactory |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var ExpressionEvaluator |
19
|
|
|
*/ |
20
|
|
|
private $expressionEvaluator; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var UrlGeneratorRegistry |
24
|
|
|
*/ |
25
|
|
|
private $urlGeneratorRegistry; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ExpressionEvaluator $expressionEvaluator |
29
|
|
|
* @param UrlGeneratorRegistry $urlGeneratorRegistry |
30
|
|
|
*/ |
31
|
|
|
public function __construct(ExpressionEvaluator $expressionEvaluator, UrlGeneratorRegistry $urlGeneratorRegistry) |
32
|
|
|
{ |
33
|
|
|
$this->expressionEvaluator = $expressionEvaluator; |
34
|
|
|
$this->urlGeneratorRegistry = $urlGeneratorRegistry; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param object $object |
39
|
|
|
* @param Relation $relation |
40
|
|
|
* |
41
|
|
|
* @return Link |
42
|
|
|
*/ |
43
|
|
|
public function createLink($object, Relation $relation) |
44
|
|
|
{ |
45
|
|
|
$rel = $this->expressionEvaluator->evaluate($relation->getName(), $object); |
46
|
|
|
$href = $relation->getHref(); |
47
|
|
|
|
48
|
|
|
if ($href instanceof Route) { |
49
|
|
|
if (!$this->urlGeneratorRegistry->hasGenerators()) { |
50
|
|
|
throw new \RuntimeException('You cannot use a route without an url generator.'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$name = $this->expressionEvaluator->evaluate($href->getName(), $object); |
54
|
|
|
$parameters = is_array($href->getParameters()) |
55
|
|
|
? $this->expressionEvaluator->evaluateArray($href->getParameters(), $object) |
56
|
|
|
: $this->expressionEvaluator->evaluate($href->getParameters(), $object) |
57
|
|
|
; |
58
|
|
|
$isAbsolute = $this->expressionEvaluator->evaluate($href->isAbsolute(), $object) |
|
|
|
|
59
|
|
|
? SymfonyUrlGeneratorInterface::ABSOLUTE_PATH |
60
|
|
|
: SymfonyUrlGeneratorInterface::ABSOLUTE_URL |
61
|
|
|
; |
62
|
|
|
|
63
|
|
|
if (!is_array($parameters)) { |
64
|
|
|
throw new \RuntimeException( |
65
|
|
|
sprintf( |
66
|
|
|
'The route parameters should be an array, %s given. Maybe you forgot to wrap the expression in expr(...).', |
67
|
|
|
gettype($parameters) |
68
|
|
|
) |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$href = $this->urlGeneratorRegistry |
73
|
|
|
->get($href->getGenerator()) |
74
|
|
|
->generate($name, $parameters, $isAbsolute) |
|
|
|
|
75
|
|
|
; |
76
|
|
|
} else { |
77
|
|
|
$href = $this->expressionEvaluator->evaluate($href, $object); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$attributes = $this->expressionEvaluator->evaluateArray($relation->getAttributes(), $object); |
81
|
|
|
|
82
|
|
|
return new Link($rel, $href, $attributes); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
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: