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

ExportReader::getFields()   B

Complexity

Conditions 7
Paths 28

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 31
rs 8.8333
c 0
b 0
f 0
cc 7
nc 28
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Neimheadh\SonataAnnotationBundle\Reader;
6
7
use Neimheadh\SonataAnnotationBundle\Annotation\Sonata\Admin;
8
use Neimheadh\SonataAnnotationBundle\Annotation\Sonata\ExportAssociationField;
9
use Neimheadh\SonataAnnotationBundle\Annotation\Sonata\ExportField;
10
use Neimheadh\SonataAnnotationBundle\Annotation\Sonata\ExportFormats;
11
use ReflectionClass;
12
use ReflectionMethod;
13
use ReflectionProperty;
14
15
/**
16
 * Export configuration reader.
17
 *
18
 * @author Marko Kunic <[email protected]>
19
 * @author Mathieu Wambre <[email protected]>
20
 */
21
final class ExportReader extends AbstractReader
22
{
23
24
    /**
25
     * Get exported fields.
26
     *
27
     * @param ReflectionClass $class Entity class.
28
     *
29
     * @return array<string, string> Label => property/method name list.
30
     */
31
    public function getFields(ReflectionClass $class): array
32
    {
33
        /** @var Admin|null $admin */
34
        $admin = $this->getClassAnnotation($class, Admin::class);
35
36
        $fields = $admin ? $admin->getExportFields() : [];
37
        $allFields = [];
38
        $properties = array_merge(
39
            $class->getProperties(),
40
            $class->getMethods()
41
        );
42
43
        foreach ($properties as $property) {
44
            $annotations = $property instanceof ReflectionProperty
45
                ? $this->getPropertyAnnotations($property)
46
                : $this->getMethodAnnotations($property);
47
48
            $this->stackExportProperty(
49
                $property,
50
                new ExportField(),
51
                $allFields
52
            );
53
54
            foreach ($annotations as $annotation) {
55
                if ($annotation instanceof ExportField) {
56
                    $this->stackExportProperty($property, $annotation, $fields);
57
                }
58
            }
59
        }
60
61
        return empty($fields) ? $allFields : $fields;
62
    }
63
64
    /**
65
     * Get list of extract formats.
66
     *
67
     * @param ReflectionClass $class Entity class.
68
     *
69
     * @return array
70
     */
71
    public function getFormats(ReflectionClass $class): array
72
    {
73
        /** @var ExportFormats|null $annotation */
74
        if ($annotation = $this->getClassAnnotation(
75
            $class,
76
            ExportFormats::class
77
        )) {
78
            return $annotation->formats;
79
        }
80
81
        return [];
82
    }
83
84
    /**
85
     * Stack property or method to exported fields.
86
     *
87
     * @param ReflectionProperty|ReflectionMethod $property   Stacked property.
88
     * @param object                              $annotation Annotation.
89
     * @param array                               $fields     Fields stack.
90
     *
91
     * @return void
92
     */
93
    private function stackExportProperty(
94
        $property,
95
        object $annotation,
96
        array &$fields
97
    ): void {
98
        if ($annotation instanceof ExportAssociationField) {
99
            $fieldName = $property->getName()
100
                . '.'
101
                . $annotation->field;
102
103
            $fields[$annotation->label ?: $fieldName] = $fieldName;
104
105
            return;
106
        }
107
108
        $label = $annotation->label ?: $property->getName();
109
        $fields[$label] = $property->getName();
110
    }
111
112
}
113