Completed
Pull Request — feature/stable-doctrine-module (#518)
by
unknown
23:58 queued 21:46
created

checkForExcludeElementFromMetadata()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
ccs 0
cts 12
cp 0
rs 9.2
cc 4
eloc 10
nc 6
nop 2
crap 20
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
    public function __construct(ObjectManager $objectManager)
48
    {
49
        $this->objectManager = $objectManager;
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55
    public function setEventManager(EventManagerInterface $events)
56
    {
57
        parent::setEventManager($events);
58
59
        $this->getEventManager()->attach(new ElementAnnotationsListener($this->objectManager));
0 ignored issues
show
Documentation introduced by
new \DoctrineORMModule\F...r($this->objectManager) is of type object<DoctrineORMModule...entAnnotationsListener>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60
61
        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
    public function getFormSpecification($entity)
74
    {
75
        $formSpec     = parent::getFormSpecification($entity);
76
        $metadata     = $this->objectManager->getClassMetadata(is_object($entity) ? get_class($entity) : $entity);
77
        $inputFilter  = $formSpec['input_filter'];
78
79
        $formElements = array(
80
            'DoctrineModule\Form\Element\ObjectSelect',
81
            'DoctrineModule\Form\Element\ObjectMultiCheckbox',
82
            'DoctrineModule\Form\Element\ObjectRadio',
83
        );
84
85
        foreach ($formSpec['elements'] as $key => $elementSpec) {
86
            $name          = isset($elementSpec['spec']['name']) ? $elementSpec['spec']['name'] : null;
87
            $isFormElement = (isset($elementSpec['spec']['type']) &&
88
                              in_array($elementSpec['spec']['type'], $formElements));
89
90
            if (!$name) {
91
                continue;
92
            }
93
94
            if (!isset($inputFilter[$name])) {
95
                $inputFilter[$name] = new \ArrayObject();
96
            }
97
98
            $params = array(
99
                'metadata'    => $metadata,
100
                'name'        => $name,
101
                'elementSpec' => $elementSpec,
102
                'inputSpec'   => $inputFilter[$name]
103
            );
104
105
            if ($this->checkForExcludeElementFromMetadata($metadata, $name)) {
106
                $elementSpec = $formSpec['elements'];
107
                unset($elementSpec[$key]);
108
                $formSpec['elements'] = $elementSpec;
109
110
                if (isset($inputFilter[$name])) {
111
                    unset($inputFilter[$name]);
112
                }
113
114
                $formSpec['input_filter'] = $inputFilter;
115
                continue;
116
            }
117
118
            if ($metadata->hasField($name) || (!$metadata->hasAssociation($name) && $isFormElement)) {
119
                $this->getEventManager()->trigger(static::EVENT_CONFIGURE_FIELD, $this, $params);
120
            } elseif ($metadata->hasAssociation($name)) {
121
                $this->getEventManager()->trigger(static::EVENT_CONFIGURE_ASSOCIATION, $this, $params);
122
            }
123
        }
124
125
        $formSpec['options'] = array('prefer_form_input_filter' => true);
126
127
        return $formSpec;
128
    }
129
130
    /**
131
     * @param ClassMetadata $metadata
132
     * @param $name
133
     * @return bool
134
     */
135
    protected function checkForExcludeElementFromMetadata(ClassMetadata $metadata, $name)
136
    {
137
        $params = array('metadata' => $metadata, 'name' => $name);
138
        $result = false;
139
140
        if ($metadata->hasField($name)) {
141
            $result = $this->getEventManager()->trigger(static::EVENT_EXCLUDE_FIELD, $this, $params);
142
        } elseif ($metadata->hasAssociation($name)) {
143
            $result = $this->getEventManager()->trigger(static::EVENT_EXCLUDE_ASSOCIATION, $this, $params);
144
        }
145
146
        if ($result) {
147
            $result = (bool)$result->last();
148
        }
149
150
        return $result;
151
    }
152
}
153