Completed
Push — master ( 9cc7b2...bb332d )
by Kristijan
11s
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
     * 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 18
    public function create($formClass, array $options = [], array $data = [])
54
    {
55 18
        $class = $this->getNamespaceFromConfig() . $formClass;
56
57 18
        if (!class_exists($class)) {
58 1
            throw new \InvalidArgumentException(
59 1
                'Form class with name ' . $class . ' does not exist.'
60
            );
61
        }
62
63 17
        $form = $this->setDependenciesAndOptions($this->container->make($class), $options, $data);
64
65 17
        $form->buildForm();
66
67 17
        $this->eventDispatcher->fire(new AfterFormCreation($form));
68
69 17
        $form->filterFields();
70
71 17
        return $form;
72
    }
73
74
    /**
75
     * @param $items
76
     * @param array $options
77
     * @param array $data
78
     * @return mixed
79
     */
80 1
    public function createByArray($items, array $options = [], array $data = [])
81
    {
82 1
        $form = $this->setDependenciesAndOptions(
83 1
            $this->container->make($this->plainFormClass),
84 1
            $options,
85 1
            $data
86
        );
87
88 1
        $this->buildFormByArray($form, $items);
89
90 1
        $this->eventDispatcher->fire(new AfterFormCreation($form));
91
92 1
        $form->filterFields();
93
94 1
        return $form;
95
    }
96
97
    /**
98
     * @param $form
99
     * @param $items
100
     */
101 1
    public function buildFormByArray($form, $items)
102
    {
103 1
        foreach ($items as $item) {
104 1
            if (!isset($item['name'])) {
105
                throw new \InvalidArgumentException(
106
                    'Name is not set in form array.'
107
                );
108
            }
109 1
            $name = $item['name'];
110 1
            $type = isset($item['type']) && $item['type'] ? $item['type'] : '';
111 1
            $modify = isset($item['modify']) && $item['modify'] ? $item['modify'] : false;
112 1
            unset($item['name']);
113 1
            unset($item['type']);
114 1
            unset($item['modify']);
115 1
            $form->add($name, $type, $item, $modify);
116
        }
117 1
    }
118
119
    /**
120
     * Get the namespace from the config
121
     *
122
     * @return string
123
     */
124 18
    protected function getNamespaceFromConfig()
125
    {
126 18
        $namespace = $this->formHelper->getConfig('default_namespace');
127
128 18
        if (!$namespace) {
129 17
            return '';
130
        }
131
132 1
        return $namespace . '\\';
133
    }
134
135
    /**
136
     * Get instance of the empty form which can be modified
137
     * Get the plain form class.
138
     *
139
     * @return string
140
     */
141
    public function getFormClass() {
142
        return $this->plainFormClass;
143
    }
144
145
    /**
146
     * Set the plain form class.
147
     *
148
     * @param string $class
149
     */
150
    public function setFormClass($class) {
151
        $parent = Form::class;
152
        if (!is_a($class, $parent, true)) {
153
            throw new \InvalidArgumentException("Class must be or extend $parent; $class is not.");
154
        }
155
156
        $this->plainFormClass = $class;
157
    }
158
159
    /**
160
     * Get instance of the empty form which can be modified.
161
     *
162
     * @param array $options
163
     * @param array $data
164
     * @return \Kris\LaravelFormBuilder\Form
165
     */
166 129
    public function plain(array $options = [], array $data = [])
167
    {
168 129
        $form = $this->setDependenciesAndOptions(
169 129
            $this->container->make($this->plainFormClass),
170 129
            $options,
171 129
            $data
172
        );
173
174 129
        $this->eventDispatcher->fire(new AfterFormCreation($form));
175
176 129
        $form->filterFields();
177
178 129
        return $form;
179
    }
180
181
    /**
182
     * Set depedencies and options on existing form instance
183
     *
184
     * @param \Kris\LaravelFormBuilder\Form $instance
185
     * @param array $options
186
     * @param array $data
187
     * @return \Kris\LaravelFormBuilder\Form
188
     */
189 129
    public function setDependenciesAndOptions($instance, array $options = [], array $data = [])
190
    {
191
        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...
192 129
            ->addData($data)
193 129
            ->setRequest($this->container->make('request'))
194 129
            ->setFormHelper($this->formHelper)
195 129
            ->setEventDispatcher($this->eventDispatcher)
196 129
            ->setFormBuilder($this)
197 129
            ->setValidator($this->container->make('validator'))
198 129
            ->setFormOptions($options);
199
    }
200
}
201