Completed
Push — 1.x ( d1a99d...be31ef )
by Marko
02:10
created

ExportReader::getFields()   C

Complexity

Conditions 7
Paths 15

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
c 0
b 0
f 0
rs 6.7272
cc 7
eloc 13
nc 15
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace KunicMarko\SonataAnnotationBundle\Reader;
6
7
use KunicMarko\SonataAnnotationBundle\Annotation\ExportAssociationField;
8
use KunicMarko\SonataAnnotationBundle\Annotation\ExportField;
9
use KunicMarko\SonataAnnotationBundle\Annotation\ExportFormats;
10
11
/**
12
 * @author Marko Kunic <[email protected]>
13
 */
14
class ExportReader
15
{
16
    use AnnotationReaderTrait;
17
18
    public function getFields(\ReflectionClass $class): array
19
    {
20
        $fields = [];
21
22
        foreach ($class->getProperties() as $property) {
23
            foreach ($this->getPropertyAnnotations($property) as $annotation) {
24
                if ($annotation instanceof ExportAssociationField) {
25
                    $fieldName = $property->getName() . '.' . $annotation->field;
26
27
                    $fields[$annotation->label ?? $fieldName] = $fieldName;
28
                    continue;
29
                }
30
31
                if ($annotation instanceof ExportField) {
32
                    $fields[$annotation->label ?? $property->getName()] = $property->getName();
33
                }
34
            }
35
        }
36
37
        foreach ($class->getMethods() as $method) {
38
            if ($annotation = $this->getMethodAnnotation($method, ExportField::class)) {
39
                $fields[$annotation->label ?? $method->getName()] = $method->getName();
40
            }
41
        }
42
43
        return $fields;
44
    }
45
46
    public function getFormats(\ReflectionClass $class): array
47
    {
48
        if ($annotation = $this->getClassAnnotation($class, ExportFormats::class)) {
49
            return $annotation->formats;
50
        }
51
52
        return [];
53
    }
54
}
55