Completed
Pull Request — master (#316)
by
unknown
04:34
created

FormBuilder::buildFormByArray()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 18
nop 2
dl 0
loc 17
ccs 0
cts 13
cp 0
crap 56
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 107
    public function __construct(Container $container, FormHelper $formHelper, EventDispatcher $eventDispatcher)
36
    {
37 107
        $this->container = $container;
38 107
        $this->formHelper = $formHelper;
39 107
        $this->eventDispatcher = $eventDispatcher;
40 107
    }
41
42
    /**
43
     * @param       $formClass
44
     * @param       $options
45
     * @param       $data
46
     * @return Form
47
     */
48 17
    public function create($formClass, array $options = [], array $data = [])
49
    {
50 17
        $class = $this->getNamespaceFromConfig() . $formClass;
51
52 17
        if (!class_exists($class)) {
53 1
            throw new \InvalidArgumentException(
54 1
                'Form class with name ' . $class . ' does not exist.'
55
            );
56
        }
57
58 16
        $form = $this->container
59 16
            ->make($class)
60 16
            ->addData($data)
61 16
            ->setRequest($this->container->make('request'))
62 16
            ->setFormHelper($this->formHelper)
63 16
            ->setEventDispatcher($this->eventDispatcher)
64 16
            ->setFormBuilder($this)
65 16
            ->setValidator($this->container->make('validator'))
66 16
            ->setFormOptions($options);
67
68 16
        $form->buildForm();
69
70 16
        $this->eventDispatcher->fire(new AfterFormCreation($form));
71
72 16
        return $form;
73
    }
74
75
    /**
76
     * @param $items
77
     * @param array $options
78
     * @param array $data
79
     * @return mixed
80
     */
81
    public function createByArray($items, array $options = [], array $data = [])
82
    {
83
        $form = $this->container
84
            ->make(Form::class)
85
            ->addData($data)
86
            ->setRequest($this->container->make('request'))
87
            ->setFormHelper($this->formHelper)
88
            ->setEventDispatcher($this->eventDispatcher)
89
            ->setFormBuilder($this)
90
            ->setValidator($this->container->make('validator'))
91
            ->setFormOptions($options);
92
93
        $this->buildFormByArray($form, $items);
94
95
        $this->eventDispatcher->fire(new AfterFormCreation($form));
96
97
        return $form;
98
    }
99
100
    /**
101
     * @param $form
102
     * @param $items
103
     */
104
    public function buildFormByArray($form, $items)
105
    {
106
        foreach ($items as $item) {
107
            if (!isset($item['name'])) {
108
                throw new \InvalidArgumentException(
109
                    'Name is not set in form array.'
110
                );
111
            }
112
            $name = $item['name'];
113
            $type = isset($item['type']) && $item['type'] ? $item['type'] : '';
114
            $modify = isset($item['modify']) && $item['modify'] ? $item['modify'] : false;
115
            unset($item['name']);
116
            unset($item['type']);
117
            unset($item['modify']);
118
            $form->add($name, $type, $item, $modify);
119
        }
120
    }
121
122
    /**
123
     * Get the namespace from the config
124
     *
125
     * @return string
126
     */
127 17
    protected function getNamespaceFromConfig()
128
    {
129 17
        $namespace = $this->formHelper->getConfig('default_namespace');
130
131 17
        if (!$namespace) {
132 16
            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 107
    public function plain(array $options = [], array $data = [])
170
    {
171 107
        $form = $this->container
172 107
            ->make($this->plainFormClass)
173 107
            ->addData($data)
174 107
            ->setRequest($this->container->make('request'))
175 107
            ->setFormHelper($this->formHelper)
176 107
            ->setEventDispatcher($this->eventDispatcher)
177 107
            ->setFormBuilder($this)
178 107
            ->setValidator($this->container->make('validator'))
179 107
            ->setFormOptions($options);
180
181 107
        $this->eventDispatcher->fire(new AfterFormCreation($form));
182
183 107
        return $form;
184
    }
185
}
186