Completed
Pull Request — master (#508)
by Oliver
03:04
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 0
Metric Value
c 0
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 DoctrineModule\Form\Element;
25
use Zend\EventManager\EventManagerInterface;
26
use Zend\Form\Annotation\AnnotationBuilder as ZendAnnotationBuilder;
27
28
/**
29
 * @author Kyle Spraggs <[email protected]>
30
 */
31
class AnnotationBuilder extends ZendAnnotationBuilder
32
{
33
    const EVENT_CONFIGURE_FIELD       = 'configureField';
34
    const EVENT_CONFIGURE_ASSOCIATION = 'configureAssociation';
35
    const EVENT_EXCLUDE_FIELD         = 'excludeField';
36
    const EVENT_EXCLUDE_ASSOCIATION   = 'excludeAssociation';
37
38
    /**
39
     * @var \Doctrine\Common\Persistence\ObjectManager
40
     */
41
    protected $objectManager;
42
43
    /**
44
     * Constructor. Ensures ObjectManager is present.
45
     *
46
     * @param \Doctrine\Common\Persistence\ObjectManager $objectManager
47
     */
48 4
    public function __construct(ObjectManager $objectManager)
49
    {
50 4
        $this->objectManager = $objectManager;
51 4
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56 4
    public function setEventManager(EventManagerInterface $events)
57
    {
58 4
        parent::setEventManager($events);
59
60 4
        (new ElementAnnotationsListener($this->objectManager))->attach($this->getEventManager());
61
62 4
        return $this;
63
    }
64
65
    /**
66
     * Overrides the base getFormSpecification() to additionally iterate through each
67
     * field/association in the metadata and trigger the associated event.
68
     *
69
     * This allows building of a form from metadata instead of requiring annotations.
70
     * Annotations are still allowed through the ElementAnnotationsListener.
71
     *
72
     * {@inheritDoc}
73
     */
74 6
    public function getFormSpecification($entity)
75
    {
76 6
        $formSpec     = parent::getFormSpecification($entity);
77 6
        $metadata     = $this->objectManager->getClassMetadata(is_object($entity) ? get_class($entity) : $entity);
78 6
        $inputFilter  = $formSpec['input_filter'];
79
80
        $formElements = [
81 6
            Element\ObjectSelect::class,
82 6
            Element\ObjectMultiCheckbox::class,
83 6
            Element\ObjectRadio::class,
84 6
        ];
85
86 6
        foreach ($formSpec['elements'] as $key => $elementSpec) {
87 6
            $name          = isset($elementSpec['spec']['name']) ? $elementSpec['spec']['name'] : null;
88 6
            $isFormElement = (isset($elementSpec['spec']['type']) &&
89 6
                              in_array($elementSpec['spec']['type'], $formElements));
90
91 6
            if (! $name) {
92
                continue;
93
            }
94
95 6
            if (! isset($inputFilter[$name])) {
96 6
                $inputFilter[$name] = new \ArrayObject();
97 6
            }
98
99
            $params = [
100 6
                'metadata'    => $metadata,
101 6
                'name'        => $name,
102 6
                'elementSpec' => $elementSpec,
103 6
                'inputSpec'   => $inputFilter[$name],
104 6
            ];
105
106 6
            if ($this->checkForExcludeElementFromMetadata($metadata, $name)) {
107 6
                $elementSpec = $formSpec['elements'];
108 6
                unset($elementSpec[$key]);
109 6
                $formSpec['elements'] = $elementSpec;
110
111 6
                if (isset($inputFilter[$name])) {
112 6
                    unset($inputFilter[$name]);
113 6
                }
114
115 6
                $formSpec['input_filter'] = $inputFilter;
116 6
                continue;
117
            }
118
119 4
            if ($metadata->hasField($name) || (! $metadata->hasAssociation($name) && $isFormElement)) {
120 4
                $this->getEventManager()->trigger(static::EVENT_CONFIGURE_FIELD, $this, $params);
121 4
            } elseif ($metadata->hasAssociation($name)) {
122 4
                $this->getEventManager()->trigger(static::EVENT_CONFIGURE_ASSOCIATION, $this, $params);
123 4
            }
124 6
        }
125
126 6
        $formSpec['options'] = ['prefer_form_input_filter' => true];
127
128 6
        return $formSpec;
129 1
    }
130
131
    /**
132
     * @param ClassMetadata $metadata
133
     * @param $name
134
     * @return bool
135
     */
136 4
    protected function checkForExcludeElementFromMetadata(ClassMetadata $metadata, $name)
137
    {
138 4
        $params = ['metadata' => $metadata, 'name' => $name];
139 4
        $result = false;
140
141 4
        if ($metadata->hasField($name)) {
142 4
            $result = $this->getEventManager()->trigger(static::EVENT_EXCLUDE_FIELD, $this, $params);
143 4
        } elseif ($metadata->hasAssociation($name)) {
144 4
            $result = $this->getEventManager()->trigger(static::EVENT_EXCLUDE_ASSOCIATION, $this, $params);
145 4
        }
146
147 4
        if ($result) {
148 4
            $result = (bool)$result->last();
149 4
        }
150
151 4
        return $result;
152
    }
153
}
154