Completed
Pull Request — master (#316)
by
unknown
05:29
created

FormBuilder::buildFormByArray()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7.1782

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 18
nop 2
dl 0
loc 17
ccs 11
cts 13
cp 0.8462
crap 7.1782
rs 8.2222
c 0
b 0
f 0
1
<?php namespace Kris\LaravelFormBuilder;
2
3
use Illuminate\Contracts\Container\Container;
4
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
5
use Kris\LaravelFormBuilder\Events\AfterFormCreation;
6
7
class FormBuilder
8
{
9
10
    /**
11
     * @var Container
12
     */
13
    protected $container;
14
15
    /**
16
     * @var FormHelper
17
     */
18
    protected $formHelper;
19
20
    /**
21
     * @var EventDispatcher
22
     */
23
    protected $eventDispatcher;
24
25
    /**
26
     * @param Container $container
27
     * @var string
28
     */
29
    protected $plainFormClass = Form::class;
30
31
    /**
32
     * @param Container  $container
33
     * @param FormHelper $formHelper
34
     */
35 108
    public function __construct(Container $container, FormHelper $formHelper, EventDispatcher $eventDispatcher)
36
    {
37 108
        $this->container = $container;
38 108
        $this->formHelper = $formHelper;
39 108
        $this->eventDispatcher = $eventDispatcher;
40 108
    }
41
42
    /**
43
     * @param       $formClass
44
     * @param       $options
45
     * @param       $data
46
     * @return Form
47
     */
48 18
    public function create($formClass, array $options = [], array $data = [])
49
    {
50 18
        $class = $this->getNamespaceFromConfig() . $formClass;
51
52 18
        if (!class_exists($class)) {
53 1
            throw new \InvalidArgumentException(
54 1
                'Form class with name ' . $class . ' does not exist.'
55
            );
56
        }
57
58 17
        $form = $this->container
59 17
            ->make($class)
60 17
            ->addData($data)
61 17
            ->setRequest($this->container->make('request'))
62 17
            ->setFormHelper($this->formHelper)
63 17
            ->setEventDispatcher($this->eventDispatcher)
64 17
            ->setFormBuilder($this)
65 17
            ->setValidator($this->container->make('validator'))
66 17
            ->setFormOptions($options);
67
68 17
        $form->buildForm();
69
70 17
        $this->eventDispatcher->fire(new AfterFormCreation($form));
71
72 17
        return $form;
73
    }
74
75
    /**
76
     * @param $items
77
     * @param array $options
78
     * @param array $data
79
     * @return mixed
80
     */
81 1
    public function createByArray($items, array $options = [], array $data = [])
82
    {
83 1
        $form = $this->container
84 1
            ->make($this->plainFormClass)
85 1
            ->addData($data)
86 1
            ->setRequest($this->container->make('request'))
87 1
            ->setFormHelper($this->formHelper)
88 1
            ->setEventDispatcher($this->eventDispatcher)
89 1
            ->setFormBuilder($this)
90 1
            ->setValidator($this->container->make('validator'))
91 1
            ->setFormOptions($options);
92
93 1
        $this->buildFormByArray($form, $items);
94
95 1
        $this->eventDispatcher->fire(new AfterFormCreation($form));
96
97 1
        return $form;
98
    }
99
100
    /**
101
     * @param $form
102
     * @param $items
103
     */
104 1
    public function buildFormByArray($form, $items)
105
    {
106 1
        foreach ($items as $item) {
107 1
            if (!isset($item['name'])) {
108
                throw new \InvalidArgumentException(
109
                    'Name is not set in form array.'
110
                );
111
            }
112 1
            $name = $item['name'];
113 1
            $type = isset($item['type']) && $item['type'] ? $item['type'] : '';
114 1
            $modify = isset($item['modify']) && $item['modify'] ? $item['modify'] : false;
115 1
            unset($item['name']);
116 1
            unset($item['type']);
117 1
            unset($item['modify']);
118 1
            $form->add($name, $type, $item, $modify);
119
        }
120 1
    }
121
122
    /**
123
     * Get the namespace from the config
124
     *
125
     * @return string
126
     */
127 18
    protected function getNamespaceFromConfig()
128
    {
129 18
        $namespace = $this->formHelper->getConfig('default_namespace');
130
131 18
        if (!$namespace) {
132 17
            return '';
133
        }
134
135 1
        return $namespace . '\\';
136
    }
137
138
    /**
139
     * Get instance of the empty form which can be modified
140
     * Get the plain form class.
141
     *
142
     * @return string
143
     */
144
    public function getFormClass() {
145
        return $this->plainFormClass;
146
    }
147
148
    /**
149
     * Set the plain form class.
150
     *
151
     * @param string $class
152
     */
153
    public function setFormClass($class) {
154
        $parent = Form::class;
155
        if (!is_a($class, $parent, true)) {
156
            throw new \InvalidArgumentException("Class must be or extend $parent; $class is not.");
157
        }
158
159
        $this->plainFormClass = $class;
160
    }
161
162
    /**
163
     * Get instance of the empty form which can be modified.
164
     *
165
     * @param array $options
166
     * @param array $data
167
     * @return Form
168
     */
169 108
    public function plain(array $options = [], array $data = [])
170
    {
171 108
        $form = $this->container
172 108
            ->make($this->plainFormClass)
173 108
            ->addData($data)
174 108
            ->setRequest($this->container->make('request'))
175 108
            ->setFormHelper($this->formHelper)
176 108
            ->setEventDispatcher($this->eventDispatcher)
177 108
            ->setFormBuilder($this)
178 108
            ->setValidator($this->container->make('validator'))
179 108
            ->setFormOptions($options);
180
181 108
        $this->eventDispatcher->fire(new AfterFormCreation($form));
182
183 108
        return $form;
184
    }
185
}
186