Passed
Branch master (046a17)
by Mathieu
02:22
created

ExportReader::getFormats()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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