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