Skip to content

Better support for enums. #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 58 additions & 25 deletions src/Console/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ protected function getPropertiesFromTable($model)
$name !== $model::UPDATED_AT
) {
if (!method_exists($model, 'getDeletedAtColumn') || (method_exists($model, 'getDeletedAtColumn') && $name !== $model->getDeletedAtColumn())) {
$this->setProperty($name, $type, $table);
$this->setProperty($model, $name, $type, $table);
}
}
}
Expand Down Expand Up @@ -277,7 +277,7 @@ protected function getPropertiesFromMethods($model)
if ($pos = stripos($code, $search)) {
$relationObj = $model->$method();
if ($relationObj instanceof Relation) {
$this->setProperty($relationObj->getForeignKeyName(), 'factory(' . get_class($relationObj->getRelated()) . '::class)');
$this->setProperty($model, $relationObj->getForeignKeyName(), 'factory(' . get_class($relationObj->getRelated()) . '::class)');
}
}
}
Expand All @@ -289,31 +289,14 @@ protected function getPropertiesFromMethods($model)
* @param string $name
* @param string|null $type
*/
protected function setProperty($name, $type = null, $table = null)
protected function setProperty($model, $name, $type = null, $table = null)
{
if ($type !== null && Str::startsWith($type, 'factory(')) {
$this->properties[$name] = $type;

return;
}

$fakeableTypes = [
'enum' => '$faker->randomElement(' . $this->enumValues($table, $name) . ')',
'string' => '$faker->word',
'text' => '$faker->text',
'date' => '$faker->date()',
'time' => '$faker->time()',
'guid' => '$faker->word',
'datetimetz' => '$faker->dateTime()',
'datetime' => '$faker->dateTime()',
'integer' => '$faker->randomNumber()',
'bigint' => '$faker->randomNumber()',
'smallint' => '$faker->randomNumber()',
'decimal' => '$faker->randomFloat()',
'float' => '$faker->randomFloat()',
'boolean' => '$faker->boolean'
];

$fakeableNames = [
'city' => '$faker->city',
'company' => '$faker->company',
Expand Down Expand Up @@ -354,6 +337,31 @@ protected function setProperty($name, $type = null, $table = null)
return;
}

$enumValues = $this->enumValues($model, $table, $name);

$fakeableTypes = [
'enum' => '$faker->randomElement(' . $enumValues . ')',
'string' => '$faker->word',
'text' => '$faker->text',
'date' => '$faker->date()',
'time' => '$faker->time()',
'guid' => '$faker->word',
'datetimetz' => '$faker->dateTime()',
'datetime' => '$faker->dateTime()',
'integer' => '$faker->randomNumber()',
'bigint' => '$faker->randomNumber()',
'smallint' => '$faker->randomNumber()',
'decimal' => '$faker->randomFloat()',
'float' => '$faker->randomFloat()',
'boolean' => '$faker->boolean'
];

if ($enumValues !== '[]') {
$this->properties[$name] = $fakeableTypes['enum'];

return;
}

if (isset($fakeableTypes[$type])) {
$this->properties[$name] = $fakeableTypes[$type];

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

public static function enumValues($table, $name)
public function enumValues($model, $table, $name)
{
if ($table === null) {
return "[]";
}

$type = DB::select(DB::raw('SHOW COLUMNS FROM ' . $table . ' WHERE Field = "' . $name . '"'))[0]->Type;
$driver = $model->getConnection()->getDriverName();
$values = null;

if ($driver === 'mysql') {
$type = DB::connection($model->getConnectionName())
->select(DB::raw('SHOW COLUMNS FROM ' . $table . ' WHERE Field = "' . $name . '"'))[0]->Type;

preg_match_all("/'([^']+)'/", $type, $matches);
preg_match_all("/'([^']+)'/", $type, $matches);

$values = isset($matches[1]) ? $matches[1] : array();
$values = isset($matches[1]) ? $matches[1] : null;
} else if ($driver === 'pgsql') {
$types = DB::connection($model->getConnectionName())
->select(DB::raw("
select matches[1]
from pg_constraint, regexp_matches(pg_get_constraintdef(\"oid\"), '''(.+?)''', 'g') matches
where contype = 'c'
and conname = '{$table}_{$name}_check'
and conrelid = 'public.{$table}'::regclass;
"));

if (count($types)) {
$values = array();

foreach ($types as $type){
$values[] = $type->matches;
}
}
}

return "['" . implode("', '", $values) . "']";
return $values
? "['" . implode("', '", $values) . "']"
: "[]";
}


Expand Down