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