Skip to content

Commit

Permalink
Allow to generate correct values for decimal type (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDoctor0 authored May 14, 2024
2 parents 4281cc0 + 9b5a221 commit fed9260
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/Console/GenerateFactoryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ protected function getPropertiesFromTable(Model $model): void
$type = $column['type_name'];

if ($this->isFieldFakeable($field, $model)) {
$this->setProperty($model, $field, $type, $nullable);
$this->setProperty($model, $field, $type, $column, $nullable);
}
}
}
Expand Down Expand Up @@ -254,8 +254,21 @@ protected function getPropertiesFromMethods(Model $model): void
}
}

protected function setProperty(Model $model, string $field, string $type, bool $nullable = false): void
protected function setProperty(Model $model, string $field, string $type, array $column, bool $nullable = false): void
{
if ($type === 'decimal') {
$pattern = '/decimal\((?P<integer_digits>\d+),(?P<decimal_digits>\d+)\)/';
if (preg_match($pattern, $column['type'], $matches)) {
$integerDigits = $matches['integer_digits'];
$decimalDigits = $matches['decimal_digits'];
}

$maxNumber = pow(10, $integerDigits - $decimalDigits) - 1;
$this->properties[$field] = $this->fakerPrefix("randomFloat($decimalDigits, 0, $maxNumber)", $nullable);

return;
}

if ($enumValues = EnumValues::get($model, $field)) {
$enumValues = implode("', '", $enumValues);

Expand Down Expand Up @@ -405,7 +418,6 @@ protected function mapByType(string $field, bool $nullable = false): ?string
'bigint' => $this->fakerPrefix('randomNumber()', $nullable),
'smallint' => $this->fakerPrefix('randomNumber()', $nullable),
'tinyint' => $this->fakerPrefix('randomNumber(1)', $nullable),
'decimal' => $this->fakerPrefix('randomFloat()', $nullable),
'float' => $this->fakerPrefix('randomFloat()', $nullable),
'boolean' => $this->fakerPrefix('boolean', $nullable),
];
Expand Down

0 comments on commit fed9260

Please sign in to comment.