diff --git a/src/Dacapo/Domain/ValueObject/Schema/ColumnType/BaseFloatType.php b/src/Dacapo/Domain/ValueObject/Schema/ColumnType/BaseFloatType.php index a4f4427e..3fe13fcd 100644 --- a/src/Dacapo/Domain/ValueObject/Schema/ColumnType/BaseFloatType.php +++ b/src/Dacapo/Domain/ValueObject/Schema/ColumnType/BaseFloatType.php @@ -16,11 +16,9 @@ public function __construct($args = null) $args = explode(',', $args); } - if (is_array($args) && count($args) === 2) { - $this->total = (int) $args[0]; - $this->places = (int) $args[1]; - } elseif (is_array($args) && count($args) === 1) { - $this->total = (int) $args[0]; + if (is_array($args)) { + $this->total = isset($args[0]) ? (int) $args[0] : null; + $this->places = isset($args[1]) ? (int) $args[1] : null; } elseif (is_int($args)) { $this->total = $args; } diff --git a/src/Dacapo/Domain/ValueObject/Schema/ColumnType/BaseIntegerType.php b/src/Dacapo/Domain/ValueObject/Schema/ColumnType/BaseIntegerType.php index 6ad57dac..870f6a90 100644 --- a/src/Dacapo/Domain/ValueObject/Schema/ColumnType/BaseIntegerType.php +++ b/src/Dacapo/Domain/ValueObject/Schema/ColumnType/BaseIntegerType.php @@ -12,12 +12,9 @@ abstract class BaseIntegerType implements ColumnType public function __construct($args = null) { - if (is_array($args) && count($args) === 2) { - $this->autoIncrement = (bool) $args[0]; - $this->unsigned = (bool) $args[1]; - } elseif (is_array($args) && count($args) === 1) { - $this->autoIncrement = (bool) $args[0]; - $this->unsigned = false; + if (is_array($args)) { + $this->autoIncrement = (bool) $args[0] ?? false; + $this->unsigned = (bool) $args[1] ?? false; } elseif (is_bool($args)) { $this->autoIncrement = $args; $this->unsigned = false; diff --git a/src/Dacapo/Domain/ValueObject/Schema/ColumnType/DecimalType.php b/src/Dacapo/Domain/ValueObject/Schema/ColumnType/DecimalType.php index ce0e04b8..6c281fb7 100644 --- a/src/Dacapo/Domain/ValueObject/Schema/ColumnType/DecimalType.php +++ b/src/Dacapo/Domain/ValueObject/Schema/ColumnType/DecimalType.php @@ -14,23 +14,20 @@ class DecimalType implements ColumnType public function __construct($args = null) { if (is_string($args) && $args !== '') { - $args = explode(',', $args); + $args = array_map('trim', explode(',', $args)); } - if (is_array($args) && count($args) === 3) { - $this->total = (int) $args[0]; - $this->places = (int) $args[1]; + if (is_array($args)) { + $this->total = isset($args[0]) ? (int) $args[0] : null; + $this->places = isset($args[1]) ? (int) $args[1] : null; - if (is_string($args[2])) { - $this->unsigned = (trim($args[2]) === 'true') ? true : false; - } else { - $this->unsigned = $args[2]; + if (isset($args[2])) { + if (is_string($args[2])) { + $this->unsigned = $args[2] === 'true'; + } else { + $this->unsigned = $args[2]; + } } - } elseif (is_array($args) && count($args) === 2) { - $this->total = (int) $args[0]; - $this->places = (int) $args[1]; - } elseif (is_array($args) && count($args) === 1) { - $this->total = (int) $args[0]; } elseif (is_int($args)) { $this->total = $args; } diff --git a/src/Dacapo/Domain/ValueObject/Schema/ColumnType/UnsignedDecimalType.php b/src/Dacapo/Domain/ValueObject/Schema/ColumnType/UnsignedDecimalType.php index 5e87de69..a0d5fc89 100644 --- a/src/Dacapo/Domain/ValueObject/Schema/ColumnType/UnsignedDecimalType.php +++ b/src/Dacapo/Domain/ValueObject/Schema/ColumnType/UnsignedDecimalType.php @@ -2,42 +2,13 @@ namespace UcanLab\LaravelDacapo\Dacapo\Domain\ValueObject\Schema\ColumnType; -use UcanLab\LaravelDacapo\Dacapo\Domain\ValueObject\Schema\ColumnName; -use UcanLab\LaravelDacapo\Dacapo\Domain\ValueObject\Schema\ColumnType; - -class UnsignedDecimalType implements ColumnType +class UnsignedDecimalType extends BaseFloatType { - protected ?int $total = null; - protected ?int $places = null; - - public function __construct($args = null) - { - if (is_string($args) && $args !== '') { - $args = explode(',', $args); - } - - if (is_array($args) && count($args) === 2) { - $this->total = (int) $args[0]; - $this->places = (int) $args[1]; - } elseif (is_array($args) && count($args) === 1) { - $this->total = (int) $args[0]; - } elseif (is_int($args)) { - $this->total = $args; - } - } - /** - * @param ColumnName $columnName * @return string */ - public function createMigrationMethod(ColumnName $columnName): string + protected function getName(): string { - if (is_int($this->total) && is_int($this->places)) { - return sprintf("->unsignedDecimal('%s', %d, %d)", $columnName->getName(), $this->total, $this->places); - } elseif (is_int($this->total) && is_int($this->places) === false) { - return sprintf("->unsignedDecimal('%s', %d)", $columnName->getName(), $this->total); - } - - return sprintf("->unsignedDecimal('%s')", $columnName->getName()); + return 'unsignedDecimal'; } } diff --git a/src/Providers/ConsoleServiceProvider.php b/src/Providers/ConsoleServiceProvider.php index 374996b7..eb0fa655 100644 --- a/src/Providers/ConsoleServiceProvider.php +++ b/src/Providers/ConsoleServiceProvider.php @@ -75,23 +75,22 @@ protected function registerBindings(): void $this->app->bind($abstract, $concrete); } - $driver = $this->getDatabaseDriver(); - - if (isset($this->databaseBuilders[$driver]) === false) { - throw new Exception(sprintf('driver %s is not found.', $driver)); - } - - $this->app->bind(DatabaseBuilder::class, $this->databaseBuilders[$driver]); + $this->app->bind(DatabaseBuilder::class, $this->concreteDatabaseBuilder()); } /** * @return string + * @throws Exception */ - protected function getDatabaseDriver(): string + protected function concreteDatabaseBuilder(): string { $connection = config('database.default'); $driver = config("database.connections.{$connection}.driver"); - return (string) $driver; + if (isset($this->databaseBuilders[$driver]) === false) { + throw new Exception(sprintf('driver %s is not found.', $driver)); + } + + return $this->databaseBuilders[$driver]; } }