Completed
Push — master ( c96bfe...df0c48 )
by Marko
02:29
created

ExportReader::getFields()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace KunicMarko\SonataAnnotationBundle\Reader;
4
5
use Doctrine\Common\Annotations\Reader;
6
use KunicMarko\SonataAnnotationBundle\Annotation\ExportField;
7
use KunicMarko\SonataAnnotationBundle\Annotation\ExportFormats;
8
9
/**
10
 * @author Marko Kunic <[email protected]>
11
 */
12
class ExportReader
13
{
14
    protected $annotationReader;
15
16
    public function __construct(Reader $annotationReader)
17
    {
18
        $this->annotationReader = $annotationReader;
19
    }
20
21
    public function getFields(\ReflectionClass $entity): array
22
    {
23
        $properties = [];
24
25
        foreach ($entity->getProperties() as $property) {
26
            if ($annotation = $this->annotationReader->getPropertyAnnotation($property, ExportField::class)) {
27
                $properties[$annotation->label ?? $property->getName()] = $property->getName();
28
            }
29
        }
30
31
        return $properties;
32
    }
33
34
    public function getFormats(\ReflectionClass $entity): array
35
    {
36
        if ($annotation = $this->annotationReader->getClassAnnotation($entity, ExportFormats::class)) {
37
            return $annotation->formats;
38
        }
39
40
        return [];
41
    }
42
}
43