1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Mikemirten\Component\JsonApi\Mapper\Handler\LinkHandler; |
5
|
|
|
|
6
|
|
|
use Mikemirten\Component\JsonApi\Document\LinkObject; |
7
|
|
|
use Mikemirten\Component\JsonApi\Document\ResourceObject; |
8
|
|
|
use Mikemirten\Component\JsonApi\Mapper\Definition\Link as LinkDefinition; |
9
|
|
|
use Mikemirten\Component\JsonApi\Mapper\Handler\HandlerInterface; |
10
|
|
|
use Mikemirten\Component\JsonApi\Mapper\Handler\LinkRepository\Link as LinkData; |
11
|
|
|
use Mikemirten\Component\JsonApi\Mapper\Handler\LinkRepository\RepositoryProvider as LinkRepositoryProvider; |
12
|
|
|
use Mikemirten\Component\JsonApi\Mapper\Handler\PropertyAccessor\PropertyAccessorInterface; |
13
|
|
|
use Mikemirten\Component\JsonApi\Mapper\MappingContext; |
14
|
|
|
use Mikemirten\Component\JsonApi\Mapper\Definition\Behaviour\LinksAwareInterface as LinksAwareDefinitionInterface; |
15
|
|
|
use Mikemirten\Component\JsonApi\Document\Behaviour\LinksAwareInterface as LinksAwareDocumentInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* An implementation of links handler based on repositories of links. |
19
|
|
|
* |
20
|
|
|
* @package Mikemirten\Component\JsonApi\Mapper\Handler\LinkHandler |
21
|
|
|
*/ |
22
|
|
|
class LinkHandler implements HandlerInterface, LinkHandlerInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Provider of links' repositories |
26
|
|
|
* |
27
|
|
|
* @var LinkRepositoryProvider |
28
|
|
|
*/ |
29
|
|
|
protected $provider; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Property accessor |
33
|
|
|
* |
34
|
|
|
* @var PropertyAccessorInterface |
35
|
|
|
*/ |
36
|
|
|
protected $accessor; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* LinkHandler constructor. |
40
|
|
|
* |
41
|
|
|
* @param LinkRepositoryProvider $provider |
42
|
|
|
* @param PropertyAccessorInterface $accessor |
43
|
|
|
*/ |
44
|
1 |
|
public function __construct(LinkRepositoryProvider $provider, PropertyAccessorInterface $accessor) |
45
|
|
|
{ |
46
|
1 |
|
$this->provider = $provider; |
47
|
1 |
|
$this->accessor = $accessor; |
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
1 |
|
public function toResource($object, ResourceObject $resource, MappingContext $context) |
54
|
|
|
{ |
55
|
1 |
|
$this->handleLinks($object, $context->getDefinition(), $resource); |
56
|
1 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
1 |
|
public function handleLinks( |
62
|
|
|
$object, |
63
|
|
|
LinksAwareDefinitionInterface $definition, |
64
|
|
|
LinksAwareDocumentInterface $document |
65
|
|
|
) { |
66
|
1 |
|
foreach ($definition->getLinks() as $linkDefinition) |
67
|
|
|
{ |
68
|
1 |
|
$repoName = $linkDefinition->getRepositoryName(); |
69
|
1 |
|
$linkName = $linkDefinition->getLinkName(); |
70
|
|
|
|
71
|
1 |
|
$parameters = $this->resolveParameters($object, $linkDefinition); |
72
|
|
|
|
73
|
1 |
|
$linkData = $this->provider |
74
|
1 |
|
->getRepository($repoName) |
75
|
1 |
|
->getLink($linkName, $parameters); |
76
|
|
|
|
77
|
1 |
|
$link = $this->createLink($linkDefinition, $linkData); |
78
|
|
|
|
79
|
1 |
|
$document->setLink($linkDefinition->getName(), $link); |
80
|
|
|
} |
81
|
1 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Create link-object |
85
|
|
|
* |
86
|
|
|
* @param LinkDefinition $definition |
87
|
|
|
* @param LinkData $data |
88
|
|
|
* @return LinkObject |
89
|
|
|
*/ |
90
|
1 |
|
protected function createLink(LinkDefinition $definition, LinkData $data): LinkObject |
91
|
|
|
{ |
92
|
1 |
|
$reference = $data->getReference(); |
93
|
|
|
|
94
|
1 |
|
$metadata = array_replace( |
95
|
1 |
|
$data->getMetadata(), |
96
|
1 |
|
$definition->getMetadata() |
97
|
|
|
); |
98
|
|
|
|
99
|
1 |
|
return new LinkObject($reference, $metadata); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Resolve parameters |
104
|
|
|
* |
105
|
|
|
* @param mixed $object |
106
|
|
|
* @param LinkDefinition $definition |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
1 |
|
protected function resolveParameters($object, LinkDefinition $definition): array |
110
|
|
|
{ |
111
|
1 |
|
$resolved = []; |
112
|
|
|
|
113
|
1 |
|
foreach ($definition->getParameters() as $name => $value) |
114
|
|
|
{ |
115
|
1 |
|
if (strpos($value, '@') === 0) { |
116
|
1 |
|
$resolved[$name] = $this->accessor->getValue($object, substr($value, 1)); |
117
|
1 |
|
continue; |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
$resolved[$name] = $value; |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
return $resolved; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
public function fromResource($object, ResourceObject $resource, MappingContext $context) |
130
|
|
|
{ |
131
|
|
|
// Do nothing |
132
|
|
|
} |
133
|
|
|
} |