IdentifiedSiteType::getParent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FH\Bundle\MultiSiteBundle\Form\Type;
6
7
use FH\Bundle\MultiSiteBundle\Site\IdentifiedSiteInterface;
8
use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer;
9
use Symfony\Bridge\Doctrine\Form\EventListener\MergeDoctrineCollectionListener;
10
use Symfony\Component\Form\AbstractType;
11
use Symfony\Component\Form\FormBuilderInterface;
12
use Symfony\Component\OptionsResolver\Options;
13
use Symfony\Component\OptionsResolver\OptionsResolver;
14
15
/**
16
 * @author Joris van de Sande <[email protected]>
17
 */
18
final class IdentifiedSiteType extends AbstractType
19
{
20
    public function buildForm(FormBuilderInterface $builder, array $options): void
21
    {
22
        if ($options['multiple']) {
23
            $builder
24
                ->addEventSubscriber(new MergeDoctrineCollectionListener())
25
                ->addViewTransformer(new CollectionToArrayTransformer(), true);
26
        }
27
    }
28
29
    public function configureOptions(OptionsResolver $resolver): void
30
    {
31
        $resolver->setDefaults([
32
            'exclude_sites' => [],
33
        ]);
34
35
        $resolver->setNormalizer('choices', function (Options $options, $value) {
36
            if ($options['exclude_sites']) {
37
                $value = array_filter($value, function (IdentifiedSiteInterface $site) use ($options): bool {
38
                    return !\in_array($site->getIdentifier(), $options['exclude_sites'], true);
39
                });
40
            }
41
42
            return $value;
43
        });
44
    }
45
46
    public function getParent(): string
47
    {
48
        return SiteType::class;
49
    }
50
}
51