1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace DoctrineORMModule\Form\Annotation; |
6
|
|
|
|
7
|
|
|
use ArrayObject; |
8
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata; |
9
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
10
|
|
|
use DoctrineModule\Form\Element; |
11
|
|
|
use Laminas\EventManager\EventManagerInterface; |
12
|
|
|
use Laminas\Form\Annotation\AnnotationBuilder as LaminasAnnotationBuilder; |
13
|
|
|
use function get_class; |
14
|
|
|
use function in_array; |
15
|
|
|
use function is_object; |
16
|
|
|
|
17
|
|
|
class AnnotationBuilder extends LaminasAnnotationBuilder |
18
|
|
|
{ |
19
|
|
|
public const EVENT_CONFIGURE_FIELD = 'configureField'; |
20
|
|
|
public const EVENT_CONFIGURE_ASSOCIATION = 'configureAssociation'; |
21
|
|
|
public const EVENT_EXCLUDE_FIELD = 'excludeField'; |
22
|
|
|
public const EVENT_EXCLUDE_ASSOCIATION = 'excludeAssociation'; |
23
|
|
|
|
24
|
|
|
/** @var ObjectManager */ |
25
|
|
|
protected $objectManager; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Constructor. Ensures ObjectManager is present. |
29
|
|
|
*/ |
30
|
|
|
public function __construct(ObjectManager $objectManager) |
31
|
|
|
{ |
32
|
|
|
$this->objectManager = $objectManager; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritDoc} |
37
|
|
|
*/ |
38
|
|
|
public function setEventManager(EventManagerInterface $events) |
39
|
|
|
{ |
40
|
|
|
parent::setEventManager($events); |
41
|
|
|
|
42
|
|
|
(new ElementAnnotationsListener($this->objectManager))->attach($this->getEventManager()); |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Overrides the base getFormSpecification() to additionally iterate through each |
49
|
|
|
* field/association in the metadata and trigger the associated event. |
50
|
|
|
* |
51
|
|
|
* This allows building of a form from metadata instead of requiring annotations. |
52
|
|
|
* Annotations are still allowed through the ElementAnnotationsListener. |
53
|
|
|
* |
54
|
|
|
* {@inheritDoc} |
55
|
|
|
*/ |
56
|
|
|
public function getFormSpecification($entity) |
57
|
|
|
{ |
58
|
|
|
$formSpec = parent::getFormSpecification($entity); |
59
|
|
|
$metadata = $this->objectManager->getClassMetadata(is_object($entity) ? get_class($entity) : $entity); |
60
|
|
|
$inputFilter = $formSpec['input_filter']; |
61
|
|
|
|
62
|
|
|
$formElements = [ |
63
|
|
|
Element\ObjectSelect::class, |
64
|
|
|
Element\ObjectMultiCheckbox::class, |
65
|
|
|
Element\ObjectRadio::class, |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
foreach ($formSpec['elements'] as $key => $elementSpec) { |
69
|
|
|
$name = $elementSpec['spec']['name'] ?? null; |
70
|
|
|
$isFormElement = (isset($elementSpec['spec']['type']) && |
71
|
|
|
in_array($elementSpec['spec']['type'], $formElements)); |
72
|
|
|
|
73
|
|
|
if (! $name) { |
74
|
|
|
continue; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (! isset($inputFilter[$name])) { |
78
|
|
|
$inputFilter[$name] = new ArrayObject(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$params = [ |
82
|
|
|
'metadata' => $metadata, |
83
|
|
|
'name' => $name, |
84
|
|
|
'elementSpec' => $elementSpec, |
85
|
|
|
'inputSpec' => $inputFilter[$name], |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
if ($this->checkForExcludeElementFromMetadata($metadata, $name)) { |
|
|
|
|
89
|
|
|
$elementSpec = $formSpec['elements']; |
90
|
|
|
unset($elementSpec[$key]); |
91
|
|
|
$formSpec['elements'] = $elementSpec; |
92
|
|
|
|
93
|
|
|
if (isset($inputFilter[$name])) { |
94
|
|
|
unset($inputFilter[$name]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$formSpec['input_filter'] = $inputFilter; |
98
|
|
|
continue; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ($metadata->hasField($name) || (! $metadata->hasAssociation($name) && $isFormElement)) { |
102
|
|
|
$this->getEventManager()->trigger(self::EVENT_CONFIGURE_FIELD, $this, $params); |
103
|
|
|
} elseif ($metadata->hasAssociation($name)) { |
104
|
|
|
$this->getEventManager()->trigger(self::EVENT_CONFIGURE_ASSOCIATION, $this, $params); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$formSpec['options'] = ['prefer_form_input_filter' => true]; |
109
|
|
|
|
110
|
|
|
return $formSpec; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected function checkForExcludeElementFromMetadata(ClassMetadata $metadata, string $name) : bool |
114
|
|
|
{ |
115
|
|
|
$params = ['metadata' => $metadata, 'name' => $name]; |
116
|
|
|
$result = false; |
117
|
|
|
|
118
|
|
|
if ($metadata->hasField($name)) { |
119
|
|
|
$result = $this->getEventManager()->trigger(self::EVENT_EXCLUDE_FIELD, $this, $params); |
120
|
|
|
} elseif ($metadata->hasAssociation($name)) { |
121
|
|
|
$result = $this->getEventManager()->trigger(self::EVENT_EXCLUDE_ASSOCIATION, $this, $params); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if ($result) { |
125
|
|
|
$result = (bool) $result->last(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $result; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.