Completed
Pull Request — master (#465)
by
unknown
19:28
created

AnnotationBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace DoctrineORMModule\Form\Annotation;
21
22
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
23
use Doctrine\Common\Persistence\ObjectManager;
24
use Zend\EventManager\EventManagerInterface;
25
use Zend\Form\Annotation\AnnotationBuilder as ZendAnnotationBuilder;
26
27
/**
28
 * @author Kyle Spraggs <[email protected]>
29
 */
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)
56
    {
57 4
        parent::setEventManager($events);
58
59 4
        $this->getEventManager()->attach(
60 4
            '*',
61 4
            function () use ($events) {
62 4
                $listener = new ElementAnnotationsListener($this->objectManager);
63 4
                $listener->attach($events);
64 4
            }
65 4
        );
66
67 4
        return $this;
68
    }
69
70
    /**
71
     * Overrides the base getFormSpecification() to additionally iterate through each
72
     * field/association in the metadata and trigger the associated event.
73
     *
74
     * This allows building of a form from metadata instead of requiring annotations.
75
     * Annotations are still allowed through the ElementAnnotationsListener.
76
     *
77
     * {@inheritDoc}
78
     */
79 6
    public function getFormSpecification($entity)
80
    {
81 6
        $formSpec     = parent::getFormSpecification($entity);
82 6
        $metadata     = $this->objectManager->getClassMetadata(is_object($entity) ? get_class($entity) : $entity);
83 6
        $inputFilter  = $formSpec['input_filter'];
84
85
        $formElements = array(
86 6
            'DoctrineModule\Form\Element\ObjectSelect',
87 6
            'DoctrineModule\Form\Element\ObjectMultiCheckbox',
88 6
            'DoctrineModule\Form\Element\ObjectRadio',
89 6
        );
90
91 6
        foreach ($formSpec['elements'] as $key => $elementSpec) {
92 6
            $name          = isset($elementSpec['spec']['name']) ? $elementSpec['spec']['name'] : null;
93 6
            $isFormElement = (isset($elementSpec['spec']['type']) &&
94 6
                              in_array($elementSpec['spec']['type'], $formElements));
95
96 6
            if (!$name) {
97
                continue;
98
            }
99
100 6
            if (!isset($inputFilter[$name])) {
101 6
                $inputFilter[$name] = new \ArrayObject();
102 6
            }
103
104
            $params = array(
105 6
                'metadata'    => $metadata,
106 6
                'name'        => $name,
107 6
                'elementSpec' => $elementSpec,
108 6
                'inputSpec'   => $inputFilter[$name]
109 6
            );
110
111 6
            if ($this->checkForExcludeElementFromMetadata($metadata, $name)) {
112
                $elementSpec = $formSpec['elements'];
113
                unset($elementSpec[$key]);
114
                $formSpec['elements'] = $elementSpec;
115
116
                if (isset($inputFilter[$name])) {
117
                    unset($inputFilter[$name]);
118
                }
119
120
                $formSpec['input_filter'] = $inputFilter;
121
                continue;
122
            }
123
124 6
            if ($metadata->hasField($name) || (!$metadata->hasAssociation($name) && $isFormElement)) {
125 6
                $this->getEventManager()->trigger(static::EVENT_CONFIGURE_FIELD, $this, $params);
126 6
            } elseif ($metadata->hasAssociation($name)) {
127 4
                $this->getEventManager()->trigger(static::EVENT_CONFIGURE_ASSOCIATION, $this, $params);
128 4
            }
129 6
        }
130
131 6
        $formSpec['options'] = array('prefer_form_input_filter' => true);
132
133 6
        return $formSpec;
134
    }
135
136
    /**
137
     * @param ClassMetadata $metadata
138
     * @param $name
139
     * @return bool
140
     */
141 4
    protected function checkForExcludeElementFromMetadata(ClassMetadata $metadata, $name)
142
    {
143 4
        $params = array('metadata' => $metadata, 'name' => $name);
144 4
        $result = false;
145
146 4
        if ($metadata->hasField($name)) {
147 4
            $result = $this->getEventManager()->trigger(static::EVENT_EXCLUDE_FIELD, $this, $params);
148 4
        } elseif ($metadata->hasAssociation($name)) {
149 4
            $result = $this->getEventManager()->trigger(static::EVENT_EXCLUDE_ASSOCIATION, $this, $params);
150 4
        }
151
152 4
        if ($result) {
153 4
            $result = (bool)$result->last();
154 4
        }
155
156 4
        return $result;
157
    }
158
}
159