|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace hiapi\jsonApi; |
|
5
|
|
|
|
|
6
|
|
|
use Psr\Container\ContainerInterface; |
|
7
|
|
|
use RuntimeException; |
|
8
|
|
|
use WoohooLabs\Yin\JsonApi\Schema\Resource\ResourceInterface; |
|
9
|
|
|
use WoohooLabs\Yin\JsonApi\Schema\Document\ResourceDocumentInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Creates JsonApi resource document for given content. |
|
13
|
|
|
* |
|
14
|
|
|
* @author Andrii Vasyliev <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class ResourceDocumentFactory implements ResourceDocumentFactoryInterface |
|
17
|
|
|
{ |
|
18
|
|
|
private ContainerInterface $container; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
private array $resourceMap; |
|
21
|
|
|
|
|
22
|
|
|
private array $collections = []; |
|
23
|
|
|
|
|
24
|
|
|
private array $resources = []; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(array $resourceMap, ContainerInterface $container) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->resourceMap = $resourceMap; |
|
29
|
|
|
$this->container = $container; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** {@inheritDoc} */ |
|
33
|
|
|
public function getResourceDocumentFor($content): ResourceDocumentInterface |
|
34
|
|
|
{ |
|
35
|
|
|
if (is_array($content)) { |
|
36
|
|
|
return $this->getCollectionDocument($content); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return $this->getSingleDocument($content); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** {@inheritDoc} */ |
|
43
|
|
|
public function getResourceByClassName(string $entityClassName): ResourceInterface |
|
44
|
|
|
{ |
|
45
|
|
|
if (empty($this->resources[$entityClassName])) { |
|
46
|
|
|
$this->resources[$entityClassName] = $this->container->get( |
|
47
|
|
|
$this->findResourceClass($entityClassName) |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $this->resources[$entityClassName]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** {@inheritDoc} */ |
|
55
|
|
|
public function getResourceFor(object $entity): ResourceInterface |
|
56
|
|
|
{ |
|
57
|
|
|
$class = get_class($entity); |
|
58
|
|
|
|
|
59
|
|
|
return $this->getResourceByClassName($class); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
private function findResourceClass(string $class): string |
|
63
|
|
|
{ |
|
64
|
|
|
if (! empty($this->resourceMap[$class])) { |
|
65
|
|
|
return $this->resourceMap[$class]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$res = $this->findResourceClassByAttribution($class); |
|
69
|
|
|
|
|
70
|
|
|
return $res ?? $this->findResourceClassByAncestor($class); |
|
71
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
private function findResourceClassByAttribution($class): ?string |
|
75
|
|
|
{ |
|
76
|
|
|
$result = substr($class, 0, -11) . 'Resource'; |
|
77
|
|
|
|
|
78
|
|
|
return class_exists($result) ? $result : null; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function findResourceClassByAncestor($class): string |
|
82
|
|
|
{ |
|
83
|
|
|
foreach ($this->resourceMap as $ancestor => $result) { |
|
84
|
|
|
if (is_subclass_of($class, $ancestor)) { |
|
85
|
|
|
return $result; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
throw new RuntimeException("JsonApi resource not defined for '$class'"); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private function getCollectionDocument(array $rows): ResourceDocumentInterface |
|
93
|
|
|
{ |
|
94
|
|
|
if (empty($rows)) { |
|
95
|
|
|
return new EmptyCollectionDocument(); |
|
96
|
|
|
} |
|
97
|
|
|
$class = get_class(reset($rows)); |
|
98
|
|
|
if (empty($this->collections[$class])) { |
|
99
|
|
|
$this->collections[$class] = $this->findCollection($class); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $this->collections[$class]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
private function getSingleDocument(object $entity): ResourceDocumentInterface |
|
106
|
|
|
{ |
|
107
|
|
|
$singleDocumentClass = $this->resource2singleDocument( |
|
108
|
|
|
$this->findResourceClass(get_class($entity)) |
|
109
|
|
|
); |
|
110
|
|
|
|
|
111
|
|
|
return $this->container->get($singleDocumentClass); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
private function findCollection(string $class): ResourceDocumentInterface |
|
115
|
|
|
{ |
|
116
|
|
|
$class = $this->resource2collection($this->findResourceClass($class)); |
|
117
|
|
|
|
|
118
|
|
|
return $this->container->get($class); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
private function resource2collection(string $class): string |
|
122
|
|
|
{ |
|
123
|
|
|
// XXX TMP quick and dirty TODO improve later |
|
124
|
|
|
return substr($class, 0, -8) . 'sCollectionDocument'; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
private function resource2singleDocument(string $class): string |
|
128
|
|
|
{ |
|
129
|
|
|
// XXX TMP quick and dirty TODO improve later |
|
130
|
|
|
return substr($class, 0, -8) . 'Document'; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|