|
1
|
|
|
<?php |
|
2
|
|
|
namespace Yoanm\JsonRpcParamsSymfonyConstraintDoc\App\Helper; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\Validator\Constraint; |
|
5
|
|
|
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\TypeDoc; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class ConstraintPayloadDocHelper |
|
9
|
|
|
*/ |
|
10
|
|
|
class ConstraintPayloadDocHelper |
|
11
|
|
|
{ |
|
12
|
|
|
/** Use this key in constraint payload to define parameter documentation */ |
|
13
|
|
|
const PAYLOAD_DOCUMENTATION_KEY = 'documentation'; |
|
14
|
|
|
const PAYLOAD_DOCUMENTATION_TYPE_KEY = 'type'; |
|
15
|
|
|
const PAYLOAD_DOCUMENTATION_REQUIRED_KEY = 'required'; |
|
16
|
|
|
const PAYLOAD_DOCUMENTATION_NULLABLE_KEY = 'nullable'; |
|
17
|
|
|
const PAYLOAD_DOCUMENTATION_DESCRIPTION_KEY = 'description'; |
|
18
|
|
|
const PAYLOAD_DOCUMENTATION_DEFAULT_KEY = 'default'; |
|
19
|
|
|
const PAYLOAD_DOCUMENTATION_EXAMPLE_KEY = 'example'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param TypeDoc $doc |
|
23
|
|
|
* @param Constraint $constraint |
|
24
|
|
|
*/ |
|
25
|
159 |
|
public function appendPayloadDoc(TypeDoc $doc, Constraint $constraint) : void |
|
26
|
|
|
{ |
|
27
|
159 |
|
if (!$this->hasPayloadDoc($constraint)) { |
|
28
|
61 |
|
return; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
// /!\ Do not override value if payload have nothing defined for a key ! /!\ |
|
32
|
|
|
|
|
33
|
103 |
|
$doc->setRequired( |
|
34
|
103 |
|
$this->getPayloadDocValue($constraint, self::PAYLOAD_DOCUMENTATION_REQUIRED_KEY) ?? $doc->isRequired() |
|
35
|
103 |
|
); |
|
36
|
103 |
|
$doc->setNullable( |
|
37
|
103 |
|
$this->getPayloadDocValue($constraint, self::PAYLOAD_DOCUMENTATION_NULLABLE_KEY) ?? $doc->isNullable() |
|
38
|
103 |
|
); |
|
39
|
|
|
|
|
40
|
103 |
|
$doc->setExample( |
|
41
|
103 |
|
$this->getPayloadDocValue($constraint, self::PAYLOAD_DOCUMENTATION_EXAMPLE_KEY) ?? $doc->getExample() |
|
42
|
103 |
|
); |
|
43
|
103 |
|
$doc->setDefault( |
|
44
|
103 |
|
$this->getPayloadDocValue($constraint, self::PAYLOAD_DOCUMENTATION_DEFAULT_KEY) ?? $doc->getDefault() |
|
45
|
103 |
|
); |
|
46
|
|
|
|
|
47
|
103 |
|
$description = $this->getPayloadDocValue($constraint, self::PAYLOAD_DOCUMENTATION_DESCRIPTION_KEY); |
|
48
|
103 |
|
if (null !== $description) { |
|
49
|
76 |
|
$doc->setDescription($description); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param Constraint $constraint |
|
55
|
|
|
* |
|
56
|
|
|
* @return mixed|null Null in case type does not exist |
|
57
|
|
|
*/ |
|
58
|
156 |
|
public function getTypeIfExist(Constraint $constraint) |
|
59
|
|
|
{ |
|
60
|
156 |
|
return $this->hasPayloadDoc($constraint) |
|
61
|
100 |
|
? $this->getPayloadDocValue($constraint, self::PAYLOAD_DOCUMENTATION_TYPE_KEY) |
|
62
|
156 |
|
: null |
|
63
|
156 |
|
; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param Constraint $constraint |
|
68
|
|
|
* |
|
69
|
|
|
* @return bool |
|
70
|
|
|
*/ |
|
71
|
162 |
|
protected function hasPayloadDoc(Constraint $constraint) : bool |
|
72
|
|
|
{ |
|
73
|
162 |
|
return is_array($constraint->payload) |
|
74
|
162 |
|
&& array_key_exists(self::PAYLOAD_DOCUMENTATION_KEY, $constraint->payload); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param Constraint $constraint |
|
79
|
|
|
* @param string $documentationKey |
|
80
|
|
|
* |
|
81
|
|
|
* @return mixed|null Return null if value does not exist |
|
82
|
|
|
*/ |
|
83
|
105 |
|
protected function getPayloadDocValue(Constraint $constraint, string $documentationKey) |
|
84
|
|
|
{ |
|
85
|
105 |
|
return $this->hasPayloadDocKey($constraint, $documentationKey) |
|
86
|
104 |
|
? $constraint->payload[self::PAYLOAD_DOCUMENTATION_KEY][$documentationKey] |
|
87
|
105 |
|
: null |
|
88
|
105 |
|
; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param Constraint $constraint |
|
93
|
|
|
* @param string $documentationKey |
|
94
|
|
|
* |
|
95
|
|
|
* @return bool |
|
96
|
|
|
*/ |
|
97
|
105 |
|
protected function hasPayloadDocKey(Constraint $constraint, string $documentationKey) : bool |
|
98
|
|
|
{ |
|
99
|
105 |
|
return array_key_exists($documentationKey, $constraint->payload[self::PAYLOAD_DOCUMENTATION_KEY]); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|