1 | <?php namespace Kris\LaravelFormBuilder; |
||
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) |
|
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 = []) |
|
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() |
|
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() { |
||
147 | |||
148 | /** |
||
149 | * Set the plain form class. |
||
150 | * |
||
151 | * @param string $class |
||
152 | */ |
||
153 | public function setFormClass($class) { |
||
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 = []) |
|
185 | } |
||
186 |