|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace W2w\Lib\Apie\OpenApiSchema\SubActions; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use ReflectionMethod; |
|
8
|
|
|
use ReflectionType; |
|
9
|
|
|
use Symfony\Component\PropertyInfo\Type; |
|
10
|
|
|
use Symfony\Component\Serializer\NameConverter\NameConverterInterface; |
|
11
|
|
|
|
|
12
|
|
|
class SubActionFactory |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var NameConverterInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
private $nameConverter; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(NameConverterInterface $nameConverter) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->nameConverter = $nameConverter; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function createFromReflectionMethod(string $name, string $resourceClass, ReflectionMethod $reflectionMethod, ?object $object = null): ?SubAction |
|
25
|
|
|
{ |
|
26
|
|
|
$parameters = $reflectionMethod->getParameters(); |
|
27
|
|
|
$returnType = null; |
|
28
|
|
|
if ($reflectionMethod->getReturnType()) { |
|
29
|
|
|
$returnType = $this->fromReflectionType($reflectionMethod->getReturnType()); |
|
30
|
|
|
} |
|
31
|
|
|
if (empty($parameters)) { |
|
32
|
|
|
return new SubAction($name, [], $reflectionMethod, $returnType, $object); |
|
33
|
|
|
} |
|
34
|
|
|
$firstParameter = array_shift($parameters); |
|
35
|
|
|
$wantedClass = $firstParameter->getType(); |
|
36
|
|
|
// if there is no typehint or the typehint is the desired class this sub action is correct. |
|
37
|
|
|
if (!$wantedClass || is_a($wantedClass->getName(), $resourceClass, true)) { |
|
38
|
|
|
$types = []; |
|
39
|
|
|
foreach ($parameters as $parameter) { |
|
40
|
|
|
$fieldName = $this->nameConverter->denormalize($parameter->getName()); |
|
41
|
|
|
$types[$fieldName] = null; |
|
42
|
|
|
if ($parameter->getType()) { |
|
43
|
|
|
$types[$fieldName] = $this->fromReflectionType($parameter->getType()); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
return new SubAction($name, $types, $reflectionMethod, $returnType, $object); |
|
47
|
|
|
} |
|
48
|
|
|
return null; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
private function fromReflectionType(ReflectionType $reflectionType): Type |
|
52
|
|
|
{ |
|
53
|
|
|
$nullable = $reflectionType->allowsNull(); |
|
54
|
|
|
if ($reflectionType->getName() === Type::BUILTIN_TYPE_ARRAY) { |
|
55
|
|
|
return new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true); |
|
56
|
|
|
} |
|
57
|
|
|
if ($reflectionType->getName() === 'void') { |
|
58
|
|
|
return new Type(Type::BUILTIN_TYPE_NULL, $nullable); |
|
59
|
|
|
} |
|
60
|
|
|
if ($reflectionType->isBuiltin()) { |
|
61
|
|
|
return new Type($reflectionType->getName(), $nullable); |
|
62
|
|
|
} |
|
63
|
|
|
return new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $reflectionType->getName()); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|