Completed
Pull Request — master (#406)
by
unknown
03:47
created

FormBuilder::injectFormConfigIntoHelper()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Kris\LaravelFormBuilder;
4
5
use Illuminate\Contracts\Container\Container;
6
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
7
use Kris\LaravelFormBuilder\Events\AfterFormCreation;
8
9
class FormBuilder
10
{
11
12
    /**
13
     * @var Container
14
     */
15
    protected $container;
16
17
    /**
18
     * @var FormHelper
19
     */
20
    protected $formHelper;
21
22
    /**
23
     * @var EventDispatcher
24
     */
25
    protected $eventDispatcher;
26
27
    /**
28
     * @param Container $container
29
     * @var string
30
     */
31
    protected $plainFormClass = Form::class;
32
33
    /**
34
     * @param Container  $container
35
     * @param FormHelper $formHelper
36
     * @param EventDispatcher $eventDispatcher
37
     */
38 130
    public function __construct(Container $container, FormHelper $formHelper, EventDispatcher $eventDispatcher)
39
    {
40 130
        $this->container = $container;
41 130
        $this->formHelper = $formHelper;
42 130
        $this->eventDispatcher = $eventDispatcher;
43 130
    }
44
45
    /**
46
     * Create a Form instance.
47
     *
48
     * @param string $formClass The name of the class that inherits \Kris\LaravelFormBuilder\Form.
49
     * @param array $options|null
0 ignored issues
show
Documentation introduced by
There is no parameter named $options|null. Did you maybe mean $options?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
50
     * @param array $data|null
0 ignored issues
show
Bug introduced by
There is no parameter named $data|null. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
51
     * @return Form
52
     */
53 19
    public function create($formClass, array $options = [], array $data = [])
54
    {
55 19
        $form = $this->instantiateFormClass($formClass);
56 18
        $formHelper = $this->injectFormConfigIntoHelper(clone $this->formHelper, $form->getFormConfig());
57
58 18
        $form = $this->setDependenciesAndOptions($form, $options, $data, $formHelper);
59
60 18
        $form->buildForm();
61
62 18
        $this->eventDispatcher->fire(new AfterFormCreation($form));
63
64 18
        $form->filterFields();
65
66 18
        return $form;
67
    }
68
69
    /**
70
     * @param $items
71
     * @param array $options
72
     * @param array $data
73
     * @return mixed
74
     */
75 1
    public function createByArray($items, array $options = [], array $data = [])
76
    {
77 1
        $form = $this->setDependenciesAndOptions(
78 1
            $this->container->make($this->plainFormClass),
79 1
            $options,
80 1
            $data
81
        );
82
83 1
        $this->buildFormByArray($form, $items);
84
85 1
        $this->eventDispatcher->fire(new AfterFormCreation($form));
86
87 1
        $form->filterFields();
88
89 1
        return $form;
90
    }
91
92
    /**
93
     * @param $form
94
     * @param $items
95
     */
96 1
    public function buildFormByArray($form, $items)
97
    {
98 1
        foreach ($items as $item) {
99 1
            if (!isset($item['name'])) {
100
                throw new \InvalidArgumentException(
101
                    'Name is not set in form array.'
102
                );
103
            }
104 1
            $name = $item['name'];
105 1
            $type = isset($item['type']) && $item['type'] ? $item['type'] : '';
106 1
            $modify = isset($item['modify']) && $item['modify'] ? $item['modify'] : false;
107 1
            unset($item['name']);
108 1
            unset($item['type']);
109 1
            unset($item['modify']);
110 1
            $form->add($name, $type, $item, $modify);
111
        }
112 1
    }
113
114
    /**
115
     * Get the namespace from the config
116
     *
117
     * @return string
118
     */
119 19
    protected function getNamespaceFromConfig()
120
    {
121 19
        $namespace = $this->formHelper->getConfig('default_namespace');
122
123 19
        if (!$namespace) {
124 18
            return '';
125
        }
126
127 1
        return $namespace . '\\';
128
    }
129
130
    /**
131
     * Get instance of the empty form which can be modified
132
     * Get the plain form class.
133
     *
134
     * @return string
135
     */
136
    public function getFormClass() {
137
        return $this->plainFormClass;
138
    }
139
140
    /**
141
     * Set the plain form class.
142
     *
143
     * @param string $class
144
     */
145
    public function setFormClass($class) {
146
        $parent = Form::class;
147
        if (!is_a($class, $parent, true)) {
148
            throw new \InvalidArgumentException("Class must be or extend $parent; $class is not.");
149
        }
150
151
        $this->plainFormClass = $class;
152
    }
153
154
    /**
155
     * Get instance of the empty form which can be modified.
156
     *
157
     * @param array $options
158
     * @param array $data
159
     * @return \Kris\LaravelFormBuilder\Form
160
     */
161 130
    public function plain(array $options = [], array $data = [])
162
    {
163 130
        $form = $this->setDependenciesAndOptions(
164 130
            $this->container->make($this->plainFormClass),
165 130
            $options,
166 130
            $data
167
        );
168
169 130
        $this->eventDispatcher->fire(new AfterFormCreation($form));
170
171 130
        $form->filterFields();
172
173 130
        return $form;
174
    }
175
176
    /**
177
     * Set depedencies and options on existing form instance
178
     *
179
     * @param \Kris\LaravelFormBuilder\Form $instance
180
     * @param array $options
181
     * @param array $data
182
     * @param \Kris\LaravelFormBuilder\FormHelper|null $formHelper
183
     * @return \Kris\LaravelFormBuilder\Form
184
     */
185 130
    public function setDependenciesAndOptions($instance, array $options = [], array $data = [], $formHelper = null)
186
    {
187
        return $instance
0 ignored issues
show
Deprecated Code introduced by
The method Kris\LaravelFormBuilder\Form::addData() has been deprecated with message: deprecated since 1.6.12, will be removed in 1.7 - use 3rd param on create, or 2nd on plain method to pass data
will be switched to protected in 1.7.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
188 130
            ->addData($data)
189 130
            ->setRequest($this->container->make('request'))
190 130
            ->setFormHelper($formHelper ? $formHelper : $this->formHelper)
191 130
            ->setEventDispatcher($this->eventDispatcher)
192 130
            ->setFormBuilder($this)
193 130
            ->setValidator($this->container->make('validator'))
194 130
            ->setFormOptions($options);
195
    }
196
197
    /**
198
     * Instantiate and return the passed form class name.
199
     *
200
     * @param string $formClass
201
     * @return \Kris\LaravelFormBuilder\Form
202
     */
203 19
    private function instantiateFormClass($formClass)
204
    {
205 19
        $class = $this->getNamespaceFromConfig() . $formClass;
206
207 19
        if (!class_exists($class)) {
208 1
            throw new \InvalidArgumentException(
209 1
                'Form class with name ' . $class . ' does not exist.'
210
            );
211
        }
212
213 18
        return $this->container->make($class);
214
    }
215
216
    /**
217
     * Inject the passed form object config into the form helper.
218
     *
219
     * @param \Kris\LaravelFormBuilder\FormHelper $formHelper
220
     * @param \Kris\LaravelFormBuilder\Form
221
     *
222
     * @return \Kris\LaravelFormBuilder\FormHelper
223
     */
224 18
    private function injectFormConfigIntoHelper($formHelper, $formConfig)
225
    {
226 18
        $loadedConfig = $formHelper->getConfig();
227
228 18
        if (is_array($formConfig))
229
        {
230 18
            $formHelper->setConfig(array_replace_recursive($loadedConfig, $formConfig));
231
        }
232
233 18
        return $formHelper;
234
    }
235
}
236