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 | * @param FormHelper $formHelper |
||
28 | */ |
||
29 | 101 | public function __construct(Container $container, FormHelper $formHelper, EventDispatcher $eventDispatcher) |
|
35 | |||
36 | /** |
||
37 | * @param $formClass |
||
38 | * @param $options |
||
39 | * @param $data |
||
40 | * @return Form |
||
41 | */ |
||
42 | 14 | public function create($formClass, array $options = [], array $data = []) |
|
43 | { |
||
44 | 14 | $class = $this->getNamespaceFromConfig() . $formClass; |
|
45 | |||
46 | 14 | if (!class_exists($class)) { |
|
47 | 1 | throw new \InvalidArgumentException( |
|
48 | 1 | 'Form class with name ' . $class . ' does not exist.' |
|
49 | ); |
||
50 | } |
||
51 | |||
52 | 13 | $form = $this->container |
|
53 | 13 | ->make($class) |
|
54 | 13 | ->addData($data) |
|
55 | 13 | ->setRequest($this->container->make('request')) |
|
56 | 13 | ->setFormHelper($this->formHelper) |
|
57 | 13 | ->setEventDispatcher($this->eventDispatcher) |
|
58 | 13 | ->setFormBuilder($this) |
|
59 | 13 | ->setValidator($this->container->make('validator')) |
|
60 | 13 | ->setFormOptions($options); |
|
61 | |||
62 | 13 | $form->buildForm(); |
|
63 | |||
64 | 13 | $this->eventDispatcher->fire(new AfterFormCreation($form)); |
|
65 | |||
66 | 13 | return $form; |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * Get the namespace from the config |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | 14 | protected function getNamespaceFromConfig() |
|
84 | |||
85 | /** |
||
86 | * Get instance of the empty form which can be modified |
||
87 | * |
||
88 | * @param array $options |
||
89 | * @param array $data |
||
90 | * @return Form |
||
91 | */ |
||
92 | 101 | public function plain(array $options = [], array $data = []) |
|
108 | } |
||
109 |