Skip to content

Commit

Permalink
Merge pull request #125 from ucan-lab/topic-refactor
Browse files Browse the repository at this point in the history
[4.0]Minor adjustment
  • Loading branch information
ucan-lab authored Jan 23, 2021
2 parents fa1df58 + 9ce3e76 commit 5f87e31
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 10 additions & 13 deletions src/Dacapo/Domain/ValueObject/Schema/ColumnType/DecimalType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
17 changes: 8 additions & 9 deletions src/Providers/ConsoleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}

0 comments on commit 5f87e31

Please sign in to comment.