-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
FlagBagType.php
62 lines (53 loc) · 1.59 KB
/
FlagBagType.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
54
55
56
57
58
59
60
61
62
<?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\Bridge\Symfony\Form\DataTransformer\FlagBagToCollectionTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Exception\InvalidConfigurationException;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* @final
*/
class FlagBagType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
if (!$options['multiple']) {
throw new InvalidConfigurationException(sprintf(
'The "multiple" option of the "%s" form type cannot be set to false.',
static::class
));
}
if (!\is_string($options['class']) || !is_subclass_of($options['class'], \BackedEnum::class)) {
throw new InvalidConfigurationException(sprintf(
'The "class" option of the "%s" form type must contains the FQCN of a BackedEnum.',
static::class
));
}
$builder->addModelTransformer(new FlagBagToCollectionTransformer($options['class']));
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefault('multiple', true)
;
}
/**
* {@inheritdoc}
*/
public function getParent(): ?string
{
return EnumType::class;
}
}