OperationDataType::configureOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Form\Type;
6
7
use Symfony\Component\Form\AbstractType;
8
use Symfony\Component\Form\FormBuilderInterface;
9
use Symfony\Component\Form\FormEvent;
10
use Symfony\Component\Form\FormEvents;
11
use Symfony\Component\OptionsResolver\OptionsResolver;
12
use Symfony\Component\PropertyAccess\PropertyAccess;
13
14
class OperationDataType extends AbstractType
15
{
16
    public function buildForm(FormBuilderInterface $builder, array $options)
17
    {
18
        $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) {
19
            $data = $event->getData();
20
            $form = $event->getForm();
21
            $accessor = PropertyAccess::createPropertyAccessor();
22
            $reflection = new \ReflectionClass($data);
23
24
            foreach ($reflection->getProperties() as $property) {
25
                if (\in_array($property->getName(), $options['exclude'])) {
26
                    continue;
27
                }
28
29
                if ($accessor->isWritable($data, $property->getName())) {
30
                    $form->add($property->getName());
31
                }
32
            }
33
        });
34
    }
35
36
    public function configureOptions(OptionsResolver $resolver)
37
    {
38
        $resolver
39
            ->setDefaults([
40
               'exclude' => [],
41
            ])
42
            ->setAllowedTypes('exclude', 'array')
43
        ;
44
    }
45
}
46