-
-
Notifications
You must be signed in to change notification settings - Fork 900
Expand file tree
/
Copy pathPropertyDescriber.php
More file actions
94 lines (78 loc) · 2.83 KB
/
PropertyDescriber.php
File metadata and controls
94 lines (78 loc) · 2.83 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
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\PropertyDescriber;
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareInterface;
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareTrait;
use OpenApi\Annotations as OA;
use Symfony\Component\PropertyInfo\Type;
final class PropertyDescriber implements PropertyDescriberInterface, ModelRegistryAwareInterface
{
use ModelRegistryAwareTrait;
/** @var array<string, PropertyDescriberInterface[]> Recursion helper */
private array $called = [];
/** @var iterable<PropertyDescriberInterface> */
private iterable $propertyDescribers;
/**
* @param iterable<PropertyDescriberInterface> $propertyDescribers
*/
public function __construct(
iterable $propertyDescribers,
) {
$this->propertyDescribers = $propertyDescribers;
}
/**
* @param array<string, mixed> $context Context options for describing the property
*/
public function describe(array $types, OA\Schema $property, array $context = []): void
{
if (null === $propertyDescriber = $this->getPropertyDescriber($types, $context)) {
return;
}
$this->called[$this->getHash($types)][] = $propertyDescriber;
$propertyDescriber->describe($types, $property, $context);
$this->called = []; // Reset recursion helper
}
public function supports(array $types, array $context = []): bool
{
return null !== $this->getPropertyDescriber($types, $context);
}
/**
* @param Type[] $types
*/
private function getHash(array $types): string
{
return md5(serialize($types));
}
/**
* @param Type[] $types
* @param array<string, mixed> $context
*/
private function getPropertyDescriber(array $types, array $context): ?PropertyDescriberInterface
{
foreach ($this->propertyDescribers as $propertyDescriber) {
// Prevent infinite recursion
if (\array_key_exists($this->getHash($types), $this->called)) {
if (\in_array($propertyDescriber, $this->called[$this->getHash($types)], true)) {
continue;
}
}
if ($propertyDescriber instanceof ModelRegistryAwareInterface) {
$propertyDescriber->setModelRegistry($this->modelRegistry);
}
if ($propertyDescriber instanceof PropertyDescriberAwareInterface) {
$propertyDescriber->setPropertyDescriber($this);
}
if ($propertyDescriber->supports($types, $context)) {
return $propertyDescriber;
}
}
return null;
}
}