diff src/Generator/Factory/FactoryGenerator.php @ 40:2cf26b593f4a ls_dev_2025_09 tip

better support for different column types
author Luka Sitas <sitas.luka.97@gmail.com>
date Thu, 16 Oct 2025 10:54:04 -0400
parents b5c6ebd33547
children
line wrap: on
line diff
--- a/src/Generator/Factory/FactoryGenerator.php	Thu Sep 25 23:16:13 2025 -0400
+++ b/src/Generator/Factory/FactoryGenerator.php	Thu Oct 16 10:54:04 2025 -0400
@@ -85,71 +85,58 @@
     protected function gatherRelations()
     {
         $relations = RelationshipNavigator::getRelations($this->getCurrentTable());
+        foreach ($relations as $relation_type => $relation_values) {
+            $relations[$relation_type] = array_column($relation_values, null, 'column');
+        }
 
         return $relations;
-
     }
 
     protected function renderColumns()
     {
-        $insert = '';
         $values = [];
+        $relations = $this->gatherRelations();
+
         foreach ($this->get_columns() as $column) {
             if (in_array($column['name'], $this->columns_to_ignore)) {
                 continue;
             }
 
-            $type = $column['type_name'];
-            $nullable = ($column['nullable'] ? '->optional($weight = 0.5)' : '');
-            $value = 'fake()'.$nullable;
-            $name = $column['name'];
-
-            // Get the expected header name
-            $replacements = [
-                '{{value}}' => $value.'->text()',
-                '{{column_name}}' => $name,
-            ];
-
-            // date
-            if (in_array($type, ['date'])) {
-                $replacements['{{value}}'] = $value.'->date()';
-            }
-            // time
-            if (in_array($type, ['timestamp'])) {
-                $replacements['{{value}}'] = $value.'->timestamp()';
-            }
-            // checkbox
-            if (in_array($type, ['tinyint'])) {
-                $replacements['{{value}}'] = $value.'->numberBetween(0,1)';
-            }
-            // select
-            elseif (in_array($type, ['bigint']) && array_key_exists($name, $selects)) {
-                $replacements['{{header}}'] = Str::headline(Str::singular($selects[$name]));
-                $replacements['{{value}}'] = '{{ $item->'.Str::singular($selects[$name]).'?->name ?? "" }}';
-            }
-            // bigint, float
-            elseif (in_array($type, ['bigint', 'int'])) {
-                $replacements['{{value}}'] = $value.'->randomNumber()';
-            } elseif (in_array($type, ['float'])) {
-                $replacements['{{value}}'] = $value.'->randomFloat()';
-            } else {
-                // text area
-                // varchar, , etc
-            }
-
+            $value = $this->getFakeValue($column, $relations);
             $snippet = $this->getSnippet('value');
-            // Replace placeholders with actual values
-            $values[] = str_replace(
-                array_keys($replacements),
-                $replacements,
-                $snippet
-            );
 
             // Replace placeholders with actual values
+            $values[] = str_replace(['{{value}}', '{{column_name}}'], [$value, $column['name']], $snippet);
         }
-        $insert = implode("\n", $values);
+
+        return implode("\n", $values);
+    }
+
+    protected function getFakeValue($column, $relations)
+    {
+        $value = '$this->faker';
+        $nullable = ($column['nullable'] ? '->optional($weight = 0.5)' : '');
 
-        return $insert;
+        switch ($column['type_name']) {
+            case 'date':
+                return "$value$nullable->date()";
+            case 'timestamp':
+                return "$value$nullable->timestamp()";
+            case 'tinyint':
+                return "$value$nullable->numberBetween(0,1)";
+            case 'bigint':
+                if (isset($relations['belongsTo'][$column['name']])) {
+                    $related = $this->getNamespacedModel($relations['belongsTo'][$column['name']]['table']);
+                    return "$related::factory()";
+                }
+                // fall through to int for lack of better option
+            case 'int':
+                return "$value$nullable->randomNumber()";
+            case 'float':
+                return "$value$nullable->randomFloat()";
+            default:
+                return "$value$nullable->text()";
+        }
     }
 
     /**