-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
EnumType.php
53 lines (46 loc) · 1.13 KB
/
EnumType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
declare(strict_types=1);
/*
* This file is part of the "elao/enum" package.
*
* Copyright (C) Elao
*
* @author Elao <contact@elao.com>
*/
namespace Elao\Enum\Bridge\Symfony\Form\Type;
use Elao\Enum\ReadableEnumInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EnumType as SymfonyEnumType;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* @final
*/
class EnumType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver): void
{
$resolver
// Override the original type logic for label for readable enums.
->setDefault('choice_label', static function (\UnitEnum $choice): string {
return $choice instanceof ReadableEnumInterface ? $choice->getReadable() : $choice->name;
})
;
}
/**
* {@inheritdoc}
*/
public function getParent(): ?string
{
return SymfonyEnumType::class;
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix(): string
{
return 'elao_enum';
}
}