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