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

ExportReader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getFormats() 0 7 2
A getFields() 0 11 3
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