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 | 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 | public function create($formClass, array $options = [], array $data = []) |
||
43 | { |
||
44 | $class = $this->getNamespaceFromConfig() . $formClass; |
||
45 | |||
46 | if (!class_exists($class)) { |
||
47 | throw new \InvalidArgumentException( |
||
48 | 'Form class with name ' . $class . ' does not exist.' |
||
49 | ); |
||
50 | } |
||
51 | |||
52 | $form = $this->container |
||
53 | ->make($class) |
||
54 | ->addData($data) |
||
55 | ->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 | |||
64 | $this->eventDispatcher->fire(new AfterFormCreation($form)); |
||
65 | |||
66 | return $form; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Get the namespace from the config |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | 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 | public function plain(array $options = [], array $data = []) |
||
103 | } |
||
104 |