|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Mikemirten\Component\JsonApi\Mapper\Handler; |
|
5
|
|
|
|
|
6
|
|
|
use Mikemirten\Component\JsonApi\Document\AbstractRelationship; |
|
7
|
|
|
use Mikemirten\Component\JsonApi\Document\IdentifierCollectionRelationship; |
|
8
|
|
|
use Mikemirten\Component\JsonApi\Document\NoDataRelationship; |
|
9
|
|
|
use Mikemirten\Component\JsonApi\Document\ResourceIdentifierObject; |
|
10
|
|
|
use Mikemirten\Component\JsonApi\Document\ResourceObject; |
|
11
|
|
|
use Mikemirten\Component\JsonApi\Document\SingleIdentifierRelationship; |
|
12
|
|
|
use Mikemirten\Component\JsonApi\Mapper\Definition\Relationship as RelationshipDefinition; |
|
13
|
|
|
use Mikemirten\Component\JsonApi\Mapper\Handler\LinkHandler\LinkHandlerInterface; |
|
14
|
|
|
use Mikemirten\Component\JsonApi\Mapper\MappingContext; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Handler of relationships |
|
18
|
|
|
* |
|
19
|
|
|
* @package Mikemirten\Component\JsonApi\Mapper\Handler |
|
20
|
|
|
*/ |
|
21
|
|
|
class RelationshipHandler implements HandlerInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Links' handler |
|
25
|
|
|
* |
|
26
|
|
|
* @var LinkHandlerInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $linkHandler; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* RelationshipHandler constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param LinkHandlerInterface $linkHandler |
|
34
|
|
|
*/ |
|
35
|
5 |
|
public function __construct(LinkHandlerInterface $linkHandler) |
|
36
|
|
|
{ |
|
37
|
5 |
|
$this->linkHandler = $linkHandler; |
|
38
|
5 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
5 |
|
public function toResource($object, ResourceObject $resource, MappingContext $context) |
|
44
|
|
|
{ |
|
45
|
5 |
|
$definitions = $context->getDefinition()->getRelationships(); |
|
46
|
|
|
|
|
47
|
5 |
|
foreach ($definitions as $definition) |
|
48
|
|
|
{ |
|
49
|
5 |
|
$relationship = $this->createRelationship($object, $definition, $context); |
|
50
|
5 |
|
$this->linkHandler->handleLinks($object, $definition, $relationship); |
|
51
|
|
|
|
|
52
|
5 |
|
$resource->setRelationship($definition->getName(), $relationship); |
|
53
|
|
|
} |
|
54
|
5 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
public function fromResource($object, ResourceObject $resource, MappingContext $context) |
|
60
|
|
|
{ |
|
61
|
|
|
// Do nothing |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Create relationship contains a single resource-identifier |
|
66
|
|
|
* |
|
67
|
|
|
* @param mixed $object |
|
68
|
|
|
* @param RelationshipDefinition $definition |
|
69
|
|
|
* @param MappingContext $context |
|
70
|
|
|
* @return AbstractRelationship |
|
71
|
|
|
*/ |
|
72
|
5 |
|
protected function createRelationship($object, RelationshipDefinition $definition, MappingContext $context): AbstractRelationship |
|
73
|
|
|
{ |
|
74
|
5 |
|
if (! $definition->isDataIncluded()) { |
|
75
|
2 |
|
return new NoDataRelationship(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
3 |
|
return $definition->isCollection() |
|
79
|
2 |
|
? $this->createIdentifierCollectionRelationship($object, $definition, $context) |
|
80
|
3 |
|
: $this->createSingleIdentifierRelationship($object, $definition, $context); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Create relationship contains a single resource-identifier |
|
85
|
|
|
* |
|
86
|
|
|
* @param mixed $object |
|
87
|
|
|
* @param RelationshipDefinition $definition |
|
88
|
|
|
* @param MappingContext $context |
|
89
|
|
|
* @return AbstractRelationship ( NoDataRelationship | NoDataRelationship ) |
|
90
|
|
|
*/ |
|
91
|
1 |
|
protected function createSingleIdentifierRelationship($object, RelationshipDefinition $definition, MappingContext $context): AbstractRelationship |
|
92
|
|
|
{ |
|
93
|
1 |
|
$relatedObject = $object->{$definition->getGetter()}(); |
|
94
|
|
|
|
|
95
|
1 |
|
if ($relatedObject === null) { |
|
96
|
|
|
return new NoDataRelationship(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
$resource = $context->getMapper()->toResourceIdentifier($relatedObject); |
|
100
|
|
|
|
|
101
|
1 |
|
return new SingleIdentifierRelationship($resource); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Create relationship contains a collection of resource-identifiers |
|
106
|
|
|
* |
|
107
|
|
|
* @param mixed $object |
|
108
|
|
|
* @param RelationshipDefinition $definition |
|
109
|
|
|
* @param MappingContext $context |
|
110
|
|
|
* @return IdentifierCollectionRelationship |
|
111
|
|
|
*/ |
|
112
|
2 |
|
protected function createIdentifierCollectionRelationship($object, RelationshipDefinition $definition, MappingContext $context): IdentifierCollectionRelationship |
|
113
|
|
|
{ |
|
114
|
2 |
|
$relationship = new IdentifierCollectionRelationship(); |
|
115
|
2 |
|
$collection = $object->{$definition->getGetter()}(); |
|
116
|
2 |
|
$dataLimit = $definition->getDataLimit(); |
|
117
|
|
|
|
|
118
|
2 |
|
if ($dataLimit > 0) { |
|
119
|
1 |
|
$collection = new \LimitIterator($collection, 0, $dataLimit); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
2 |
|
$mapper = $context->getMapper(); |
|
123
|
|
|
|
|
124
|
2 |
|
foreach ($collection as $relatedObject) |
|
125
|
|
|
{ |
|
126
|
2 |
|
$resource = $mapper->toResourceIdentifier($relatedObject); |
|
127
|
|
|
|
|
128
|
2 |
|
$relationship->addIdentifier($resource); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
2 |
|
return $relationship; |
|
132
|
|
|
} |
|
133
|
|
|
} |