Completed
Push — master ( 50808e...33b518 )
by Gianluca
05:07
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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
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
        (new ElementAnnotationsListener($this->objectManager))->attach($this->getEventManager());
60
61 4
        return $this;
62
    }
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)
136
    {
137 4
        $params = array('metadata' => $metadata, 'name' => $name);
138 4
        $result = false;
139
140 4
        if ($metadata->hasField($name)) {
141 4
            $result = $this->getEventManager()->trigger(static::EVENT_EXCLUDE_FIELD, $this, $params);
142 4
        } elseif ($metadata->hasAssociation($name)) {
143 4
            $result = $this->getEventManager()->trigger(static::EVENT_EXCLUDE_ASSOCIATION, $this, $params);
144 4
        }
145
146 4
        if ($result) {
147 4
            $result = (bool)$result->last();
148 4
        }
149
150 4
        return $result;
151
    }
152
}
153