Completed
Push — release/1.0.0 ( 2e4f33...037c3d )
by Yo
01:46
created

ConstraintPayloadDocHelper::getPayloadDocValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
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 6
    public function appendPayloadDoc(TypeDoc $doc, Constraint $constraint)
26
    {
27 6
        if (!$this->hasPayloadDoc($constraint)) {
28 1
            return;
29
        }
30
31
        // /!\ Do not override value if payload have nothing defined for a key ! /!\
32
33 5
        $doc->setRequired(
34 5
            $this->getPayloadDocValue($constraint, self::PAYLOAD_DOCUMENTATION_REQUIRED_KEY) ?? $doc->isRequired()
35
        );
36 5
        $doc->setNullable(
37 5
            $this->getPayloadDocValue($constraint, self::PAYLOAD_DOCUMENTATION_NULLABLE_KEY) ?? $doc->isNullable()
38
        );
39
40 5
        $doc->setExample(
41 5
            $this->getPayloadDocValue($constraint, self::PAYLOAD_DOCUMENTATION_EXAMPLE_KEY) ?? $doc->getExample()
42
        );
43 5
        $doc->setDefault(
44 5
            $this->getPayloadDocValue($constraint, self::PAYLOAD_DOCUMENTATION_DEFAULT_KEY) ?? $doc->getDefault()
45
        );
46
47 5
        $description = $this->getPayloadDocValue($constraint, self::PAYLOAD_DOCUMENTATION_DESCRIPTION_KEY);
48 5
        if (null !== $description) {
49 1
            $doc->setDescription($description);
50
        }
51 5
    }
52
53
    /**
54
     * @param Constraint $constraint
55
     *
56
     * @return mixed|null Null in case type does not exist
57
     */
58 2
    public function getTypeIfExist(Constraint $constraint)
59
    {
60 2
        return $this->getPayloadDocValue($constraint, self::PAYLOAD_DOCUMENTATION_TYPE_KEY);
61
    }
62
63
    /**
64
     * @param Constraint $constraint
65
     *
66
     * @return bool
67
     */
68 6
    protected function hasPayloadDoc(Constraint $constraint) : bool
69
    {
70 6
        return is_array($constraint->payload)
71 6
            && array_key_exists(self::PAYLOAD_DOCUMENTATION_KEY, $constraint->payload);
72
    }
73
74
    /**
75
     * @param Constraint $constraint
76
     * @param string     $documentationKey
77
     *
78
     * @return mixed|null Return null if value does not exist
79
     */
80 7
    protected function getPayloadDocValue(Constraint $constraint, string $documentationKey)
81
    {
82 7
        return $this->hasPayloadDocKey($constraint, $documentationKey)
83 6
            ? $constraint->payload[self::PAYLOAD_DOCUMENTATION_KEY][$documentationKey]
84 7
            : null
85
        ;
86
    }
87
88
    /**
89
     * @param Constraint $constraint
90
     * @param string     $documentationKey
91
     *
92
     * @return bool
93
     */
94 7
    protected function hasPayloadDocKey(Constraint $constraint, string $documentationKey) : bool
95
    {
96 7
        return array_key_exists($documentationKey, $constraint->payload[self::PAYLOAD_DOCUMENTATION_KEY]);
97
    }
98
}
99