|
1
|
|
|
<?php |
|
2
|
|
|
namespace Yoanm\JsonRpcParamsSymfonyConstraintDoc\App\Helper; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\Validator\Constraint; |
|
5
|
|
|
use Symfony\Component\Validator\Constraints; |
|
6
|
|
|
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\StringDoc; |
|
7
|
|
|
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\TypeDoc; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class StringDocHelper |
|
11
|
|
|
*/ |
|
12
|
|
|
class StringDocHelper |
|
13
|
|
|
{ |
|
14
|
|
|
use ClassComparatorTrait; |
|
15
|
|
|
|
|
16
|
|
|
const CONSTRAINT_WITH_FORMAT_FROM_CLASSNAME = [ |
|
17
|
|
|
Constraints\Bic::class, |
|
18
|
|
|
Constraints\CardScheme::class, |
|
19
|
|
|
Constraints\Country::class, |
|
20
|
|
|
Constraints\Currency::class, |
|
21
|
|
|
Constraints\Date::class, |
|
22
|
|
|
Constraints\DateTime::class, |
|
23
|
|
|
Constraints\Range::class, |
|
24
|
|
|
Constraints\Email::class, |
|
25
|
|
|
Constraints\File::class, |
|
26
|
|
|
Constraints\Iban::class, |
|
27
|
|
|
Constraints\Ip::class, |
|
28
|
|
|
Constraints\Isbn::class, |
|
29
|
|
|
Constraints\Issn::class, |
|
30
|
|
|
Constraints\Language::class, |
|
31
|
|
|
Constraints\Locale::class, |
|
32
|
|
|
Constraints\Luhn::class, |
|
33
|
|
|
Constraints\Time::class, |
|
34
|
|
|
Constraints\Url::class, |
|
35
|
|
|
Constraints\Uuid::class, |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
const CONSTRAINT_WITH_FORMAT_FROM_PROPERTY = [ |
|
39
|
|
|
Constraints\Regex::class => 'pattern', |
|
40
|
|
|
Constraints\Expression::class => 'expression', |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param TypeDoc $doc |
|
45
|
|
|
* @param Constraint $constraint |
|
46
|
|
|
* |
|
47
|
|
|
* @throws \ReflectionException |
|
48
|
|
|
*/ |
|
49
|
175 |
|
public function append(TypeDoc $doc, Constraint $constraint) : void |
|
50
|
|
|
{ |
|
51
|
|
|
// If not a string nor scalar => give up |
|
52
|
175 |
|
if (!$doc instanceof StringDoc) { |
|
53
|
94 |
|
return; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
85 |
|
if (null !== $this->getMatchingClassNameIn($constraint, self::CONSTRAINT_WITH_FORMAT_FROM_CLASSNAME)) { |
|
57
|
59 |
|
$this->enhanceFromClassName($doc, $constraint); |
|
58
|
26 |
|
} elseif (null !== ($match = $this->getMatchingClassNameIn( |
|
59
|
26 |
|
$constraint, |
|
60
|
26 |
|
array_keys(self::CONSTRAINT_WITH_FORMAT_FROM_PROPERTY) |
|
61
|
26 |
|
))) { |
|
62
|
5 |
|
$doc->setFormat($constraint->{self::CONSTRAINT_WITH_FORMAT_FROM_PROPERTY[$match]}); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param StringDoc $doc |
|
68
|
|
|
* @param Constraint $constraint |
|
69
|
|
|
* |
|
70
|
|
|
* @throws \ReflectionException |
|
71
|
|
|
*/ |
|
72
|
59 |
|
private function enhanceFromClassName(StringDoc $doc, Constraint $constraint): void |
|
73
|
|
|
{ |
|
74
|
59 |
|
static $dateTimeClassList = [Constraints\DateTime::class, Constraints\Range::class]; |
|
75
|
59 |
|
if (null !== $this->getMatchingClassNameIn($constraint, $dateTimeClassList)) { |
|
76
|
|
|
// If it's a string range it must be a date range check (either it must be an integer or float value) |
|
77
|
6 |
|
$format = 'datetime'; |
|
78
|
|
|
} else { |
|
79
|
53 |
|
$format = lcfirst((new \ReflectionClass($constraint))->getShortName()); |
|
80
|
|
|
} |
|
81
|
59 |
|
$doc->setFormat($format); |
|
82
|
|
|
|
|
83
|
59 |
|
if ($constraint instanceof Constraints\Uuid) { |
|
84
|
3 |
|
$formatDescription = sprintf( |
|
85
|
3 |
|
'%s (%s)', |
|
86
|
3 |
|
ucfirst($format), |
|
87
|
3 |
|
implode( |
|
88
|
3 |
|
', ', |
|
89
|
3 |
|
array_map( |
|
90
|
3 |
|
function ($version) { |
|
91
|
3 |
|
return sprintf('v%s', $version); |
|
92
|
3 |
|
}, |
|
93
|
3 |
|
$constraint->versions |
|
94
|
3 |
|
) |
|
95
|
3 |
|
) |
|
96
|
3 |
|
); |
|
97
|
3 |
|
$doc->setDescription( |
|
98
|
3 |
|
sprintf( |
|
99
|
3 |
|
'%s%s%s', |
|
100
|
3 |
|
$doc->getDescription(), |
|
101
|
3 |
|
strlen($doc->getDescription() ?? '') ? ' ' : '', |
|
102
|
3 |
|
$formatDescription |
|
103
|
3 |
|
) |
|
104
|
3 |
|
); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|