|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hateoas\Serializer; |
|
4
|
|
|
|
|
5
|
|
|
use Hateoas\Configuration\Exclusion; |
|
6
|
|
|
use Hateoas\Configuration\Relation; |
|
7
|
|
|
use Hateoas\Expression\ExpressionEvaluator; |
|
8
|
|
|
use Hateoas\Serializer\Metadata\RelationPropertyMetadata; |
|
9
|
|
|
use JMS\Serializer\SerializationContext; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @author Adrien Brault <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class ExclusionManager |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var ExpressionEvaluator |
|
18
|
|
|
*/ |
|
19
|
|
|
private $expressionEvaluator; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(ExpressionEvaluator $expressionEvaluator) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->expressionEvaluator = $expressionEvaluator; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function shouldSkipLink($object, Relation $relation, SerializationContext $context) |
|
27
|
|
|
{ |
|
28
|
|
|
if ($this->shouldSkipRelation($object, $relation, $context)) { |
|
29
|
|
|
return true; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if (null === $relation->getHref()) { |
|
33
|
|
|
return true; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return false; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function shouldSkipEmbedded($object, Relation $relation, SerializationContext $context) |
|
40
|
|
|
{ |
|
41
|
|
|
if (null === $relation->getEmbedded()) { |
|
42
|
|
|
return true; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if (null === $relation->getEmbedded()->getExclusion()) { |
|
46
|
|
|
return $this->shouldSkipRelation($object, $relation, $context); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $this->shouldSkip($object, $relation, $relation->getEmbedded()->getExclusion(), $context); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function shouldSkipRelation($object, Relation $relation, SerializationContext $context) |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->shouldSkip($object, $relation, $relation->getExclusion(), $context); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
private function shouldSkip($object, Relation $relation, Exclusion $exclusion = null, SerializationContext $context) |
|
58
|
|
|
{ |
|
59
|
|
|
if ($context->getExclusionStrategy()) { |
|
60
|
|
|
$propertyMetadata = new RelationPropertyMetadata($exclusion, $relation); |
|
61
|
|
|
|
|
62
|
|
|
if ($context->getExclusionStrategy()->shouldSkipProperty($propertyMetadata, $context)) { |
|
63
|
|
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if (null !== $exclusion |
|
68
|
|
|
&& null !== $exclusion->getExcludeIf() |
|
69
|
|
|
&& $this->expressionEvaluator->evaluate($exclusion->getExcludeIf(), $object) |
|
70
|
|
|
) { |
|
71
|
|
|
return true; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|