EnumType::getParent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yokai\EnumBundle\Form\Type;
6
7
use Symfony\Component\Form\AbstractType;
8
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
9
use Symfony\Component\OptionsResolver\Options;
10
use Symfony\Component\OptionsResolver\OptionsResolver;
11
use Yokai\EnumBundle\EnumRegistry;
12
13
/**
14
 * @author Yann Eugoné <[email protected]>
15
 */
16
class EnumType extends AbstractType
17
{
18
    /**
19
     * @var EnumRegistry
20
     */
21
    private $enumRegistry;
22
23
    /**
24
     * @param EnumRegistry $enumRegistry
25
     */
26
    public function __construct(EnumRegistry $enumRegistry)
27
    {
28
        $this->enumRegistry = $enumRegistry;
29
    }
30
31
    /**
32
     * @inheritdoc
33
     */
34
    public function configureOptions(OptionsResolver $resolver): void
35
    {
36
        $resolver
37
            ->setRequired('enum')
38
            ->setAllowedValues(
39
                'enum',
40
                function (string $name): bool {
41
                    return $this->enumRegistry->has($name);
42
                }
43
            )
44
            ->setDefault(
45
                'choices',
46
                function (Options $options): array {
47
                    return array_flip($this->enumRegistry->get($options['enum'])->getChoices());
48
                }
49
            )
50
        ;
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56
    public function getParent(): string
57
    {
58
        return ChoiceType::class;
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function getBlockPrefix(): string
65
    {
66
        return 'enum';
67
    }
68
}
69