comparison 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
comparison
equal deleted inserted replaced
39:b5c6ebd33547 40:2cf26b593f4a
83 } 83 }
84 84
85 protected function gatherRelations() 85 protected function gatherRelations()
86 { 86 {
87 $relations = RelationshipNavigator::getRelations($this->getCurrentTable()); 87 $relations = RelationshipNavigator::getRelations($this->getCurrentTable());
88 foreach ($relations as $relation_type => $relation_values) {
89 $relations[$relation_type] = array_column($relation_values, null, 'column');
90 }
88 91
89 return $relations; 92 return $relations;
90
91 } 93 }
92 94
93 protected function renderColumns() 95 protected function renderColumns()
94 { 96 {
95 $insert = '';
96 $values = []; 97 $values = [];
98 $relations = $this->gatherRelations();
99
97 foreach ($this->get_columns() as $column) { 100 foreach ($this->get_columns() as $column) {
98 if (in_array($column['name'], $this->columns_to_ignore)) { 101 if (in_array($column['name'], $this->columns_to_ignore)) {
99 continue; 102 continue;
100 } 103 }
101 104
102 $type = $column['type_name']; 105 $value = $this->getFakeValue($column, $relations);
103 $nullable = ($column['nullable'] ? '->optional($weight = 0.5)' : '');
104 $value = 'fake()'.$nullable;
105 $name = $column['name'];
106
107 // Get the expected header name
108 $replacements = [
109 '{{value}}' => $value.'->text()',
110 '{{column_name}}' => $name,
111 ];
112
113 // date
114 if (in_array($type, ['date'])) {
115 $replacements['{{value}}'] = $value.'->date()';
116 }
117 // time
118 if (in_array($type, ['timestamp'])) {
119 $replacements['{{value}}'] = $value.'->timestamp()';
120 }
121 // checkbox
122 if (in_array($type, ['tinyint'])) {
123 $replacements['{{value}}'] = $value.'->numberBetween(0,1)';
124 }
125 // select
126 elseif (in_array($type, ['bigint']) && array_key_exists($name, $selects)) {
127 $replacements['{{header}}'] = Str::headline(Str::singular($selects[$name]));
128 $replacements['{{value}}'] = '{{ $item->'.Str::singular($selects[$name]).'?->name ?? "" }}';
129 }
130 // bigint, float
131 elseif (in_array($type, ['bigint', 'int'])) {
132 $replacements['{{value}}'] = $value.'->randomNumber()';
133 } elseif (in_array($type, ['float'])) {
134 $replacements['{{value}}'] = $value.'->randomFloat()';
135 } else {
136 // text area
137 // varchar, , etc
138 }
139
140 $snippet = $this->getSnippet('value'); 106 $snippet = $this->getSnippet('value');
141 // Replace placeholders with actual values
142 $values[] = str_replace(
143 array_keys($replacements),
144 $replacements,
145 $snippet
146 );
147 107
148 // Replace placeholders with actual values 108 // Replace placeholders with actual values
109 $values[] = str_replace(['{{value}}', '{{column_name}}'], [$value, $column['name']], $snippet);
149 } 110 }
150 $insert = implode("\n", $values);
151 111
152 return $insert; 112 return implode("\n", $values);
113 }
114
115 protected function getFakeValue($column, $relations)
116 {
117 $value = '$this->faker';
118 $nullable = ($column['nullable'] ? '->optional($weight = 0.5)' : '');
119
120 switch ($column['type_name']) {
121 case 'date':
122 return "$value$nullable->date()";
123 case 'timestamp':
124 return "$value$nullable->timestamp()";
125 case 'tinyint':
126 return "$value$nullable->numberBetween(0,1)";
127 case 'bigint':
128 if (isset($relations['belongsTo'][$column['name']])) {
129 $related = $this->getNamespacedModel($relations['belongsTo'][$column['name']]['table']);
130 return "$related::factory()";
131 }
132 // fall through to int for lack of better option
133 case 'int':
134 return "$value$nullable->randomNumber()";
135 case 'float':
136 return "$value$nullable->randomFloat()";
137 default:
138 return "$value$nullable->text()";
139 }
153 } 140 }
154 141
155 /** 142 /**
156 * Get available insertions including model relationships. 143 * Get available insertions including model relationships.
157 */ 144 */