-
-
Notifications
You must be signed in to change notification settings - Fork 966
Expand file tree
/
Copy pathResourceMetadataTrait.php
More file actions
109 lines (88 loc) · 4.16 KB
/
Copy pathResourceMetadataTrait.php
File metadata and controls
109 lines (88 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\JsonSchema;
use ApiPlatform\Metadata\CollectionOperationInterface;
use ApiPlatform\Metadata\Exception\OperationNotFoundException;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
use ApiPlatform\Metadata\Util\ResourceClassInfoTrait;
/**
* @internal
*/
trait ResourceMetadataTrait
{
use ResourceClassInfoTrait;
private function findOutputClass(string $className, string $type, Operation $operation, ?array $serializerContext): ?string
{
$resourceClass = (Schema::TYPE_OUTPUT === $type ? $operation->getOutputClass() : $operation->getInputClass()) ?? $className;
$forceSubschema = $serializerContext[SchemaFactory::FORCE_SUBSCHEMA] ?? false;
return $forceSubschema ? ($resourceClass ?? $operation->getClass()) : $resourceClass;
}
private function findOperation(string $className, string $type, ?Operation $operation, ?array $serializerContext, ?string $format = null): Operation
{
if (null === $operation) {
if (null === $this->resourceMetadataFactory) {
return new HttpOperation();
}
$resourceMetadataCollection = $this->resourceMetadataFactory->create($className);
try {
$operation = $resourceMetadataCollection->getOperation();
} catch (OperationNotFoundException $e) {
$operation = new HttpOperation();
}
$forceSubschema = $serializerContext[SchemaFactory::FORCE_SUBSCHEMA] ?? false;
if ($operation->getShortName() === $this->getShortClassName($className) && $forceSubschema) {
$operation = new HttpOperation();
}
return $this->findOperationForType($resourceMetadataCollection, $type, $operation, $forceSubschema ? null : $format);
}
// The best here is to use an Operation when calling `buildSchema`, we try to do a smart guess otherwise
if ($this->resourceMetadataFactory && !$operation->getClass()) {
$resourceMetadataCollection = $this->resourceMetadataFactory->create($className);
if ($operation->getName()) {
return $resourceMetadataCollection->getOperation($operation->getName());
}
return $this->findOperationForType($resourceMetadataCollection, $type, $operation, $format);
}
return $operation;
}
private function findOperationForType(ResourceMetadataCollection $resourceMetadataCollection, string $type, Operation $operation, ?string $format = null): Operation
{
$lookForCollection = $operation instanceof CollectionOperationInterface;
// Find the operation and use the first one that matches criterias
foreach ($resourceMetadataCollection as $resourceMetadata) {
foreach ($resourceMetadata->getOperations() ?? [] as $op) {
if (!$lookForCollection && $op instanceof CollectionOperationInterface) {
continue;
}
if (Schema::TYPE_INPUT === $type && \in_array($op->getMethod(), ['POST', 'PATCH', 'PUT'], true)) {
$operation = $op;
break 2;
}
if ($format && Schema::TYPE_OUTPUT === $type && \array_key_exists($format, $op->getOutputFormats() ?? [])) {
$operation = $op;
break 2;
}
}
}
return $operation;
}
private function getSerializerContext(Operation $operation, string $type = Schema::TYPE_OUTPUT): array
{
return Schema::TYPE_OUTPUT === $type ? ($operation->getNormalizationContext() ?? []) : ($operation->getDenormalizationContext() ?? []);
}
private function getShortClassName(string $fullyQualifiedName): string
{
$parts = explode('\\', $fullyQualifiedName);
return end($parts);
}
}