Passed
Push — master ( d8fa97...555fd2 )
by Yo
01:32
created

StringDocHelper::append()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 2
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 4
rs 10
c 0
b 0
f 0
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 22
    public function append(TypeDoc $doc, Constraint $constraint) : void
50
    {
51
        // If not a string nor scalar => give up
52 22
        if (!$doc instanceof StringDoc) {
53 1
            return;
54
        }
55
56 21
        if (null !== $this->getMatchingClassNameIn($constraint, self::CONSTRAINT_WITH_FORMAT_FROM_CLASSNAME)) {
57 19
            $this->enhanceFromClassName($doc, $constraint);
58 2
        } elseif (null !== ($match = $this->getMatchingClassNameIn(
59 2
            $constraint,
60 2
            array_keys(self::CONSTRAINT_WITH_FORMAT_FROM_PROPERTY)
61
        ))) {
62 2
            $doc->setFormat($constraint->{self::CONSTRAINT_WITH_FORMAT_FROM_PROPERTY[$match]});
63
        }
64 21
    }
65
66
    /**
67
     * @param StringDoc    $doc
68
     * @param Constraint $constraint
69
     *
70
     * @throws \ReflectionException
71
     */
72 19
    private function enhanceFromClassName(StringDoc $doc, Constraint $constraint): void
73
    {
74 19
        static $dateTimeClassList = [Constraints\DateTime::class, Constraints\Range::class];
75 19
        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 2
            $format = 'datetime';
78
        } else {
79 17
            $format = lcfirst((new \ReflectionClass($constraint))->getShortName());
80
        }
81 19
        $doc->setFormat($format);
82
83 19
        if ($constraint instanceof Constraints\Uuid) {
84 1
            $formatDescription = sprintf(
85 1
                '%s (%s)',
86 1
                ucfirst($format),
87 1
                implode(
88 1
                    ', ',
89 1
                    array_map(
90
                        function ($version) {
91 1
                            return sprintf('v%s', $version);
92 1
                        },
93 1
                        $constraint->versions
94
                    )
95
                )
96
            );
97 1
            $doc->setDescription(
98 1
                sprintf(
99 1
                    '%s%s%s',
100 1
                    $doc->getDescription(),
101 1
                    strlen($doc->getDescription()) ? ' ' : '',
102 1
                    $formatDescription
103
                )
104
            );
105
        }
106 19
    }
107
}
108