1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Neimheadh\SonataAnnotationBundle\Reader; |
6
|
|
|
|
7
|
|
|
use Neimheadh\SonataAnnotationBundle\Annotation\Sonata\ExportAssociationField; |
8
|
|
|
use Neimheadh\SonataAnnotationBundle\Annotation\Sonata\ExportField; |
9
|
|
|
use Neimheadh\SonataAnnotationBundle\Annotation\Sonata\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
|
|
|
$allFields = []; |
34
|
|
|
|
35
|
|
|
foreach ($class->getProperties() as $property) { |
36
|
|
|
foreach ($this->getPropertyAnnotations($property) as $annotation) { |
37
|
|
|
$this->stackExportProperty( |
38
|
|
|
$property, |
39
|
|
|
new ExportField(), |
40
|
|
|
$allFields |
41
|
|
|
); |
42
|
|
|
if ($annotation instanceof ExportField) { |
43
|
|
|
$this->stackExportProperty($property, $annotation, $fields); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
foreach ($class->getMethods() as $method) { |
49
|
|
|
$this->stackExportProperty( |
50
|
|
|
$property, |
|
|
|
|
51
|
|
|
new ExportField(), |
52
|
|
|
$allFields |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
if ($annotation = $this->getMethodAnnotation( |
56
|
|
|
$method, |
57
|
|
|
ExportField::class |
58
|
|
|
)) { |
59
|
|
|
$this->stackExportProperty($method, $annotation, $fields); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return empty($fields) ? $allFields : $fields; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get list of extract formats. |
68
|
|
|
* |
69
|
|
|
* @param ReflectionClass $class Entity class. |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public function getFormats(ReflectionClass $class): array |
74
|
|
|
{ |
75
|
|
|
/** @var ExportFormats|null $annotation */ |
76
|
|
|
if ($annotation = $this->getClassAnnotation( |
77
|
|
|
$class, |
78
|
|
|
ExportFormats::class |
79
|
|
|
)) { |
80
|
|
|
return $annotation->formats; |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return []; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Stack property or method to exported fields. |
88
|
|
|
* |
89
|
|
|
* @param ReflectionProperty|ReflectionMethod $property Stacked property. |
90
|
|
|
* @param object $annotation Annotation. |
91
|
|
|
* @param array $fields Fields stack. |
92
|
|
|
* |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
|
|
private function stackExportProperty( |
96
|
|
|
$property, |
97
|
|
|
object $annotation, |
98
|
|
|
array &$fields |
99
|
|
|
): void { |
100
|
|
|
if ($annotation instanceof ExportAssociationField) { |
101
|
|
|
$fieldName = $property->getName() |
102
|
|
|
. '.' |
103
|
|
|
. $annotation->field; |
104
|
|
|
|
105
|
|
|
$fields[$annotation->label ?: $fieldName] = $fieldName; |
106
|
|
|
|
107
|
|
|
return; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$label = $annotation->label ?: $property->getName(); |
111
|
|
|
$fields[$label] = $property->getName(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|