Completed
Push — master ( 976295...cf57a5 )
by Kristijan
03:22
created

FormBuilder::buildFormByArray()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7.1782

Importance

Changes 0
Metric Value
cc 7
nc 18
nop 2
dl 0
loc 17
rs 8.8333
c 0
b 0
f 0
ccs 11
cts 13
cp 0.8462
crap 7.1782
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 129
    public function __construct(Container $container, FormHelper $formHelper, EventDispatcher $eventDispatcher)
39
    {
40 129
        $this->container = $container;
41 129
        $this->formHelper = $formHelper;
42 129
        $this->eventDispatcher = $eventDispatcher;
43 129
    }
44
45
    /**
46
     * Fire an event.
47
     *
48
     * @param object $event
49
     * @return array|null
50
     */
51 9
    public function fireEvent($event)
52
    {
53 9
        return $this->eventDispatcher->dispatch($event);
54
    }
55
56
    /**
57
     * Create a Form instance.
58
     *
59
     * @param string $formClass The name of the class that inherits \Kris\LaravelFormBuilder\Form.
60
     * @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...
61
     * @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...
62
     * @return Form
63
     */
64 18
    public function create($formClass, array $options = [], array $data = [])
65
    {
66 18
        $class = $this->getNamespaceFromConfig() . $formClass;
67
68 18
        if (!class_exists($class)) {
69 1
            throw new \InvalidArgumentException(
70 1
                'Form class with name ' . $class . ' does not exist.'
71
            );
72
        }
73
74 17
        $form = $this->setDependenciesAndOptions($this->container->make($class), $options, $data);
75
76 17
        $form->buildForm();
77
78 17
        $this->eventDispatcher->dispatch(new AfterFormCreation($form));
79
80 17
        $form->filterFields();
81
82 17
        return $form;
83
    }
84
85
    /**
86
     * @param $items
87
     * @param array $options
88
     * @param array $data
89
     * @return mixed
90
     */
91 1
    public function createByArray($items, array $options = [], array $data = [])
92
    {
93 1
        $form = $this->setDependenciesAndOptions(
94 1
            $this->container->make($this->plainFormClass),
95 1
            $options,
96 1
            $data
97
        );
98
99 1
        $this->buildFormByArray($form, $items);
100
101 1
        $this->eventDispatcher->dispatch(new AfterFormCreation($form));
102
103 1
        $form->filterFields();
104
105 1
        return $form;
106
    }
107
108
    /**
109
     * @param $form
110
     * @param $items
111
     */
112 1
    public function buildFormByArray($form, $items)
113
    {
114 1
        foreach ($items as $item) {
115 1
            if (!isset($item['name'])) {
116
                throw new \InvalidArgumentException(
117
                    'Name is not set in form array.'
118
                );
119
            }
120 1
            $name = $item['name'];
121 1
            $type = isset($item['type']) && $item['type'] ? $item['type'] : '';
122 1
            $modify = isset($item['modify']) && $item['modify'] ? $item['modify'] : false;
123 1
            unset($item['name']);
124 1
            unset($item['type']);
125 1
            unset($item['modify']);
126 1
            $form->add($name, $type, $item, $modify);
127
        }
128 1
    }
129
130
    /**
131
     * Get the namespace from the config
132
     *
133
     * @return string
134
     */
135 18
    protected function getNamespaceFromConfig()
136
    {
137 18
        $namespace = $this->formHelper->getConfig('default_namespace');
138
139 18
        if (!$namespace) {
140 17
            return '';
141
        }
142
143 1
        return $namespace . '\\';
144
    }
145
146
    /**
147
     * Get instance of the empty form which can be modified
148
     * Get the plain form class.
149
     *
150
     * @return string
151
     */
152
    public function getFormClass() {
153
        return $this->plainFormClass;
154
    }
155
156
    /**
157
     * Set the plain form class.
158
     *
159
     * @param string $class
160
     */
161
    public function setFormClass($class) {
162
        $parent = Form::class;
163
        if (!is_a($class, $parent, true)) {
164
            throw new \InvalidArgumentException("Class must be or extend $parent; $class is not.");
165
        }
166
167
        $this->plainFormClass = $class;
168
    }
169
170
    /**
171
     * Get instance of the empty form which can be modified.
172
     *
173
     * @param array $options
174
     * @param array $data
175
     * @return \Kris\LaravelFormBuilder\Form
176
     */
177 129
    public function plain(array $options = [], array $data = [])
178
    {
179 129
        $form = $this->setDependenciesAndOptions(
180 129
            $this->container->make($this->plainFormClass),
181 129
            $options,
182 129
            $data
183
        );
184
185 129
        $this->eventDispatcher->dispatch(new AfterFormCreation($form));
186
187 129
        $form->filterFields();
188
189 129
        return $form;
190
    }
191
192
    /**
193
     * Set depedencies and options on existing form instance
194
     *
195
     * @param \Kris\LaravelFormBuilder\Form $instance
196
     * @param array $options
197
     * @param array $data
198
     * @return \Kris\LaravelFormBuilder\Form
199
     */
200 129
    public function setDependenciesAndOptions($instance, array $options = [], array $data = [])
201
    {
202
        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...
203 129
            ->addData($data)
204 129
            ->setRequest($this->container->make('request'))
205 129
            ->setFormHelper($this->formHelper)
206 129
            ->setEventDispatcher($this->eventDispatcher)
207 129
            ->setFormBuilder($this)
208 129
            ->setValidator($this->container->make('validator'))
209 129
            ->setFormOptions($options);
210
    }
211
}
212