ParameterDto::normalizeName()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 14
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 10
cc 3
nc 4
nop 2
crap 3
1
<?php
2
3
namespace kalanis\Pohoda\Type\Dtos;
4
5
use kalanis\Pohoda\Common\Attributes;
6
use kalanis\Pohoda\Common\Dtos\AbstractDto;
7
use kalanis\Pohoda\Common\OptionsResolver;
8
use kalanis\Pohoda\Type\Enums;
9
use kalanis\PohodaException;
10
11
/**
12
 * Basic DTO for parameters
13
 */
14
class ParameterDto extends AbstractDto
15
{
16
    #[Attributes\Options\CallbackOption(['\kalanis\Pohoda\Type\Dtos\ParameterDto', 'normalizeName']), Attributes\Options\RequiredOption]
17
    public ?string $name = null;
18
    #[Attributes\Options\EnumOption(Enums\ParameterTypeEnum::class), Attributes\Options\RequiredOption]
19
    public Enums\ParameterTypeEnum|string|null $type = null;
20
    #[Attributes\Options\CallbackOption(['\kalanis\Pohoda\Type\Dtos\ParameterDto', 'normalizeList'])]
21
    public mixed $value = null;
22
    public mixed $list = null;
23
24 17
    public static function normalizeName(OptionsResolver $options, mixed $value): string
25
    {
26 17
        $prefix = 'VPr';
27 17
        $value = \strval($value);
28
29 17
        if ('list' == $options['type']) {
30 13
            $prefix = 'RefVPr';
31
        }
32
33 17
        if (\str_starts_with($value, $prefix)) {
34 14
            return $value;
35
        }
36
37 14
        return $prefix . $value;
38
    }
39
40 17
    public static function normalizeList(OptionsResolver $options, mixed $value): mixed
41
    {
42 17
        $normalizer = $options['type'];
43
44
        // date for datetime
45 17
        if ('datetime' == $normalizer) {
46 1
            $normalizer = 'date';
47
        }
48
49
        try {
50 17
            $normalizerClass = OptionsResolver\Normalizers\NormalizerFactory::createNormalizer(\strval($normalizer));
51 14
            return \call_user_func($normalizerClass->normalize(...), [], $value);
52 14
        } catch (PohodaException) {
53 14
            return \is_array($value) ? $value : \strval($value);
54
        }
55
    }
56
}
57