1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace hiapi\jsonApi; |
5
|
|
|
|
6
|
|
|
use hiapi\jsonApi\ResourceDocumentFactoryInterface; |
7
|
|
|
use hiqdev\DataMapper\Attribute\DateTimeAttribute; |
8
|
|
|
use hiqdev\DataMapper\Attribution\AttributionInterface; |
9
|
|
|
use WoohooLabs\Yin\JsonApi\Schema\Link\ResourceLinks; |
10
|
|
|
use WoohooLabs\Yin\JsonApi\Schema\Relationship\ToOneRelationship; |
11
|
|
|
use WoohooLabs\Yin\JsonApi\Schema\Resource\AbstractResource; |
12
|
|
|
use WoohooLabs\Yin\JsonApi\Schema\Resource\ResourceInterface; |
13
|
|
|
|
14
|
|
|
abstract class AttributionBasedResource extends AbstractResource |
15
|
|
|
{ |
16
|
|
|
protected string $attributionClass; |
|
|
|
|
17
|
|
|
|
18
|
|
|
protected AttributionInterface $attribution; |
19
|
|
|
|
20
|
|
|
protected ResourceDocumentFactoryInterface $resourceFactory; |
21
|
|
|
|
22
|
|
|
public function __construct(ResourceDocumentFactoryInterface $resourceFactory) |
23
|
|
|
{ |
24
|
|
|
$this->resourceFactory = $resourceFactory; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getType($entity): string |
28
|
|
|
{ |
29
|
|
|
return lcfirst(substr(strrchr(get_class($this), '\\'), 1, -8)); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getId($entity): string |
33
|
|
|
{ |
34
|
|
|
return (string)$entity->getId(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getMeta($entity): array |
38
|
|
|
{ |
39
|
|
|
return []; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getLinks($entity): ?ResourceLinks |
43
|
|
|
{ |
44
|
|
|
return null; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getAttributes($entity): array |
48
|
|
|
{ |
49
|
|
|
$res = []; |
50
|
|
|
foreach ($this->getAttribution()->attributes() as $key => $type) { |
51
|
|
|
if ($key === 'id') { |
52
|
|
|
continue; |
53
|
|
|
} |
54
|
|
|
foreach ([$key, 'get' . ucfirst($key)] as $method) { |
55
|
|
|
if (method_exists($entity, $method)) { |
56
|
|
|
$res[$key] = $this->buildAttributeMethod($method, $type); |
57
|
|
|
break; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $res; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function buildAttributeMethod(string $method, $type): callable |
66
|
|
|
{ |
67
|
|
|
if ($type === DateTimeAttribute::class) { |
68
|
|
|
return static function ($po) use ($method): ?string { |
69
|
|
|
$value = $po->{$method}(); |
70
|
|
|
if ($value === null) { |
71
|
|
|
return null; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $value->format(DATE_ATOM); |
75
|
|
|
}; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return fn($po): ?string => $po->{$method}(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getDefaultIncludedRelationships($entity): array |
82
|
|
|
{ |
83
|
|
|
return array_keys($this->getAttribution()->relations()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getRelationships($entity): array |
87
|
|
|
{ |
88
|
|
|
$res = []; |
89
|
|
|
foreach ($this->getAttribution()->relations() as $key => $type) { |
90
|
|
|
foreach ([$key, 'get' . ucfirst($key)] as $method) { |
91
|
|
|
if (method_exists($entity, $method)) { |
92
|
|
|
$res[$key] = fn($po) => ToOneRelationship::create() |
93
|
|
|
->setData($po->{$method}(), $this->getResourceFor($type)) |
94
|
|
|
->omitDataWhenNotIncluded(); |
95
|
|
|
break; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $res; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function getAttribution(): AttributionInterface |
104
|
|
|
{ |
105
|
|
|
if (! isset($this->attribution)) { |
106
|
|
|
$class = $this->getAttributionClass(); |
107
|
|
|
$this->attribution = new $class(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this->attribution; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function getAttributionClass(): string |
114
|
|
|
{ |
115
|
|
|
if (empty($this->attributionClass)) { |
116
|
|
|
$this->attributionClass = $this->findAttributionClass(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this->attributionClass; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
private function findAttributionClass(): string |
123
|
|
|
{ |
124
|
|
|
return substr(get_class($this), 0, -8) . 'Attribution'; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function getResourceFor($className): ResourceInterface |
128
|
|
|
{ |
129
|
|
|
return $this->resourceFactory->getResourceByClassName($className); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|