Passed
Pull Request — master (#12)
by Stanislau
03:14
created

ChoiceStrategy::createChoiceArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 12
ccs 7
cts 8
cp 0.875
crap 2.0078
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 *  This file is part of the Micro framework package.
7
 *
8
 *  (c) Stanislau Komar <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Micro\Library\DTO\Preparation\Processor\Property\Assert;
15
16
use Symfony\Component\Validator\Constraints\Choice;
17
18
class ChoiceStrategy extends AbstractComparisonStrategy
19
{
20 1
    protected function generateArguments(array $config): array
21
    {
22 1
        $parent = parent::generateArguments($config);
23
24 1
        $current = [
25 1
            'choices' => $this->createChoiceArray($config),
26 1
            'callback' => $config['callback'] ?? null,
27 1
            'multiple' => $this->sanitize($config['multiple'] ?? false),
28 1
            'multipleMessage' => $config['message_multiple'] ?? null,
29 1
            'match' => $this->sanitize($config['match'] ?? false),
30 1
        ];
31
32 1
        return array_filter(
33 1
            array_merge($parent, $current)
34 1
        );
35
    }
36
37
    /**
38
     * @param array<string|mixed> $config
39
     *
40
     * @return array<int|string|float|bool|null>|null
41
     */
42 1
    protected function createChoiceArray(array $config): array|null
43
    {
44 1
        $choices = trim($config['choices'] ?? '');
45 1
        if (!$choices) {
46
            return null;
47
        }
48
49 1
        $values = $this->explodeString($choices);
50
51 1
        return array_map(function (mixed $value) {
52 1
            return $this->sanitize($value);
53 1
        }, $values);
54
    }
55
56 1
    protected function getValidatorProperty(): string
57
    {
58 1
        return 'choice';
59
    }
60
61 1
    protected function getAttributeClassName(): string
62
    {
63 1
        return Choice::class;
64
    }
65
}
66