Skip to content

Commit

Permalink
rafactor(fields): simplifies polymorphism and uses enumerations
Browse files Browse the repository at this point in the history
  • Loading branch information
noelma committed Jul 30, 2023
1 parent 79bf904 commit ad462c4
Show file tree
Hide file tree
Showing 29 changed files with 616 additions and 441 deletions.
17 changes: 17 additions & 0 deletions src/Command.php
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)
{
}
}
30 changes: 30 additions & 0 deletions src/Command/DropCommand.php
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;
}
}
30 changes: 30 additions & 0 deletions src/Command/RenameCommand.php
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;
}
}
32 changes: 32 additions & 0 deletions src/Concern/Field/ThrowInvalidType.php
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)
)
);
}
}
64 changes: 64 additions & 0 deletions src/Concern/Field/TryOrGetDate.php
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)
);
}
}
43 changes: 43 additions & 0 deletions src/Concern/Field/TryOrGetString.php
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;
}
}
26 changes: 26 additions & 0 deletions src/Enum/CurentDefaultType.php
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',
};
}
}
61 changes: 61 additions & 0 deletions src/Enum/FieldType.php
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),
};
}
}
Loading

0 comments on commit ad462c4

Please sign in to comment.