Skip to content

Commit d524e70

Browse files
committed
Add better support for enums.
1 parent 4660d61 commit d524e70

File tree

1 file changed

+56
-25
lines changed

1 file changed

+56
-25
lines changed

src/Console/GenerateCommand.php

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ protected function getPropertiesFromTable($model)
243243
$name !== $model::UPDATED_AT
244244
) {
245245
if (!method_exists($model, 'getDeletedAtColumn') || (method_exists($model, 'getDeletedAtColumn') && $name !== $model->getDeletedAtColumn())) {
246-
$this->setProperty($name, $type, $table);
246+
$this->setProperty($model, $name, $type, $table);
247247
}
248248
}
249249
}
@@ -277,7 +277,7 @@ protected function getPropertiesFromMethods($model)
277277
if ($pos = stripos($code, $search)) {
278278
$relationObj = $model->$method();
279279
if ($relationObj instanceof Relation) {
280-
$this->setProperty($relationObj->getForeignKeyName(), 'factory(' . get_class($relationObj->getRelated()) . '::class)');
280+
$this->setProperty($model, $relationObj->getForeignKeyName(), 'factory(' . get_class($relationObj->getRelated()) . '::class)');
281281
}
282282
}
283283
}
@@ -289,31 +289,14 @@ protected function getPropertiesFromMethods($model)
289289
* @param string $name
290290
* @param string|null $type
291291
*/
292-
protected function setProperty($name, $type = null, $table = null)
292+
protected function setProperty($model, $name, $type = null, $table = null)
293293
{
294294
if ($type !== null && Str::startsWith($type, 'factory(')) {
295295
$this->properties[$name] = $type;
296296

297297
return;
298298
}
299299

300-
$fakeableTypes = [
301-
'enum' => '$faker->randomElement(' . $this->enumValues($table, $name) . ')',
302-
'string' => '$faker->word',
303-
'text' => '$faker->text',
304-
'date' => '$faker->date()',
305-
'time' => '$faker->time()',
306-
'guid' => '$faker->word',
307-
'datetimetz' => '$faker->dateTime()',
308-
'datetime' => '$faker->dateTime()',
309-
'integer' => '$faker->randomNumber()',
310-
'bigint' => '$faker->randomNumber()',
311-
'smallint' => '$faker->randomNumber()',
312-
'decimal' => '$faker->randomFloat()',
313-
'float' => '$faker->randomFloat()',
314-
'boolean' => '$faker->boolean'
315-
];
316-
317300
$fakeableNames = [
318301
'city' => '$faker->city',
319302
'company' => '$faker->company',
@@ -354,6 +337,31 @@ protected function setProperty($name, $type = null, $table = null)
354337
return;
355338
}
356339

340+
$enumValues = $this->enumValues($model, $table, $name);
341+
342+
$fakeableTypes = [
343+
'enum' => '$faker->randomElement(' . $enumValues . ')',
344+
'string' => '$faker->word',
345+
'text' => '$faker->text',
346+
'date' => '$faker->date()',
347+
'time' => '$faker->time()',
348+
'guid' => '$faker->word',
349+
'datetimetz' => '$faker->dateTime()',
350+
'datetime' => '$faker->dateTime()',
351+
'integer' => '$faker->randomNumber()',
352+
'bigint' => '$faker->randomNumber()',
353+
'smallint' => '$faker->randomNumber()',
354+
'decimal' => '$faker->randomFloat()',
355+
'float' => '$faker->randomFloat()',
356+
'boolean' => '$faker->boolean'
357+
];
358+
359+
if ($enumValues !== '[]') {
360+
$this->properties[$name] = $fakeableTypes['enum'];
361+
362+
return;
363+
}
364+
357365
if (isset($fakeableTypes[$type])) {
358366
$this->properties[$name] = $fakeableTypes[$type];
359367

@@ -363,19 +371,42 @@ protected function setProperty($name, $type = null, $table = null)
363371
$this->properties[$name] = '$faker->word';
364372
}
365373

366-
public static function enumValues($table, $name)
374+
public function enumValues($model, $table, $name)
367375
{
368376
if ($table === null) {
369377
return "[]";
370378
}
371379

372-
$type = DB::select(DB::raw('SHOW COLUMNS FROM ' . $table . ' WHERE Field = "' . $name . '"'))[0]->Type;
380+
$driver = $model->getConnection()->getDriverName();
381+
$values = null;
382+
383+
if ($driver === 'mysql') {
384+
$type = DB::select(DB::raw('SHOW COLUMNS FROM ' . $table . ' WHERE Field = "' . $name . '"'))[0]->Type;
373385

374-
preg_match_all("/'([^']+)'/", $type, $matches);
386+
preg_match_all("/'([^']+)'/", $type, $matches);
375387

376-
$values = isset($matches[1]) ? $matches[1] : array();
388+
$values = isset($matches[1]) ? $matches[1] : array();
389+
390+
return "['" . implode("', '", $values) . "']";
391+
} else if ($driver === 'pgsql') {
392+
$types = DB::select(DB::raw("
393+
select matches[1]
394+
from pg_constraint, regexp_matches(consrc, '''(.+?)''', 'g') matches
395+
where contype = 'c'
396+
and conname = '{$table}_{$name}_check'
397+
and conrelid = 'public.{$table}'::regclass;
398+
"));
399+
400+
$values = array();
401+
402+
foreach ($types as $type){
403+
$values[] = $type->matches;
404+
}
405+
}
377406

378-
return "['" . implode("', '", $values) . "']";
407+
return $values
408+
? "['" . implode("', '", $values) . "']"
409+
: "[]";
379410
}
380411

381412

0 commit comments

Comments
 (0)