OperationDataType   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 15
c 1
b 0
f 1
dl 0
loc 29
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configureOptions() 0 7 1
A buildForm() 0 15 4
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