Completed
Push — master ( 1cf1dc...74f937 )
by Kristijan
04:42
created

FormBuilder::setDependenciesAndOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 3
dl 0
loc 11
ccs 8
cts 8
cp 1
crap 1
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 110
    public function __construct(Container $container, FormHelper $formHelper, EventDispatcher $eventDispatcher)
39
    {
40 110
        $this->container = $container;
41 110
        $this->formHelper = $formHelper;
42 110
        $this->eventDispatcher = $eventDispatcher;
43 110
    }
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
        return $form;
70
    }
71
72
    /**
73
     * @param $items
74
     * @param array $options
75
     * @param array $data
76
     * @return mixed
77
     */
78 1
    public function createByArray($items, array $options = [], array $data = [])
79
    {
80 1
        $form = $this->setDependenciesAndOptions(
81 1
            $this->container->make($this->plainFormClass),
82
            $options,
83 1
            $data
84
        );
85
86 1
        $this->buildFormByArray($form, $items);
87
88 1
        $this->eventDispatcher->fire(new AfterFormCreation($form));
89
90 1
        return $form;
91
    }
92
93
    /**
94
     * @param $form
95
     * @param $items
96
     */
97 1
    public function buildFormByArray($form, $items)
98
    {
99 1
        foreach ($items as $item) {
100 1
            if (!isset($item['name'])) {
101
                throw new \InvalidArgumentException(
102
                    'Name is not set in form array.'
103
                );
104
            }
105 1
            $name = $item['name'];
106 1
            $type = isset($item['type']) && $item['type'] ? $item['type'] : '';
107 1
            $modify = isset($item['modify']) && $item['modify'] ? $item['modify'] : false;
108 1
            unset($item['name']);
109 1
            unset($item['type']);
110 1
            unset($item['modify']);
111 1
            $form->add($name, $type, $item, $modify);
112
        }
113 1
    }
114
115
    /**
116
     * Get the namespace from the config
117
     *
118
     * @return string
119
     */
120 18
    protected function getNamespaceFromConfig()
121
    {
122 18
        $namespace = $this->formHelper->getConfig('default_namespace');
123
124 18
        if (!$namespace) {
125 17
            return '';
126
        }
127
128 1
        return $namespace . '\\';
129
    }
130
131
    /**
132
     * Get instance of the empty form which can be modified
133
     * Get the plain form class.
134
     *
135
     * @return string
136
     */
137
    public function getFormClass() {
138
        return $this->plainFormClass;
139
    }
140
141
    /**
142
     * Set the plain form class.
143
     *
144
     * @param string $class
145
     */
146
    public function setFormClass($class) {
147
        $parent = Form::class;
148
        if (!is_a($class, $parent, true)) {
149
            throw new \InvalidArgumentException("Class must be or extend $parent; $class is not.");
150
        }
151
152
        $this->plainFormClass = $class;
153
    }
154
155
    /**
156
     * Get instance of the empty form which can be modified.
157
     *
158
     * @param array $options
159
     * @param array $data
160
     * @return \Kris\LaravelFormBuilder\Form
161
     */
162 110
    public function plain(array $options = [], array $data = [])
163
    {
164 110
        $form = $this->setDependenciesAndOptions(
165 110
            $this->container->make($this->plainFormClass),
166
            $options,
167 110
            $data
168
        );
169
170 110
        $this->eventDispatcher->fire(new AfterFormCreation($form));
171
172 110
        return $form;
173
    }
174
175
    /**
176
     * Set depedencies and options on existing form instance
177
     *
178
     * @param \Kris\LaravelFormBuilder\Form $instance
179
     * @param array $options
180
     * @param array $data
181
     * @return \Kris\LaravelFormBuilder\Form
182
     */
183 110
    public function setDependenciesAndOptions($instance, array $options = [], array $data = [])
184
    {
185
        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...
186 110
            ->addData($data)
187 110
            ->setRequest($this->container->make('request'))
188 110
            ->setFormHelper($this->formHelper)
189 110
            ->setEventDispatcher($this->eventDispatcher)
190 110
            ->setFormBuilder($this)
191 110
            ->setValidator($this->container->make('validator'))
192 110
            ->setFormOptions($options);
193
    }
194
}
195