Mercurial > packages > magicforger
comparison src/Generator/Factory/FactoryGenerator.php @ 36:76584181267a ls_dev_2025_09
Got factories working in a basic way, not sure how complex tables will handle it though
| author | Luka Sitas <sitas.luka.97@gmail.com> |
|---|---|
| date | Sat, 20 Sep 2025 17:14:29 -0400 |
| parents | 55d2e5c5dad9 |
| children | b5c6ebd33547 |
comparison
equal
deleted
inserted
replaced
| 35:55d2e5c5dad9 | 36:76584181267a |
|---|---|
| 89 | 89 |
| 90 } | 90 } |
| 91 | 91 |
| 92 protected function renderColumns() { | 92 protected function renderColumns() { |
| 93 $insert = ''; | 93 $insert = ''; |
| 94 $values = []; | |
| 94 foreach ($this->get_columns() as $column) { | 95 foreach ($this->get_columns() as $column) { |
| 95 if (in_array($column['name'], $this->columns_to_ignore)) { | 96 if (in_array($column['name'], $this->columns_to_ignore)) { |
| 96 continue; | 97 continue; |
| 97 } | 98 } |
| 98 | 99 |
| 99 | |
| 100 $type = $column['type_name']; | 100 $type = $column['type_name']; |
| 101 $nullable = ($column['nullable'] ? '->optional($weight = 0.5)' : '' ); | 101 $nullable = ($column['nullable'] ? '->optional($weight = 0.5)' : '' ); |
| 102 $value = 'fake()' . $nullable; | |
| 102 $name = $column['name']; | 103 $name = $column['name']; |
| 103 | 104 |
| 104 // Get the expected header name | 105 // Get the expected header name |
| 105 $replacements = [ | 106 $replacements = [ |
| 106 '{{value}}' => 'fake()' . $nullable . '->text()', | 107 '{{value}}' => $value . '->text()', |
| 107 '{{column_name}}' => $name, | 108 '{{column_name}}' => $name, |
| 108 ]; | 109 ]; |
| 109 | 110 |
| 110 // date | 111 // date |
| 111 if (in_array($type, ['date'])) { | 112 if (in_array($type, ['date'])) { |
| 112 $replacements['{{value}}'] = 'fake()' . $nullable . '->date()'; | 113 $replacements['{{value}}'] = $value . '->date()'; |
| 113 } | 114 } |
| 114 // time | 115 // time |
| 115 if (in_array($type, ['timestamp'])) { | 116 if (in_array($type, ['timestamp'])) { |
| 116 $replacements['{{value}}'] = 'fake()' . $nullable . '->timestamp()'; | 117 $replacements['{{value}}'] = $value . '->timestamp()'; |
| 117 } | 118 } |
| 118 // checkbox | 119 // checkbox |
| 119 if (in_array($type, ['tinyint'])) { | 120 if (in_array($type, ['tinyint'])) { |
| 120 $replacements['{{value}}'] = 'fake()' . $nullable . '->boolean()'; | 121 $replacements['{{value}}'] = $value . '->boolean()'; |
| 121 } | 122 } |
| 122 // select | 123 // select |
| 123 elseif (in_array($type, ['bigint']) && array_key_exists($name, $selects)) { | 124 elseif (in_array($type, ['bigint']) && array_key_exists($name, $selects)) { |
| 124 $replacements['{{header}}'] = Str::headline(Str::singular($selects[$name])); | 125 $replacements['{{header}}'] = Str::headline(Str::singular($selects[$name])); |
| 125 $replacements['{{value}}'] = '{{ $item->'.Str::singular($selects[$name]).'?->name ?? "" }}'; | 126 $replacements['{{value}}'] = '{{ $item->'.Str::singular($selects[$name]).'?->name ?? "" }}'; |
| 126 } | 127 } |
| 127 // bigint, float | 128 // bigint, float |
| 128 elseif (in_array($type, ['bigint', 'float', 'int'])) { | 129 elseif (in_array($type, ['bigint', 'int'])) { |
| 129 $replacements['{{valueClass}}'] .= ' text-start'; | 130 $replacements['{{value}}'] = $value . '->randomNumber()'; |
| 131 } | |
| 132 elseif (in_array($type, ['float'])) { | |
| 133 $replacements['{{value}}'] = $value . '->randomFloat()'; | |
| 130 } else { | 134 } else { |
| 131 // text area | 135 // text area |
| 132 // varchar, , etc | 136 // varchar, , etc |
| 133 } | 137 } |
| 134 | 138 |
| 135 $snippet = $this->getSnippet('index/value'); | 139 $snippet = $this->getSnippet('value'); |
| 136 // Replace placeholders with actual values | 140 // Replace placeholders with actual values |
| 137 $values[] = str_replace( | 141 $values[] = str_replace( |
| 138 array_keys($replacements), | 142 array_keys($replacements), |
| 139 $replacements, | 143 $replacements, |
| 140 $snippet | 144 $snippet |
| 141 ); | 145 ); |
| 142 | 146 |
| 143 | |
| 144 | |
| 145 $tableName = $this->getCurrentTable(); | |
| 146 $value = 'value'; // TODO: this should be determined based on column type | |
| 147 $columnName = $column['name']; | |
| 148 | |
| 149 // Replace placeholders with actual values | 147 // Replace placeholders with actual values |
| 150 $string = str_replace( | |
| 151 ['{{value}}', '{{columnName}}'], | |
| 152 [$value, $columnName], | |
| 153 $snippet | |
| 154 ); | |
| 155 $insert .= sprintf("%s", $string); | |
| 156 } | 148 } |
| 157 dd('done'); | 149 $insert = implode("\n", $values); |
| 158 | 150 |
| 159 return $insert; | 151 return $insert; |
| 160 } | 152 } |
| 161 | 153 |
| 162 /** | 154 /** |
