1 | <?php |
||
30 | class AnnotationBuilder extends ZendAnnotationBuilder |
||
31 | { |
||
32 | const EVENT_CONFIGURE_FIELD = 'configureField'; |
||
33 | const EVENT_CONFIGURE_ASSOCIATION = 'configureAssociation'; |
||
34 | const EVENT_EXCLUDE_FIELD = 'excludeField'; |
||
35 | const EVENT_EXCLUDE_ASSOCIATION = 'excludeAssociation'; |
||
36 | |||
37 | /** |
||
38 | * @var \Doctrine\Common\Persistence\ObjectManager |
||
39 | */ |
||
40 | protected $objectManager; |
||
41 | |||
42 | /** |
||
43 | * Constructor. Ensures ObjectManager is present. |
||
44 | * |
||
45 | * @param \Doctrine\Common\Persistence\ObjectManager $objectManager |
||
46 | */ |
||
47 | 4 | public function __construct(ObjectManager $objectManager) |
|
48 | { |
||
49 | 4 | $this->objectManager = $objectManager; |
|
50 | 4 | } |
|
51 | |||
52 | /** |
||
53 | * {@inheritDoc} |
||
54 | */ |
||
55 | 4 | public function setEventManager(EventManagerInterface $events) |
|
63 | |||
64 | /** |
||
65 | * Overrides the base getFormSpecification() to additionally iterate through each |
||
66 | * field/association in the metadata and trigger the associated event. |
||
67 | * |
||
68 | * This allows building of a form from metadata instead of requiring annotations. |
||
69 | * Annotations are still allowed through the ElementAnnotationsListener. |
||
70 | * |
||
71 | * {@inheritDoc} |
||
72 | */ |
||
73 | 6 | public function getFormSpecification($entity) |
|
74 | { |
||
75 | 6 | $formSpec = parent::getFormSpecification($entity); |
|
76 | 6 | $metadata = $this->objectManager->getClassMetadata(is_object($entity) ? get_class($entity) : $entity); |
|
77 | 6 | $inputFilter = $formSpec['input_filter']; |
|
78 | |||
79 | $formElements = array( |
||
80 | 6 | 'DoctrineModule\Form\Element\ObjectSelect', |
|
81 | 6 | 'DoctrineModule\Form\Element\ObjectMultiCheckbox', |
|
82 | 6 | 'DoctrineModule\Form\Element\ObjectRadio', |
|
83 | 6 | ); |
|
84 | |||
85 | 6 | foreach ($formSpec['elements'] as $key => $elementSpec) { |
|
86 | 6 | $name = isset($elementSpec['spec']['name']) ? $elementSpec['spec']['name'] : null; |
|
87 | 6 | $isFormElement = (isset($elementSpec['spec']['type']) && |
|
88 | 6 | in_array($elementSpec['spec']['type'], $formElements)); |
|
89 | |||
90 | 6 | if (!$name) { |
|
91 | continue; |
||
92 | } |
||
93 | |||
94 | 6 | if (!isset($inputFilter[$name])) { |
|
95 | 6 | $inputFilter[$name] = new \ArrayObject(); |
|
96 | 6 | } |
|
97 | |||
98 | $params = array( |
||
99 | 6 | 'metadata' => $metadata, |
|
100 | 6 | 'name' => $name, |
|
101 | 6 | 'elementSpec' => $elementSpec, |
|
102 | 6 | 'inputSpec' => $inputFilter[$name] |
|
103 | 6 | ); |
|
104 | |||
105 | 6 | if ($this->checkForExcludeElementFromMetadata($metadata, $name)) { |
|
106 | 6 | $elementSpec = $formSpec['elements']; |
|
107 | 6 | unset($elementSpec[$key]); |
|
108 | 6 | $formSpec['elements'] = $elementSpec; |
|
109 | |||
110 | 6 | if (isset($inputFilter[$name])) { |
|
111 | 6 | unset($inputFilter[$name]); |
|
112 | 6 | } |
|
113 | |||
114 | 6 | $formSpec['input_filter'] = $inputFilter; |
|
115 | 6 | continue; |
|
116 | } |
||
117 | |||
118 | 4 | if ($metadata->hasField($name) || (!$metadata->hasAssociation($name) && $isFormElement)) { |
|
119 | 4 | $this->getEventManager()->trigger(static::EVENT_CONFIGURE_FIELD, $this, $params); |
|
120 | 4 | } elseif ($metadata->hasAssociation($name)) { |
|
121 | 4 | $this->getEventManager()->trigger(static::EVENT_CONFIGURE_ASSOCIATION, $this, $params); |
|
122 | 4 | } |
|
123 | 6 | } |
|
124 | |||
125 | 6 | $formSpec['options'] = array('prefer_form_input_filter' => true); |
|
126 | |||
127 | 6 | return $formSpec; |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * @param ClassMetadata $metadata |
||
132 | * @param $name |
||
133 | * @return bool |
||
134 | */ |
||
135 | 4 | protected function checkForExcludeElementFromMetadata(ClassMetadata $metadata, $name) |
|
152 | } |
||
153 |