|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace KunicMarko\SonataAnnotationBundle\Reader; |
|
6
|
|
|
|
|
7
|
|
|
use KunicMarko\SonataAnnotationBundle\Annotation\DatagridAssociationField; |
|
8
|
|
|
use KunicMarko\SonataAnnotationBundle\Annotation\DatagridField; |
|
9
|
|
|
use Sonata\AdminBundle\Datagrid\DatagridMapper; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @author Marko Kunic <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
final class DatagridReader |
|
15
|
|
|
{ |
|
16
|
|
|
use AnnotationReaderTrait; |
|
17
|
|
|
|
|
18
|
|
|
public function configureFields(\ReflectionClass $class, DatagridMapper $datagridMapper): void |
|
19
|
|
|
{ |
|
20
|
|
|
$propertiesWithPosition = []; |
|
21
|
|
|
$propertiesWithoutPosition = []; |
|
22
|
|
|
|
|
23
|
|
|
foreach ($class->getProperties() as $property) { |
|
24
|
|
|
foreach ($this->getPropertyAnnotations($property) as $annotation) { |
|
25
|
|
|
if (!$annotation instanceof DatagridField && !$annotation instanceof DatagridAssociationField) { |
|
26
|
|
|
continue; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
// the name property changes for DatagridAssociationField |
|
30
|
|
|
$name = $property->getName(); |
|
31
|
|
|
if ($annotation instanceof DatagridAssociationField) { |
|
32
|
|
|
$name .= '.'.$annotation->getField(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
if (!$annotation->hasPosition()) { |
|
36
|
|
|
$propertiesWithoutPosition[] = [ |
|
37
|
|
|
'name' => $name, |
|
38
|
|
|
'annotation' => $annotation, |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
|
|
continue; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if (\array_key_exists($annotation->position, $propertiesWithPosition)) { |
|
45
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
46
|
|
|
'Position "%s" is already in use by "%s", try setting a different position for "%s".', |
|
47
|
|
|
$annotation->position, |
|
48
|
|
|
$propertiesWithPosition[$annotation->position]['name'], |
|
49
|
|
|
$property->getName() |
|
50
|
|
|
)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$propertiesWithPosition[$annotation->position] = [ |
|
54
|
|
|
'name' => $name, |
|
55
|
|
|
'annotation' => $annotation, |
|
56
|
|
|
]; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
\ksort($propertiesWithPosition); |
|
61
|
|
|
|
|
62
|
|
|
$properties = \array_merge($propertiesWithPosition, $propertiesWithoutPosition); |
|
63
|
|
|
|
|
64
|
|
|
foreach ($properties as $property) { |
|
65
|
|
|
$datagridMapper->add( |
|
66
|
|
|
$property['name'], ...$property['annotation']->getSettings() |
|
|
|
|
|
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|