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) |
31
|
2 |
|
: $this->createSingleIdentifierRelationship($object, $definition); |
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 ResourceIdentifierObject |
51
|
|
|
*/ |
52
|
1 |
|
protected function createSingleIdentifierRelationship($object, RelationshipDefinition $definition): SingleIdentifierRelationship |
53
|
|
|
{ |
54
|
1 |
|
$relatedObject = $object->{$definition->getGetter()}(); |
55
|
|
|
|
56
|
1 |
|
$idGetter = $definition->getIdentifierGetter(); |
57
|
1 |
|
$identifier = (string) $relatedObject->$idGetter(); |
58
|
1 |
|
$resourceType = $definition->getResourceType(); |
59
|
|
|
|
60
|
1 |
|
$resource = new ResourceIdentifierObject($identifier, $resourceType); |
61
|
|
|
|
62
|
1 |
|
return new SingleIdentifierRelationship($resource); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Create relationship contains a collection of resource-identifiers |
67
|
|
|
* |
68
|
|
|
* @param mixed $object |
69
|
|
|
* @param RelationshipDefinition $definition |
70
|
|
|
* @return IdentifierCollectionRelationship |
71
|
|
|
*/ |
72
|
1 |
|
protected function createIdentifierCollectionRelationship($object, RelationshipDefinition $definition): IdentifierCollectionRelationship |
73
|
|
|
{ |
74
|
1 |
|
$idGetter = $definition->getIdentifierGetter(); |
75
|
1 |
|
$resourceType = $definition->getResourceType(); |
76
|
|
|
|
77
|
1 |
|
$relationship = new IdentifierCollectionRelationship(); |
78
|
1 |
|
$collection = $object->{$definition->getGetter()}(); |
79
|
|
|
|
80
|
1 |
|
foreach ($collection as $relatedObject) |
81
|
|
|
{ |
82
|
1 |
|
$identifier = (string) $relatedObject->$idGetter(); |
83
|
1 |
|
$resource = new ResourceIdentifierObject($identifier, $resourceType); |
84
|
|
|
|
85
|
1 |
|
$relationship->addIdentifier($resource); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
return $relationship; |
89
|
|
|
} |
90
|
|
|
} |