|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace W2w\Lib\Apie\Plugins\PrimaryKey; |
|
5
|
|
|
|
|
6
|
|
|
use erasys\OpenApi\Spec\v3\Schema; |
|
7
|
|
|
use W2w\Lib\Apie\Core\Resources\ApiResources; |
|
8
|
|
|
use W2w\Lib\Apie\Exceptions\BadConfigurationException; |
|
9
|
|
|
use W2w\Lib\Apie\PluginInterfaces\ApieAwareInterface; |
|
10
|
|
|
use W2w\Lib\Apie\PluginInterfaces\ApieAwareTrait; |
|
11
|
|
|
use W2w\Lib\Apie\PluginInterfaces\NormalizerProviderInterface; |
|
12
|
|
|
use W2w\Lib\Apie\PluginInterfaces\SchemaProviderInterface; |
|
13
|
|
|
use W2w\Lib\Apie\Plugins\PrimaryKey\Normalizers\ApiePrimaryKeyNormalizer; |
|
14
|
|
|
use W2w\Lib\Apie\Plugins\PrimaryKey\Normalizers\PrimaryKeyReferenceNormalizer; |
|
15
|
|
|
use W2w\Lib\Apie\Plugins\PrimaryKey\Schema\ApiResourceLinkSchemaBuilder; |
|
16
|
|
|
use W2w\Lib\Apie\Plugins\PrimaryKey\ValueObjects\PrimaryKeyReference; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Core Apie plugin to map api resources to string urls for child objects. |
|
20
|
|
|
*/ |
|
21
|
|
|
class PrimaryKeyPlugin implements NormalizerProviderInterface, ApieAwareInterface, SchemaProviderInterface |
|
22
|
|
|
{ |
|
23
|
|
|
use ApieAwareTrait; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritDoc} |
|
27
|
|
|
*/ |
|
28
|
|
|
public function getNormalizers(): array |
|
29
|
|
|
{ |
|
30
|
|
|
$primaryKeyNormalizer = new ApiePrimaryKeyNormalizer( |
|
31
|
|
|
new ApiResources($this->getApie()->getResources()), |
|
32
|
|
|
$this->getApie()->getIdentifierExtractor(), |
|
33
|
|
|
$this->getApie()->getApiResourceMetadataFactory(), |
|
34
|
|
|
$this->getApie()->getClassResourceConverter(), |
|
35
|
|
|
$this->getBaseUrl() |
|
36
|
|
|
); |
|
37
|
|
|
return [ |
|
|
|
|
|
|
38
|
|
|
new PrimaryKeyReferenceNormalizer(), |
|
39
|
|
|
$primaryKeyNormalizer, |
|
40
|
|
|
]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Returns base url if one is set up. |
|
45
|
|
|
* |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
|
|
private function getBaseUrl(): string |
|
49
|
|
|
{ |
|
50
|
|
|
try { |
|
51
|
|
|
return $this->getApie()->getBaseUrl(); |
|
52
|
|
|
} catch (BadConfigurationException $exception) { |
|
53
|
|
|
return ''; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritDoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getDefinedStaticData(): array |
|
61
|
|
|
{ |
|
62
|
|
|
return [ |
|
63
|
|
|
PrimaryKeyReference::class => new Schema([ |
|
64
|
|
|
'type' => 'string', |
|
65
|
|
|
'format' => 'path', |
|
66
|
|
|
]), |
|
67
|
|
|
]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritDoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getDynamicSchemaLogic(): array |
|
74
|
|
|
{ |
|
75
|
|
|
$res = []; |
|
76
|
|
|
$identifierExtractor = $this->getApie()->getIdentifierExtractor(); |
|
77
|
|
|
$builder = new ApiResourceLinkSchemaBuilder($this->getApie()->getClassResourceConverter()); |
|
78
|
|
|
foreach ($this->getApie()->getResources() as $resource) { |
|
79
|
|
|
if (null !== $identifierExtractor->getIdentifierKeyOfClass($resource)) { |
|
80
|
|
|
$res[$resource] = $builder; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
return $res; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|