Passed
Pull Request — feature/fix (#19)
by Yo
01:25
created

StringDocHelper::enhanceFromClassName()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 21
nc 4
nop 2
dl 0
loc 30
ccs 21
cts 21
cp 1
crap 5
rs 9.2728
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
    const CONSTRAINT_WITH_FORMAT_FROM_CLASSNAME = [
15
        Constraints\Bic::class => true,
16
        Constraints\CardScheme::class => true,
17
        Constraints\Country::class => true,
18
        Constraints\Currency::class => true,
19
        Constraints\Date::class => true,
20
        Constraints\DateTime::class => true,
21
        Constraints\Range::class => true,
22
        Constraints\Email::class => true,
23
        Constraints\File::class => true,
24
        Constraints\Iban::class => true,
25
        Constraints\Ip::class => true,
26
        Constraints\Isbn::class => true,
27
        Constraints\Issn::class => true,
28
        Constraints\Language::class => true,
29
        Constraints\Locale::class => true,
30
        Constraints\Luhn::class => true,
31
        Constraints\Time::class => true,
32
        Constraints\Url::class => true,
33
        Constraints\Uuid::class => true,
34
    ];
35
36
    const CONSTRAINT_WITH_FORMAT_FROM_PROPERTY = [
37
        Constraints\Regex::class => 'pattern',
38
        Constraints\Expression::class => 'expression',
39
    ];
40
41
    /**
42
     * @param TypeDoc    $doc
43
     * @param Constraint $constraint
44
     *
45
     * @throws \ReflectionException
46
     */
47 22
    public function append(TypeDoc $doc, Constraint $constraint) : void
48
    {
49
        // If not a string nor scalar => give up
50 22
        if (!$doc instanceof StringDoc) {
51 1
            return;
52
        }
53
54 21
        $constraintClass = get_class($constraint);
55
56 21
        if (array_key_exists($constraintClass, self::CONSTRAINT_WITH_FORMAT_FROM_CLASSNAME)) {
57 19
            $this->enhanceFromClassName($doc, $constraint);
58 2
        } elseif (array_key_exists($constraintClass, self::CONSTRAINT_WITH_FORMAT_FROM_PROPERTY)) {
59 2
            $propertyName = self::CONSTRAINT_WITH_FORMAT_FROM_PROPERTY[$constraintClass];
60 2
            $doc->setFormat($constraint->{$propertyName});
61
        }
62 21
    }
63
64
    /**
65
     * @param StringDoc    $doc
66
     * @param Constraint $constraint
67
     *
68
     * @throws \ReflectionException
69
     */
70 19
    private function enhanceFromClassName(StringDoc $doc, Constraint $constraint): void
71
    {
72 19
        if ($constraint instanceof Constraints\DateTime || $constraint instanceof Constraints\Range) {
73
            // If it's a string range it must be a date range check (either it must be an integer or float value)
74 2
            $format = 'datetime';
75
        } else {
76 17
            $format = lcfirst((new \ReflectionClass($constraint))->getShortName());
77
        }
78 19
        $doc->setFormat($format);
79
80 19
        if ($constraint instanceof Constraints\Uuid) {
81 1
            $formatDescription = sprintf(
82 1
                '%s (%s)',
83 1
                ucfirst($format),
84 1
                implode(
85 1
                    ', ',
86 1
                    array_map(
87
                        function ($version) {
88 1
                            return sprintf('v%s', $version);
89 1
                        },
90 1
                        $constraint->versions
91
                    )
92
                )
93
            );
94 1
            $doc->setDescription(
95 1
                sprintf(
96 1
                    '%s%s%s',
97 1
                    $doc->getDescription(),
98 1
                    strlen($doc->getDescription()) ? ' ' : '',
99 1
                    $formatDescription
100
                )
101
            );
102
        }
103 19
    }
104
}
105