Completed
Push — master ( 890a4a...34a5ce )
by Kristijan
04:16
created

FormBuilder::setFormClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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