Passed
Push — develop ( b8535a...b19a66 )
by Mathieu
11:29
created

FormReader::configureEditFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Neimheadh\SonataAnnotationBundle\Reader;
6
7
use Doctrine\Common\Annotations\Reader;
8
use Doctrine\ORM\Mapping\GeneratedValue;
9
use Neimheadh\SonataAnnotationBundle\Annotation\Sonata\Admin;
10
use Neimheadh\SonataAnnotationBundle\Annotation\Sonata\FormField;
11
use ReflectionClass;
12
use Sonata\AdminBundle\Form\FormMapper;
13
14
/**
15
 * Form configuration reader.
16
 *
17
 * @author Marko Kunic <[email protected]>
18
 * @author Mathieu Wambre <[email protected]>
19
 */
20
final class FormReader extends AbstractFieldConfigurationReader
21
{
22
23
    /**
24
     * {@inheritDoc}
25
     */
26
    public function __construct(
27
        Reader $annotationReader
28
    ) {
29
        parent::__construct($annotationReader, FormField::class);
30
    }
31
32
    /**
33
     * Build creation fields configuration.
34
     *
35
     * @param ReflectionClass $class      Entity class.
36
     * @param FormMapper      $formMapper Admin form mapper.
37
     *
38
     * @return void
39
     */
40
    public function configureCreateFields(
41
        ReflectionClass $class,
42
        FormMapper $formMapper
43
    ): void {
44
        $this->configureReaderFields(
45
            $class,
46
            $formMapper,
47
            $this->annotationClass,
48
            FormField::ACTION_CREATE
49
        );
50
    }
51
52
    /**
53
     * Build edit fields configuration.
54
     *
55
     * @param ReflectionClass $class      Entity class.
56
     * @param FormMapper      $formMapper Admin form mapper.
57
     *
58
     * @return void
59
     */
60
    public function configureEditFields(
61
        ReflectionClass $class,
62
        FormMapper $formMapper
63
    ): void {
64
        $this->configureReaderFields(
65
            $class,
66
            $formMapper,
67
            $this->annotationClass,
68
            FormField::ACTION_EDIT
69
        );
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75
    protected function getAdminAnnotationFields(
76
        Admin $annotation,
77
        ?string $action
78
    ): array {
79
        return $annotation->getFormFields();
80
    }
81
82
    /**
83
     * {@inheritDoc}
84
     */
85
    protected function loadDefaultFields(
86
        ReflectionClass $class,
87
        string $annotationClass
88
    ): array {
89
        $properties = [];
90
91
        foreach ($class->getProperties() as $property) {
92
            if (!$this->annotationReader->getPropertyAnnotation(
93
                $property,
94
                GeneratedValue::class
95
            )) {
96
                $properties[] = [
97
                    'name' => $property->getName(),
98
                    'annotation' => new $annotationClass(),
99
                ];
100
            }
101
        }
102
103
        return $properties;
104
    }
105
106
}
107