forked from nelmio/NelmioApiDocBundle
/
DunglasApiParser.php
193 lines (161 loc) · 5.77 KB
/
DunglasApiParser.php
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
/*
* This file is part of the NelmioApiDocBundle.
*
* (c) Nelmio <hello@nelm.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Parser;
use Dunglas\ApiBundle\Api\ResourceCollectionInterface;
use Dunglas\ApiBundle\Api\ResourceInterface;
use Dunglas\ApiBundle\Mapping\AttributeMetadataInterface;
use Dunglas\ApiBundle\Mapping\ClassMetadataFactoryInterface;
use Nelmio\ApiDocBundle\DataTypes;
use PropertyInfo\Type;
/**
* Use DunglasApiBundle to extract input and output information.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class DunglasApiParser implements ParserInterface
{
const IN_PREFIX = 'dunglas_api_in';
const OUT_PREFIX = 'dunglas_api_out';
const IRI = 'IRI';
private static $typeMap = array(
'int' => DataTypes::INTEGER,
'bool' => DataTypes::BOOLEAN,
'string' => DataTypes::STRING,
'float' => DataTypes::FLOAT,
);
/**
* @var ResourceCollectionInterface
*/
private $resourceCollection;
/**
* @var ClassMetadataFactory
*/
private $classMetadataFactory;
public function __construct(
ResourceCollectionInterface $resourceCollection,
ClassMetadataFactoryInterface $classMetadataFactory
) {
$this->resourceCollection = $resourceCollection;
$this->classMetadataFactory = $classMetadataFactory;
}
/**
* {@inheritdoc}
*/
public function supports(array $item)
{
$data = explode(':', $item['class'], 2);
if (isset($data[1])) {
return null !== $this->resourceCollection->getResourceForEntity($data[1]);
}
return false;
}
/**
* {@inheritdoc}
*/
public function parse(array $item)
{
list($io, $entityClass) = explode(':', $item['class'], 2);
$resource = $this->resourceCollection->getResourceForEntity($entityClass);
return $this->parseClass($resource, $entityClass, $io);
}
/**
* Parses a class.
*
* @param ResourceInterface $resource
* @param string $entityClass
* @param string $io
*
* @return array
*/
private function parseClass(ResourceInterface $resource, $entityClass, $io)
{
$classMetadata = $this->classMetadataFactory->getMetadataFor(
$entityClass,
$resource->getNormalizationGroups(),
$resource->getDenormalizationGroups(),
$resource->getValidationGroups()
);
$data = array();
foreach ($classMetadata->getAttributes() as $attributeMetadata) {
if (
(!$attributeMetadata->isIdentifier() && $attributeMetadata->isReadable() && self::OUT_PREFIX === $io) ||
($attributeMetadata->isWritable() && self::IN_PREFIX === $io)
) {
$data[$attributeMetadata->getName()] = $this->parseAttribute($resource, $attributeMetadata, $io);
}
}
return $data;
}
/**
* Parses an attribute.
*
* @param ResourceInterface $resource
* @param AttributeMetadataInterface $attributeMetadata
* @param string $io
* @param Type|null $type
*
* @return array
*/
private function parseAttribute(ResourceInterface $resource, AttributeMetadataInterface $attributeMetadata, $io, Type $type = null)
{
$data = array(
'dataType' => null,
'required' => $attributeMetadata->isRequired(),
'description' => $attributeMetadata->getDescription(),
'readonly' => !$attributeMetadata->isWritable(),
);
if (null == $type) {
if (!isset($attributeMetadata->getTypes()[0])) {
// Default to string
$data['dataType'] = DataTypes::STRING;
return $data;
}
// Use the first type found as primary
$type = $attributeMetadata->getTypes()[0];
}
if ($type->isCollection()) {
$data['actualType'] = DataTypes::COLLECTION;
if ($collectionType = $type->getCollectionType()) {
$subAttribute = $this->parseAttribute($resource, $attributeMetadata, $io, $collectionType);
if (self::IRI === $subAttribute['dataType']) {
$data['dataType'] = 'array of IRIs';
$data['subType'] = DataTypes::STRING;
return $data;
}
$data['subType'] = $subAttribute['subType'];
$data['children'] = $subAttribute['children'];
}
return $data;
}
$phpType = $type->getType();
if ('object' === $phpType) {
$class = $type->getClass();
if ('DateTime' === $class) {
$data['dataType'] = DataTypes::DATETIME;
$data['format'] = sprintf('{DateTime %s}', \DateTime::ATOM);
return $data;
}
if (
(self::OUT_PREFIX === $io && $attributeMetadata->isNormalizationLink()) ||
(self::IN_PREFIX === $io && $attributeMetadata->isDenormalizationLink())
) {
$data['dataType'] = self::IRI;
$data['actualType'] = DataTypes::STRING;
return $data;
}
$data['actualType'] = DataTypes::MODEL;
$data['subType'] = $class;
$data['children'] = $resource->getEntityClass() === $class ? [] : $this->parseClass($resource, $class, $io);
return $data;
}
$data['dataType'] = isset(self::$typeMap[$type->getType()]) ? self::$typeMap[$type->getType()] : DataTypes::STRING;
return $data;
}
}