1 | <?php |
||
9 | class FormBuilder |
||
10 | { |
||
11 | |||
12 | /** |
||
13 | * @var Container |
||
14 | */ |
||
15 | protected $container; |
||
16 | |||
17 | /** |
||
18 | * @var FormHelper |
||
19 | */ |
||
20 | protected $formHelper; |
||
21 | |||
22 | /** |
||
23 | * @var EventDispatcher |
||
24 | */ |
||
25 | protected $eventDispatcher; |
||
26 | |||
27 | /** |
||
28 | * @param Container $container |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $plainFormClass = Form::class; |
||
32 | |||
33 | /** |
||
34 | * @param Container $container |
||
35 | * @param FormHelper $formHelper |
||
36 | * @param EventDispatcher $eventDispatcher |
||
37 | */ |
||
38 | 125 | public function __construct(Container $container, FormHelper $formHelper, EventDispatcher $eventDispatcher) |
|
44 | |||
45 | /** |
||
46 | * Create a Form instance. |
||
47 | * |
||
48 | * @param string $formClass The name of the class that inherits \Kris\LaravelFormBuilder\Form. |
||
49 | * @param array $options|null |
||
|
|||
50 | * @param array $data|null |
||
51 | * @return Form |
||
52 | */ |
||
53 | 18 | public function create($formClass, array $options = [], array $data = []) |
|
54 | { |
||
55 | 18 | $class = $this->getNamespaceFromConfig() . $formClass; |
|
56 | |||
57 | 18 | if (!class_exists($class)) { |
|
58 | 1 | throw new \InvalidArgumentException( |
|
59 | 1 | 'Form class with name ' . $class . ' does not exist.' |
|
60 | 1 | ); |
|
61 | } |
||
62 | |||
63 | 17 | $form = $this->setDependenciesAndOptions($this->container->make($class), $options, $data); |
|
64 | |||
65 | 17 | $form->buildForm(); |
|
66 | |||
67 | 17 | $this->eventDispatcher->fire(new AfterFormCreation($form)); |
|
68 | |||
69 | 17 | $form->filterFields(); |
|
70 | |||
71 | 17 | return $form; |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param $items |
||
76 | * @param array $options |
||
77 | * @param array $data |
||
78 | * @return mixed |
||
79 | */ |
||
80 | 1 | public function createByArray($items, array $options = [], array $data = []) |
|
96 | |||
97 | /** |
||
98 | * @param $form |
||
99 | * @param $items |
||
100 | */ |
||
101 | 1 | public function buildFormByArray($form, $items) |
|
102 | { |
||
103 | 1 | foreach ($items as $item) { |
|
104 | 1 | if (!isset($item['name'])) { |
|
105 | throw new \InvalidArgumentException( |
||
106 | 'Name is not set in form array.' |
||
107 | ); |
||
108 | } |
||
109 | 1 | $name = $item['name']; |
|
110 | 1 | $type = isset($item['type']) && $item['type'] ? $item['type'] : ''; |
|
111 | 1 | $modify = isset($item['modify']) && $item['modify'] ? $item['modify'] : false; |
|
112 | 1 | unset($item['name']); |
|
113 | 1 | unset($item['type']); |
|
114 | 1 | unset($item['modify']); |
|
115 | 1 | $form->add($name, $type, $item, $modify); |
|
116 | 1 | } |
|
117 | 1 | } |
|
118 | |||
119 | /** |
||
120 | * Get the namespace from the config |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | 18 | protected function getNamespaceFromConfig() |
|
134 | |||
135 | /** |
||
136 | * Get instance of the empty form which can be modified |
||
137 | * Get the plain form class. |
||
138 | * |
||
139 | * @return string |
||
140 | */ |
||
141 | public function getFormClass() { |
||
144 | |||
145 | /** |
||
146 | * Set the plain form class. |
||
147 | * |
||
148 | * @param string $class |
||
149 | */ |
||
150 | public function setFormClass($class) { |
||
158 | |||
159 | /** |
||
160 | * Get instance of the empty form which can be modified. |
||
161 | * |
||
162 | * @param array $options |
||
163 | * @param array $data |
||
164 | * @return \Kris\LaravelFormBuilder\Form |
||
165 | */ |
||
166 | 125 | public function plain(array $options = [], array $data = []) |
|
180 | |||
181 | /** |
||
182 | * Set depedencies and options on existing form instance |
||
183 | * |
||
184 | * @param \Kris\LaravelFormBuilder\Form $instance |
||
185 | * @param array $options |
||
186 | * @param array $data |
||
187 | * @return \Kris\LaravelFormBuilder\Form |
||
188 | */ |
||
189 | 125 | public function setDependenciesAndOptions($instance, array $options = [], array $data = []) |
|
200 | } |
||
201 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.