Completed
Pull Request — 5.0 (#2163)
by Kevin
13:16
created

AdminBundle/Helper/FormWidgets/FormWidget.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminBundle\Helper\FormWidgets;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\AdminBundle\Helper\FormHelper;
7
use Kunstmaan\AdminBundle\Helper\FormWidgets\Tabs\TabInterface;
8
use Symfony\Component\Form\AbstractType;
9
use Symfony\Component\Form\FormBuilderInterface;
10
use Symfony\Component\Form\FormView;
11
use Symfony\Component\HttpFoundation\Request;
12
13
/**
14
 * The default tab implementation
15
 */
16
class FormWidget implements FormWidgetInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $identifier;
22
23
    /**
24
     * @var AbstractType[]
25
     */
26
    protected $types;
27
28
    /**
29
     * @var array
30
     */
31
    protected $data;
32
33
    /**
34
     * @var array
35
     */
36
    protected $options;
37
38
    /**
39
     * @var FormHelper
40
     */
41
    private $formHelper = null;
42
43
    /**
44
     * @var string
45
     */
46
    protected $template;
47
48
    /**
49
     * @param array $types The types
50
     * @param array $data  The data attached to the types
51
     */
52
    public function __construct(array $types = array(), array $data = array(), array $options = array())
53
    {
54
        $this->types = $types;
55
        $this->data = $data;
56
        $this->options = $options;
57
58
        $this->setTemplate('KunstmaanAdminBundle:FormWidgets\FormWidget:widget.html.twig');
59
    }
60
61
    /**
62
     * @param FormBuilderInterface $builder The form builder
63
     */
64
    public function buildForm(FormBuilderInterface $builder)
65
    {
66
        $data = $builder->getData();
67
68
        foreach ($this->types as $name => $type) {
69
            $builder->add($name, $type, $this->options[$name]);
0 ignored issues
show
$type is of type object<Symfony\Component\Form\AbstractType>, but the function expects a string|null.

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...
70
            $data[$name] = $this->data[$name];
71
        }
72
73
        $builder->setData($data);
74
    }
75
76
    /**
77
     * @param Request $request
78
     */
79
    public function bindRequest(Request $request)
80
    {
81
82
    }
83
84
    /**
85
     * @param EntityManager $em
86
     */
87
    public function persist(EntityManager $em)
88
    {
89
        foreach ($this->data as $item) {
90
            $em->persist($item);
91
        }
92
    }
93
94
    /**
95
     * @param FormView $formView
96
     *
97
     * @return array
98
     */
99
    public function getFormErrors(FormView $formView)
100
    {
101
        $formViews = array();
102
        foreach ($this->types as $name => $type) {
103
            $formViews[] = $formView[$name];
104
        }
105
106
        $formHelper = $this->getFormHelper();
107
108
        return $formHelper->getRecursiveErrorMessages($formViews);
109
    }
110
111
    /**
112
     * @return FormHelper
113
     */
114
    protected function getFormHelper()
115
    {
116
        if (is_null($this->formHelper)) {
117
            $this->formHelper = new FormHelper();
118
        }
119
120
        return $this->formHelper;
121
    }
122
123
    /**
124
     * @param string $template
125
     */
126
    public function setTemplate($template)
127
    {
128
        $this->template = $template;
129
    }
130
131
    /**
132
     * @return string
133
     */
134
    public function getTemplate()
135
    {
136
        return $this->template;
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function setIdentifier($identifier)
143
    {
144
        $this->identifier = $identifier;
145
146
        return $this;
147
    }
148
149
    /**
150
     * @return string
151
     */
152
    public function getIdentifier()
153
    {
154
        return $this->identifier;
155
    }
156
157
    /**
158
     * @param string       $name
159
     * @param string       $type
160
     * @param null         $data
161
     * @param array        $options
162
     *
163
     * @return FormWidget
164
     */
165
    public function addType($name, $type, $data = null, $options = array())
166
    {
167
        $this->types[$name] = $type;
168
        $this->data[$name] = $data;
169
        $this->options[$name] = $options;
170
171
        return $this;
172
    }
173
174
    /**
175
     * @return AbstractType[]
176
     */
177
    public function getTypes()
178
    {
179
        return $this->types;
180
    }
181
182
    /**
183
     * @return array
184
     */
185
    public function getData()
186
    {
187
        return $this->data;
188
    }
189
190
    /**
191
     * @param Request $request
192
     *
193
     * @return array
194
     */
195
    public function getExtraParams(Request $request)
196
    {
197
        return array();
198
    }
199
200
    /**
201
     * @return array
202
     */
203
    public function getOptions()
204
    {
205
        return $this->options;
206
    }
207
}
208