1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace hiapi\validators\Reflection; |
5
|
|
|
|
6
|
|
|
use hiapi\validators\IdValidator; |
7
|
|
|
use phpDocumentor\Reflection\DocBlock; |
8
|
|
|
use phpDocumentor\Reflection\DocBlockFactory; |
9
|
|
|
use phpDocumentor\Reflection\TypeResolver; |
10
|
|
|
use phpDocumentor\Reflection\Types\ContextFactory; |
11
|
|
|
use ReflectionObject; |
12
|
|
|
use yii\validators\RegularExpressionValidator; |
13
|
|
|
use yii\validators\Validator; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class ValidatorReflection inspects project Yii-based Validator and |
17
|
|
|
* |
18
|
|
|
* @author Dmytro Naumenko <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class ValidatorReflection |
21
|
|
|
{ |
22
|
|
|
protected ContextFactory $contextFactory; |
|
|
|
|
23
|
|
|
protected TypeResolver $typeResolver; |
24
|
|
|
protected ReflectionObject $reflection; |
25
|
|
|
protected Validator $validator; |
26
|
|
|
protected ?DocBlockFactory $docBlockFactory; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @psalm-param class-sting<Validator> |
30
|
|
|
*/ |
31
|
|
|
public static function fromClassname(string $className): self |
32
|
|
|
{ |
33
|
|
|
$validator = (new \ReflectionClass($className))->newInstanceWithoutConstructor(); |
34
|
|
|
|
35
|
|
|
return new self($validator); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function __construct( |
39
|
|
|
Validator $validator, |
40
|
|
|
DocBlockFactory $docBlockFactory = null, |
41
|
|
|
ContextFactory $contextFactory = null, |
42
|
|
|
TypeResolver $typeResolver = null |
43
|
|
|
) { |
44
|
|
|
$this->validator = $validator; |
45
|
|
|
$this->reflection = new ReflectionObject($validator); |
46
|
|
|
|
47
|
|
|
$this->docBlockFactory = $docBlockFactory ?? DocBlockFactory::createInstance(); |
48
|
|
|
$this->contextFactory = $contextFactory ?? new ContextFactory(); |
49
|
|
|
$this->typeResolver = $typeResolver ?? new TypeResolver(); |
50
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getPattern(): ?string |
54
|
|
|
{ |
55
|
|
|
if ($this->reflection->isSubclassOf(RegularExpressionValidator::class)) { |
56
|
|
|
return $this->reflection->getDefaultProperties()['pattern']; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return null; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getOpenApiType(): string |
63
|
|
|
{ |
64
|
|
|
if ($this->validator instanceof IdValidator) { // TODO: somehow better |
65
|
|
|
return 'int'; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return 'string'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function getDocBlock(): ?DocBlock |
72
|
|
|
{ |
73
|
|
|
try { |
74
|
|
|
return $this->docBlockFactory->create($this->reflection); |
75
|
|
|
} catch (\InvalidArgumentException $e) { |
76
|
|
|
return null; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getSummary(): string |
81
|
|
|
{ |
82
|
|
|
$docBlock = $this->getDocBlock(); |
83
|
|
|
if ($docBlock === null) { |
84
|
|
|
return ''; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $docBlock->getSummary(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getDescription(): string |
91
|
|
|
{ |
92
|
|
|
$docBlock = $this->getDocBlock(); |
93
|
|
|
if ($docBlock === null) { |
94
|
|
|
return ''; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $docBlock->getDescription()->__toString(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getExample(): ?string |
101
|
|
|
{ |
102
|
|
|
$docBlock = $this->getDocBlock(); |
103
|
|
|
if ($docBlock === null) { |
104
|
|
|
return null; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$examples = $docBlock->getTagsByName('example'); |
108
|
|
|
if (empty($examples)) { |
109
|
|
|
return null; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return reset($examples)->__toString(); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|