-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rafactor(fields): simplifies polymorphism and uses enumerations
- Loading branch information
Showing
29 changed files
with
616 additions
and
441 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* @license https://github.com/soosyze/queryflatfile/blob/master/LICENSE (MIT License) | ||
*/ | ||
namespace Soosyze\Queryflatfile; | ||
|
||
/** | ||
* @author Mathieu NOËL <[email protected]> | ||
*/ | ||
class Command | ||
{ | ||
public function __construct(readonly public string $name) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @license https://github.com/soosyze/queryflatfile/blob/master/LICENSE (MIT License) | ||
*/ | ||
|
||
namespace Soosyze\Queryflatfile\Command; | ||
|
||
use Soosyze\Queryflatfile\Command; | ||
use Soosyze\Queryflatfile\Enum\TableExecutionType; | ||
|
||
/** | ||
* @author Mathieu NOËL <[email protected]> | ||
*/ | ||
final class DropCommand extends Command | ||
{ | ||
public function __construct(readonly public string $name) | ||
{ | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getExecutionType(): TableExecutionType | ||
{ | ||
return TableExecutionType::Drop; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @license https://github.com/soosyze/queryflatfile/blob/master/LICENSE (MIT License) | ||
*/ | ||
|
||
namespace Soosyze\Queryflatfile\Command; | ||
|
||
use Soosyze\Queryflatfile\Command; | ||
use Soosyze\Queryflatfile\Enum\TableExecutionType; | ||
|
||
/** | ||
* @author Mathieu NOËL <[email protected]> | ||
*/ | ||
final class RenameCommand extends Command | ||
{ | ||
public function __construct(public readonly string $name, public readonly string $to) | ||
{ | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getExecutionType(): TableExecutionType | ||
{ | ||
return TableExecutionType::Rename; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @license https://github.com/soosyze/queryflatfile/blob/master/LICENSE (MIT License) | ||
*/ | ||
|
||
namespace Soosyze\Queryflatfile\Concern\Field; | ||
|
||
/** | ||
* @property string $name | ||
* | ||
* @author Mathieu NOËL <[email protected]> | ||
*/ | ||
trait ThrowInvalidType | ||
{ | ||
/** | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function throwInvalidType(mixed $value): never | ||
{ | ||
throw new \InvalidArgumentException( | ||
sprintf( | ||
'The value of the %s field must be of type %s: %s given.', | ||
$this->name, | ||
$this->getType()->realType(), | ||
gettype($value) | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @license https://github.com/soosyze/queryflatfile/blob/master/LICENSE (MIT License) | ||
*/ | ||
|
||
namespace Soosyze\Queryflatfile\Concern\Field; | ||
|
||
use Soosyze\Queryflatfile\Enum\CurentDefaultType; | ||
use Soosyze\Queryflatfile\Exception\TableBuilder\ColumnsValueException; | ||
|
||
/** | ||
* @property string $name | ||
* | ||
* @author Mathieu NOËL <[email protected]> | ||
*/ | ||
trait TryOrGetDate | ||
{ | ||
use ThrowInvalidType; | ||
|
||
private CurentDefaultType $currentDefault; | ||
|
||
public function tryOrGetValue(mixed $value): string | ||
{ | ||
if (!\is_string($value)) { | ||
$this->throwInvalidType($value); | ||
} | ||
|
||
if (strtolower($value) === $this->currentDefault->value) { | ||
return $this->currentDefault->value; | ||
} | ||
if (($timestamp = strtotime($value))) { | ||
return date($this->currentDefault->format(), $timestamp); | ||
} | ||
|
||
throw new ColumnsValueException( | ||
sprintf('The value of the %s field must be a valid date: %s given', $this->name, $value) | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getValueDefault(): bool|float|int|string|null | ||
{ | ||
if ($this->valueDefault !== null) { | ||
if ($this->valueDefault === $this->currentDefault->value) { | ||
return date($this->currentDefault->format(), time()); | ||
} | ||
|
||
/* Si les variables magiques ne sont pas utilisé alors la vrais valeur par defaut est retourné. */ | ||
return $this->valueDefault; | ||
} | ||
if ($this->isNullable) { | ||
return null; | ||
} | ||
|
||
throw new ColumnsValueException( | ||
sprintf('%s not nullable or not default.', $this->name) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @license https://github.com/soosyze/queryflatfile/blob/master/LICENSE (MIT License) | ||
*/ | ||
|
||
namespace Soosyze\Queryflatfile\Concern\Field; | ||
|
||
/** | ||
* @property string $name | ||
* @property int $length | ||
* | ||
* @author Mathieu NOËL <[email protected]> | ||
*/ | ||
trait TryOrGetString | ||
{ | ||
use ThrowInvalidType; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function tryOrGetValue(mixed $value): string | ||
{ | ||
if (!\is_string($value)) { | ||
$this->throwInvalidType($value); | ||
} | ||
|
||
if (strlen($value) > $this->length) { | ||
throw new \LengthException( | ||
sprintf( | ||
'The value of the %s field must be less than or equal to %s characters: %s given', | ||
$this->name, | ||
$this->length, | ||
strlen($value) | ||
) | ||
); | ||
} | ||
|
||
return $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @license https://github.com/soosyze/queryflatfile/blob/master/LICENSE (MIT License) | ||
*/ | ||
|
||
namespace Soosyze\Queryflatfile\Enum; | ||
|
||
/** | ||
* @author Mathieu NOËL <[email protected]> | ||
*/ | ||
enum CurentDefaultType: string | ||
{ | ||
case Date = 'current_date'; | ||
case DateTime = 'current_datetime'; | ||
|
||
public function format(): string | ||
{ | ||
return match ($this) { | ||
self::Date => 'Y-m-d', | ||
self::DateTime => 'Y-m-d H:i:s', | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @license https://github.com/soosyze/queryflatfile/blob/master/LICENSE (MIT License) | ||
*/ | ||
|
||
namespace Soosyze\Queryflatfile\Enum; | ||
|
||
/** | ||
* @author Mathieu NOËL <[email protected]> | ||
*/ | ||
enum FieldType: string | ||
{ | ||
case Boolean = 'boolean'; | ||
case Char = 'char'; | ||
case DateTime = 'datetime'; | ||
case Date = 'date'; | ||
case Float = 'float'; | ||
case Increment = 'increments'; | ||
case Int = 'integer'; | ||
case String = 'string'; | ||
case Text = 'text'; | ||
|
||
private const TEXT_TYPES = [self::Text, self::String, self::Char]; | ||
|
||
private const DATE_TYPES = [self::Date, self::DateTime, self::Int]; | ||
|
||
private const NUMBER_TYPES = [self::Float, self::Increment, self::Int]; | ||
|
||
public function realType(): string | ||
{ | ||
return match ($this) { | ||
self::Boolean => 'boolean', | ||
self::Char => 'string', | ||
self::DateTime => 'string', | ||
self::Date => 'string', | ||
self::Float => 'float', | ||
self::Int => 'integer', | ||
self::Increment => 'integer', | ||
self::String => 'string', | ||
self::Text => 'string', | ||
}; | ||
} | ||
|
||
public function isModify(FieldType $newType): bool | ||
{ | ||
return match ($this) { | ||
self::Boolean => in_array($newType, [self::Boolean, ...self::TEXT_TYPES]), | ||
self::Char => in_array($newType, self::TEXT_TYPES), | ||
self::DateTime => in_array($newType, self::DATE_TYPES), | ||
self::Date => in_array($newType, self::DATE_TYPES), | ||
self::Float => in_array($newType, self::NUMBER_TYPES), | ||
self::Int => in_array($newType, self::NUMBER_TYPES), | ||
self::Increment => in_array($newType, self::NUMBER_TYPES), | ||
self::String => in_array($newType, self::TEXT_TYPES), | ||
self::Text => in_array($newType, self::TEXT_TYPES), | ||
}; | ||
} | ||
} |
Oops, something went wrong.