|
1
|
|
|
<?php |
|
2
|
|
|
namespace Yoanm\JsonRpcParamsSymfonyConstraintDoc\App\Helper; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\Validator\Constraint; |
|
5
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
|
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
|
|
|
/** |
|
15
|
|
|
* @param TypeDoc $doc |
|
16
|
|
|
* @param Constraint $constraint |
|
17
|
|
|
* |
|
18
|
|
|
* @throws \ReflectionException |
|
19
|
|
|
*/ |
|
20
|
22 |
|
public function append(TypeDoc $doc, Constraint $constraint) : void |
|
21
|
|
|
{ |
|
22
|
|
|
// If format already defined or type is defined and is not a string nor scalar => give up |
|
23
|
22 |
|
if (!$doc instanceof StringDoc) { |
|
24
|
1 |
|
return; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
21 |
|
$constraintClass = get_class($constraint); |
|
28
|
|
|
$constraintForFormatList = [ |
|
29
|
21 |
|
Assert\Bic::class, |
|
30
|
|
|
Assert\CardScheme::class, |
|
31
|
|
|
Assert\Country::class, |
|
32
|
|
|
Assert\Currency::class, |
|
33
|
|
|
Assert\Date::class, |
|
34
|
|
|
Assert\DateTime::class, |
|
35
|
|
|
Assert\Email::class, |
|
36
|
|
|
Assert\File::class, |
|
37
|
|
|
Assert\Iban::class, |
|
38
|
|
|
Assert\Ip::class, |
|
39
|
|
|
Assert\Isbn::class, |
|
40
|
|
|
Assert\Issn::class, |
|
41
|
|
|
Assert\Language::class, |
|
42
|
|
|
Assert\Locale::class, |
|
43
|
|
|
Assert\Luhn::class, |
|
44
|
|
|
Assert\Time::class, |
|
45
|
|
|
Assert\Url::class, |
|
46
|
|
|
Assert\Uuid::class, |
|
47
|
|
|
]; |
|
48
|
|
|
|
|
49
|
21 |
|
if (in_array($constraintClass, $constraintForFormatList)) { |
|
50
|
18 |
|
if (Assert\DateTime::class === $constraintClass) { |
|
51
|
1 |
|
$format = 'datetime'; |
|
52
|
|
|
} else { |
|
53
|
17 |
|
$format = lcfirst((new \ReflectionClass($constraint))->getShortName()); |
|
54
|
|
|
} |
|
55
|
18 |
|
$doc->setFormat($format); |
|
56
|
|
|
|
|
57
|
18 |
|
if ($constraint instanceof Assert\Uuid) { |
|
58
|
1 |
|
$formatDescription = sprintf( |
|
59
|
1 |
|
'%s (%s)', |
|
60
|
1 |
|
ucfirst($format), |
|
61
|
1 |
|
implode( |
|
62
|
1 |
|
', ', |
|
63
|
1 |
|
array_map( |
|
64
|
|
|
function ($version) { |
|
65
|
1 |
|
return sprintf('v%s', $version); |
|
66
|
1 |
|
}, |
|
67
|
1 |
|
$constraint->versions |
|
68
|
|
|
) |
|
69
|
|
|
) |
|
70
|
|
|
); |
|
71
|
1 |
|
$doc->setDescription( |
|
72
|
1 |
|
sprintf( |
|
73
|
1 |
|
'%s%s%s', |
|
74
|
1 |
|
$doc->getDescription(), |
|
75
|
1 |
|
strlen($doc->getDescription()) ? ' ' : '', |
|
76
|
18 |
|
$formatDescription |
|
77
|
|
|
) |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
3 |
|
} elseif ($constraint instanceof Assert\Regex) { |
|
81
|
1 |
|
$doc->setFormat($constraint->pattern); |
|
82
|
2 |
|
} elseif ($constraint instanceof Assert\Range) { |
|
83
|
|
|
// If it's a string range it must be a date range check (either it must be an integer or float value) |
|
84
|
1 |
|
$doc->setFormat('datetime'); |
|
85
|
1 |
|
} elseif ($constraint instanceof Assert\Expression) { |
|
86
|
|
|
// If it's a string range it must be a date range check (either it must be an integer or float value) |
|
87
|
1 |
|
$doc->setFormat($constraint->expression); |
|
88
|
|
|
} |
|
89
|
21 |
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|