|
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
|
98 |
|
*/ |
|
23
|
|
|
protected $eventDispatcher; |
|
24
|
98 |
|
|
|
25
|
98 |
|
/** |
|
26
|
98 |
|
* @param Container $container |
|
27
|
|
|
* @param FormHelper $formHelper |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(Container $container, FormHelper $formHelper, EventDispatcher $eventDispatcher) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->container = $container; |
|
32
|
|
|
$this->formHelper = $formHelper; |
|
33
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
34
|
13 |
|
} |
|
35
|
|
|
|
|
36
|
13 |
|
/** |
|
37
|
|
|
* @param $formClass |
|
38
|
13 |
|
* @param $options |
|
39
|
1 |
|
* @param $data |
|
40
|
1 |
|
* @return Form |
|
41
|
|
|
*/ |
|
42
|
|
|
public function create($formClass, array $options = [], array $data = []) |
|
43
|
|
|
{ |
|
44
|
12 |
|
$class = $this->getNamespaceFromConfig() . $formClass; |
|
45
|
12 |
|
|
|
46
|
12 |
|
if (!class_exists($class)) { |
|
47
|
12 |
|
throw new \InvalidArgumentException( |
|
48
|
12 |
|
'Form class with name ' . $class . ' does not exist.' |
|
49
|
12 |
|
); |
|
50
|
12 |
|
} |
|
51
|
12 |
|
|
|
52
|
|
|
$form = $this->container |
|
53
|
12 |
|
->make($class) |
|
54
|
|
|
->addData($data) |
|
55
|
12 |
|
->setRequest($this->container->make('request')) |
|
56
|
|
|
->setFormHelper($this->formHelper) |
|
57
|
|
|
->setEventDispatcher($this->eventDispatcher) |
|
58
|
|
|
->setFormBuilder($this) |
|
59
|
|
|
->setValidator($this->container->make('validator')) |
|
60
|
|
|
->setFormOptions($options); |
|
61
|
|
|
|
|
62
|
|
|
$form->buildForm(); |
|
63
|
13 |
|
|
|
64
|
|
|
$this->eventDispatcher->fire(new AfterFormCreation($form)); |
|
65
|
13 |
|
|
|
66
|
|
|
return $form; |
|
67
|
13 |
|
} |
|
68
|
12 |
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get the namespace from the config |
|
71
|
1 |
|
* |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function getNamespaceFromConfig() |
|
75
|
|
|
{ |
|
76
|
|
|
$namespace = $this->formHelper->getConfig('default_namespace'); |
|
77
|
|
|
|
|
78
|
|
|
if (!$namespace) { |
|
79
|
|
|
return ''; |
|
80
|
|
|
} |
|
81
|
98 |
|
|
|
82
|
|
|
return $namespace . '\\'; |
|
83
|
98 |
|
} |
|
84
|
98 |
|
|
|
85
|
98 |
|
/** |
|
86
|
98 |
|
* Get instance of the empty form which can be modified |
|
87
|
98 |
|
* |
|
88
|
98 |
|
* @param array $options |
|
89
|
98 |
|
* @param array $data |
|
90
|
98 |
|
* @return Form |
|
91
|
|
|
*/ |
|
92
|
|
|
public function plain(array $options = [], array $data = []) |
|
93
|
|
|
{ |
|
94
|
|
|
$form = $this->container |
|
95
|
|
|
->make('Kris\LaravelFormBuilder\Form') |
|
96
|
|
|
->addData($data) |
|
97
|
|
|
->setRequest($this->container->make('request')) |
|
98
|
|
|
->setFormHelper($this->formHelper) |
|
99
|
|
|
->setEventDispatcher($this->eventDispatcher) |
|
100
|
|
|
->setFormBuilder($this) |
|
101
|
|
|
->setValidator($this->container->make('validator')) |
|
102
|
|
|
->setFormOptions($options); |
|
103
|
|
|
|
|
104
|
|
|
$this->eventDispatcher->fire(new AfterFormCreation($form)); |
|
105
|
|
|
|
|
106
|
|
|
return $form; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|