|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the API Platform project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace ApiPlatform\Core\Api; |
|
13
|
|
|
|
|
14
|
|
|
use ApiPlatform\Core\DataProvider\PaginatorInterface; |
|
15
|
|
|
use ApiPlatform\Core\Exception\InvalidArgumentException; |
|
16
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface; |
|
17
|
|
|
use ApiPlatform\Core\Util\ClassInfoTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* {@inheritdoc} |
|
21
|
|
|
* |
|
22
|
|
|
* @author Kévin Dunglas <[email protected]> |
|
23
|
|
|
* @author Samuel ROZE <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
final class ResourceClassResolver implements ResourceClassResolverInterface |
|
26
|
|
|
{ |
|
27
|
|
|
use ClassInfoTrait; |
|
28
|
|
|
|
|
29
|
|
|
private $resourceNameCollectionFactory; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->resourceNameCollectionFactory = $resourceNameCollectionFactory; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getResourceClass($value, string $resourceClass = null, bool $strict = false): string |
|
40
|
|
|
{ |
|
41
|
|
View Code Duplication |
if (is_object($value) && !$value instanceof PaginatorInterface) { |
|
|
|
|
|
|
42
|
|
|
$typeToFind = $type = $this->getObjectClass($value); |
|
43
|
|
|
if (null === $resourceClass) { |
|
44
|
|
|
$resourceClass = $typeToFind; |
|
45
|
|
|
} |
|
46
|
|
|
} elseif (null === $resourceClass) { |
|
47
|
|
|
throw new InvalidArgumentException(sprintf('No resource class found.')); |
|
48
|
|
|
} else { |
|
49
|
|
|
$typeToFind = $type = $resourceClass; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
View Code Duplication |
if (($strict && isset($type) && $resourceClass !== $type) || !$this->isResourceClass($typeToFind)) { |
|
|
|
|
|
|
53
|
|
|
if (is_subclass_of($type, $resourceClass) && $this->isResourceClass($resourceClass)) { |
|
54
|
|
|
return $type; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
throw new InvalidArgumentException(sprintf('No resource class found for object of type "%s"', $typeToFind)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $resourceClass; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getResourceClassFromContext($value, array $context = null, bool $strict = false): string |
|
67
|
|
|
{ |
|
68
|
|
|
$resourceClass = $context['resource_class'] ?? null; |
|
69
|
|
|
|
|
70
|
|
|
if (isset($context['subcollection_resource_class']) && null !== $context['subcollection_resource_class']) { |
|
71
|
|
|
$resourceClass = $context['subcollection_resource_class']; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
View Code Duplication |
if (is_object($value) && !$value instanceof PaginatorInterface) { |
|
|
|
|
|
|
75
|
|
|
$typeToFind = $type = $this->getObjectClass($value); |
|
76
|
|
|
if (null === $resourceClass) { |
|
77
|
|
|
$resourceClass = $typeToFind; |
|
78
|
|
|
} |
|
79
|
|
|
} elseif (null === $resourceClass) { |
|
80
|
|
|
throw new InvalidArgumentException(sprintf('No resource class found.')); |
|
81
|
|
|
} else { |
|
82
|
|
|
$typeToFind = $type = $resourceClass; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
View Code Duplication |
if (($strict && isset($type) && $resourceClass !== $type) || !$this->isResourceClass($typeToFind)) { |
|
|
|
|
|
|
86
|
|
|
if (is_subclass_of($type, $resourceClass) && $this->isResourceClass($resourceClass)) { |
|
87
|
|
|
return $type; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
throw new InvalidArgumentException(sprintf('No resource class found for object of type "%s"', $typeToFind)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $resourceClass; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* {@inheritdoc} |
|
98
|
|
|
*/ |
|
99
|
|
|
public function isResourceClass(string $type): bool |
|
100
|
|
|
{ |
|
101
|
|
|
foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) { |
|
102
|
|
|
if ($type === $resourceClass) { |
|
103
|
|
|
return true; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return false; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.