DictionaryType::getParent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Knp\DictionaryBundle\Form\Type;
6
7
use Knp\DictionaryBundle\Dictionary\Collection;
8
use Symfony\Component\Form\AbstractType;
9
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
10
use Symfony\Component\OptionsResolver\Options;
11
use Symfony\Component\OptionsResolver\OptionsResolver;
12
13
/**
14
 * @extends AbstractType<mixed>
15
 */
16
final class DictionaryType extends AbstractType
17
{
18
    public function __construct(private readonly Collection $collection) {}
19
20 3
    public function configureOptions(OptionsResolver $optionsResolver): void
21
    {
22 3
        $dictionaries = $this->collection;
23 3
24
        $choices = static function (Options $options) use ($dictionaries): array {
25 1
            $name    = $options['name'];
26
            $choices = $dictionaries[$name]->getValues();
27 1
28
            return array_flip($choices);
29 1
        };
30 1
31 1
        $optionsResolver
32
            ->setDefault('choices', $choices)
33 1
            ->setRequired(['name'])
34 1
            ->setAllowedValues('name', array_keys(iterator_to_array($this->collection)))
35
        ;
36
    }
37 1
38 1
    public function getParent(): string
39 1
    {
40
        return ChoiceType::class;
41 1
    }
42
}
43