1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\SwaggerBundle package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
namespace KleijnWeb\SwaggerBundle\Serialize\TypeResolver; |
9
|
|
|
|
10
|
|
|
use KleijnWeb\SwaggerBundle\Document\Specification; |
11
|
|
|
use KleijnWeb\SwaggerBundle\Document\Specification\Operation; |
12
|
|
|
|
13
|
|
|
class SerializerTypeDefinitionMapBuilder |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var TypeNameResolver |
17
|
|
|
*/ |
18
|
|
|
private $typeNameResolver; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var ClassNameResolver |
22
|
|
|
*/ |
23
|
|
|
private $classNameResolver; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* SerializerTypeDefinitionMapBuilder constructor. |
27
|
|
|
* |
28
|
|
|
* @param TypeNameResolver $typeNameResolver |
29
|
|
|
* @param ClassNameResolver $classNameResolver |
30
|
|
|
*/ |
31
|
|
|
public function __construct(TypeNameResolver $typeNameResolver, ClassNameResolver $classNameResolver) |
32
|
|
|
{ |
33
|
|
|
$this->typeNameResolver = $typeNameResolver; |
34
|
|
|
$this->classNameResolver = $classNameResolver; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param Specification $specification |
39
|
|
|
* |
40
|
|
|
* @return SerializerTypeDefinitionMap |
41
|
|
|
*/ |
42
|
|
|
public function build(Specification $specification): SerializerTypeDefinitionMap |
43
|
|
|
{ |
44
|
|
|
$typeNames = []; |
45
|
|
|
$definitions = []; |
46
|
|
|
|
47
|
|
|
$add = function ($schema, $key, $typeName, $operationId = null) use (&$typeNames, &$definitions) { |
48
|
|
|
if (!isset($schema->{'x-class'})) { |
49
|
|
|
$schema->{'x-class'} = $this->classNameResolver->resolve($typeName); |
50
|
|
|
} |
51
|
|
|
$definitions[$key] = $schema; |
52
|
|
|
$typeNames[$schema->{'x-class'}] = $typeName; |
53
|
|
|
if ($operationId) { |
54
|
|
|
$typeNames["_op_$operationId"] = $typeName; |
55
|
|
|
} |
56
|
|
|
}; |
57
|
|
|
|
58
|
|
|
$specification->apply(function (&$schema) use ($add) { |
59
|
|
|
if ($schema instanceof \stdClass) { |
60
|
|
|
if ($this->typeNameResolver->isResolvableSchema($schema)) { |
61
|
|
|
$typeName = $key = $this->typeNameResolver->resolveUsingSchema($schema); |
62
|
|
|
$add($schema, $key, $typeName); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
}); |
66
|
|
|
|
67
|
|
|
$operations = $specification->getOperations(); |
68
|
|
|
array_walk($operations, function (Operation $operation) use ($add) { |
69
|
|
|
if ($operation->hasParameters()) { |
70
|
|
|
foreach ($operation->getParameters() as $parameterDefinition) { |
71
|
|
|
if ($parameterDefinition->in == 'body' && isset($parameterDefinition->schema)) { |
72
|
|
|
$typeName = $this->typeNameResolver->resolveUsingSchema($parameterDefinition->schema); |
73
|
|
|
$add($parameterDefinition->schema, "_op_{$operation->getId()}", $typeName, $operation->getId()); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
}); |
78
|
|
|
|
79
|
|
|
return new SerializerTypeDefinitionMap($definitions, $typeNames); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|