1 | <?php |
||
10 | class Generator implements GeneratorContract |
||
11 | { |
||
12 | protected $factory; |
||
13 | |||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $fields; |
||
18 | |||
19 | /** |
||
20 | * @var Collection |
||
21 | */ |
||
22 | protected $relations; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $databaseTypeToFormType = [ |
||
28 | DbalType::INTEGER => 'number', |
||
29 | DbalType::STRING => 'text', |
||
30 | DbalType::TEXT => 'textarea', |
||
31 | DbalType::BOOLEAN => 'checkbox', |
||
32 | DbalType::DATE => 'date', |
||
33 | DbalType::TIME => 'time', |
||
34 | DbalType::DATETIME => 'datetime', |
||
35 | DbalType::DECIMAL => 'number', |
||
36 | DbalType::FLOAT => 'number', |
||
37 | 'email', |
||
38 | 'password', |
||
39 | 'hidden', |
||
40 | 'select', |
||
41 | ]; |
||
42 | |||
43 | 3 | public function __construct(FactoryInterface $factory) |
|
44 | { |
||
45 | 3 | $this->factory = $factory; |
|
46 | 3 | $this->fields = []; |
|
47 | 3 | } |
|
48 | |||
49 | public function setModelFields(array $fields) |
||
50 | { |
||
51 | $this->fields = $fields; |
||
52 | } |
||
53 | |||
54 | 1 | public function addModelFields(array $fields) |
|
55 | { |
||
56 | 1 | $this->fields = array_merge_recursive($this->fields, $fields); |
|
57 | 1 | } |
|
58 | |||
59 | 1 | public function setModelRelations(Collection $relations) |
|
60 | { |
||
61 | 1 | $this->relations = $relations; |
|
62 | 1 | } |
|
63 | |||
64 | 1 | public function setRelatedModelFields(Collection $relations) |
|
65 | { |
||
66 | 1 | $this->setModelRelations($relations); |
|
67 | |||
68 | 1 | if ($this->relations->count() > 0) { |
|
69 | 1 | foreach ($this->relations as $relation) { |
|
70 | 1 | if ($relation instanceof Collection) { |
|
71 | $this->addModelFields($relation->get('relation')->getEditFields()); |
||
72 | } else { |
||
73 | 1 | $this->addModelFields($relation->getEditFields()); |
|
74 | } |
||
75 | 1 | } |
|
76 | 1 | } |
|
77 | 1 | } |
|
78 | |||
79 | public function getForm($action) |
||
80 | { |
||
81 | $form = $this->factory->get('form', []); |
||
82 | |||
83 | $form->attr([ |
||
84 | 'action' => $action, |
||
85 | 'enctype' => 'multipart/form-data', |
||
86 | 'method' => 'post', |
||
87 | 'class' => 'form-horizontal', |
||
88 | ]); |
||
89 | |||
90 | |||
91 | $formFields = []; |
||
92 | foreach ($this->fields as $fieldGroupName => $fieldGroup) { |
||
93 | $tempFields = $this->addFormFields($fieldGroup, $fieldGroupName); |
||
94 | $formFields[key($tempFields)] = $this->factory->get('group', [$tempFields[key($tempFields)]]); |
||
95 | } |
||
96 | |||
97 | $form->add($formFields); |
||
98 | |||
99 | |||
100 | return $form; |
||
101 | } |
||
102 | |||
103 | public function getValidationRules() |
||
104 | { |
||
105 | $rules = []; |
||
106 | |||
107 | foreach ($this->fields as $fieldGroupName => $fieldGroup) { |
||
108 | foreach ($fieldGroup as $fieldKey => $field) { |
||
109 | if (is_array($field)) { |
||
110 | $rules[$fieldGroupName] = $this->addValidationRules($field, $fieldKey); |
||
111 | } else { |
||
112 | $rules[$fieldGroupName][$field->getName()] = $field->getValidationRules(); |
||
113 | } |
||
114 | } |
||
115 | } |
||
116 | |||
117 | return $rules; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @param array $fields |
||
122 | * @param $key |
||
123 | * |
||
124 | * @return array |
||
125 | */ |
||
126 | protected function addFormFields(array $fields, $key) |
||
127 | { |
||
128 | $formFields = []; |
||
129 | $tempFields = []; |
||
130 | |||
131 | foreach ($fields as $fieldKey => $field) { |
||
132 | if (is_array($field)) { |
||
133 | $group = $this->addFormFields($field, $fieldKey); |
||
134 | $tempFields[key($group)] = $this->factory->get('group', [$group[key($group)]]); |
||
135 | } else { |
||
136 | $tempFields[$field->getName()] = $field->getFormField(); |
||
137 | } |
||
138 | } |
||
139 | $formFields[$key] = $tempFields; |
||
140 | |||
141 | return $formFields; |
||
142 | } |
||
143 | |||
144 | protected function addValidationRules(array $fields, $key) |
||
160 | } |
||
161 |