|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hateoas\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use Hateoas\Configuration\RelationsRepository; |
|
6
|
|
|
use Hateoas\Expression\ExpressionEvaluator; |
|
7
|
|
|
use Hateoas\Model\Embedded; |
|
8
|
|
|
use Hateoas\Serializer\ExclusionManager; |
|
9
|
|
|
use Hateoas\Serializer\Metadata\RelationPropertyMetadata; |
|
10
|
|
|
use JMS\Serializer\SerializationContext; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @author Adrien Brault <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class EmbeddedsFactory |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var RelationsRepository |
|
19
|
|
|
*/ |
|
20
|
|
|
private $relationsRepository; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var ExpressionEvaluator |
|
24
|
|
|
*/ |
|
25
|
|
|
private $expressionEvaluator; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var ExclusionManager |
|
29
|
|
|
*/ |
|
30
|
|
|
private $exclusionManager; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param RelationsRepository $relationsRepository |
|
34
|
|
|
* @param ExpressionEvaluator $expressionEvaluator |
|
35
|
|
|
* @param ExclusionManager $exclusionManager |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct( |
|
38
|
|
|
RelationsRepository $relationsRepository, |
|
39
|
|
|
ExpressionEvaluator $expressionEvaluator, |
|
40
|
|
|
ExclusionManager $exclusionManager |
|
41
|
|
|
) { |
|
42
|
|
|
$this->relationsRepository = $relationsRepository; |
|
43
|
|
|
$this->expressionEvaluator = $expressionEvaluator; |
|
44
|
|
|
$this->exclusionManager = $exclusionManager; |
|
45
|
|
|
} |
|
46
|
|
|
/** |
|
47
|
|
|
* @param object $object |
|
48
|
|
|
* @param SerializationContext $context |
|
49
|
|
|
* @return Embedded[] |
|
50
|
|
|
*/ |
|
51
|
|
|
public function create($object, SerializationContext $context) |
|
52
|
|
|
{ |
|
53
|
|
|
$embeddeds = array(); |
|
54
|
|
|
foreach ($this->relationsRepository->getRelations($object) as $relation) { |
|
55
|
|
|
if ($this->exclusionManager->shouldSkipEmbedded($object, $relation, $context)) { |
|
56
|
|
|
continue; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$rel = $this->expressionEvaluator->evaluate($relation->getName(), $object); |
|
60
|
|
|
$data = $this->expressionEvaluator->evaluate($relation->getEmbedded()->getContent(), $object); |
|
61
|
|
|
$xmlElementName = $this->expressionEvaluator->evaluate($relation->getEmbedded()->getXmlElementName(), $object); |
|
62
|
|
|
|
|
63
|
|
|
$propertyMetadata = new RelationPropertyMetadata($relation->getEmbedded()->getExclusion(), $relation); |
|
64
|
|
|
|
|
65
|
|
|
$embeddeds[] = new Embedded($rel, $data, $propertyMetadata, $xmlElementName); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $embeddeds; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.