Skip to content

Commit

Permalink
feat: manage tables and fields as objects (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
noelma committed Dec 4, 2021
1 parent aeadaad commit 69c36ac
Show file tree
Hide file tree
Showing 21 changed files with 1,473 additions and 856 deletions.
201 changes: 201 additions & 0 deletions src/Field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
<?php

declare(strict_types=1);

/**
* Queryflatfile
*
* @license https://github.com/soosyze/queryflatfile/blob/master/LICENSE (MIT License)
*/

namespace Queryflatfile;

use Queryflatfile\Exception\TableBuilder\ColumnsValueException;
use Queryflatfile\Exception\TableBuilder\TableBuilderException;

/**
* Pattern fluent pour la création et configuration des types de données.
*
* @author Mathieu NOËL <mathieu@soosyze.com>
*/
abstract class Field
{
public const OPT_CREATE = 'create';

public const OPT_DROP = 'drop';

public const OPT_MODIFY = 'modify';

public const OPT_RENAME = 'rename';

public const TYPE = '';

protected const INVALID_ARGUMENT_MESSAGE = 'The value of the %s field must be of type %s: %s given.';

/**
* @var bool|null|numeric|string
*/
protected $valueDefault;

/**
* @var string
*/
protected $name;

/**
* @var string
*/
protected $opt = self::OPT_CREATE;

/**
* @var null|string
*/
protected $comment = null;

/**
* @var bool
*/
protected $isNullable = false;

public function __construct(string $name)
{
$this->name = $name;
}

/**
* Enregistre un commentaire.
*
* @param string $comment
*
* @return $this
*/
public function comment(string $comment): self
{
$this->comment = $comment;

return $this;
}

/**
* Enregistre le champ comme acceptant la valeur NULL.
*
* @return $this
*/
public function nullable(): self
{
$this->isNullable = true;

return $this;
}

/**
* Enregistre une valeur par défaut au champ précédent.
* Lève une exception si la valeur par défaut ne correspond pas au type de valeur passée en paramètre.
*
* @param bool|null|numeric|string $value Valeur à tester.
*
* @throws ColumnsValueException
*
* @return bool|null|numeric|string
*/
abstract public function filterValue($value);

/**
* Enregistre une valeur par défaut au champ précédent.
* Lève une exception si la valeur par défaut ne correspond pas au type de valeur passée en paramètre.
*
* @param bool|null|numeric|string $value Valeur à tester.
*
* @throws TableBuilderException
*
* @return $this
*/
public function valueDefault($value)
{
$this->valueDefault = $this->filterValue($value);

return $this;
}

/**
* Retourne la valeur par defaut.
*
* @throws ColumnsValueException
*
* @return bool|null|numeric|string Valeur par defaut.
*/
public function getValueDefault()
{
if (isset($this->valueDefault)) {
return $this->valueDefault;
}
if ($this->isNullable) {
return null;
}

throw new ColumnsValueException(
sprintf('%s not nullable or not default.', $this->name)
);
}

/**
* Enregistre la modification du champ précédent.
*
* @return void
*/
public function modify(): void
{
$this->opt = self::OPT_MODIFY;
}

/**
* Retourne le nom de l'opération du champ.
*
* @return string
*/
public function getOpt(): string
{
return $this->opt;
}

/**
* Retourne le nom du champ.
*
* @return string
*/
public function getName(): string
{
return $this->name;
}

public function setName(string $name): self
{
$this->name = $name;

return $this;
}

/**
* Retourne les données du champ.
*
* @return array
*/
public function toArray(): array
{
$data = [];
if (static::TYPE !== '') {
$data[ 'type' ] = static::TYPE;
}
if ($this->isNullable) {
$data[ 'nullable' ] = $this->isNullable;
}
if ($this->comment !== null) {
$data[ '_comment' ] = $this->comment;
}
if (isset($this->valueDefault)) {
$data[ 'default' ] = $this->valueDefault;
}

return $data;
}
}
37 changes: 37 additions & 0 deletions src/Field/BoolType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/**
* Queryflatfile
*
* @license https://github.com/soosyze/queryflatfile/blob/master/LICENSE (MIT License)
*/

namespace Queryflatfile\Field;

use Queryflatfile\Field;

/**
* @author Mathieu NOËL <mathieu@soosyze.com>
*/
class BoolType extends Field
{
public const TYPE = 'boolean';

/**
* {@inheritdoc}
*
* return bool
*/
public function filterValue($value)
{
if (!\is_bool($value)) {
throw new \InvalidArgumentException(
sprintf(self::INVALID_ARGUMENT_MESSAGE, $this->name, self::TYPE, gettype($value))
);
}

return $value;
}
}
24 changes: 24 additions & 0 deletions src/Field/CharType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

/**
* Queryflatfile
*
* @license https://github.com/soosyze/queryflatfile/blob/master/LICENSE (MIT License)
*/

namespace Queryflatfile\Field;

/**
* @author Mathieu NOËL <mathieu@soosyze.com>
*/
class CharType extends StringType
{
public const TYPE = 'char';

/**
* @var int
*/
protected $length = 1;
}
72 changes: 72 additions & 0 deletions src/Field/DateTimeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

/**
* Queryflatfile
*
* @license https://github.com/soosyze/queryflatfile/blob/master/LICENSE (MIT License)
*/

namespace Queryflatfile\Field;

use Queryflatfile\Exception\TableBuilder\ColumnsValueException;
use Queryflatfile\Field;

/**
* @author Mathieu NOËL <mathieu@soosyze.com>
*/
class DateTimeType extends Field
{
public const CURRENT_DEFAULT = 'current_datetime';

public const TYPE = 'datetime';

protected const FORMAT = 'Y-m-d H:i:s';

/**
* {@inheritdoc}
*
* return string
*/
public function filterValue($value)
{
if (!\is_string($value)) {
throw new \InvalidArgumentException(
sprintf(self::INVALID_ARGUMENT_MESSAGE, $this->name, 'string', gettype($value))
);
}
if (strtolower($value) === static::CURRENT_DEFAULT) {
return static::CURRENT_DEFAULT;
}
if (($timestamp = strtotime($value))) {
return date(static::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()
{
if (isset($this->valueDefault)) {
if ($this->valueDefault === static::CURRENT_DEFAULT) {
return date(static::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)
);
}
}
23 changes: 23 additions & 0 deletions src/Field/DateType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

/**
* Queryflatfile
*
* @license https://github.com/soosyze/queryflatfile/blob/master/LICENSE (MIT License)
*/

namespace Queryflatfile\Field;

/**
* @author Mathieu NOËL <mathieu@soosyze.com>
*/
class DateType extends DateTimeType
{
public const CURRENT_DEFAULT = 'current_date';

public const TYPE = 'date';

protected const FORMAT = 'Y-m-d';
}
37 changes: 37 additions & 0 deletions src/Field/DropType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/**
* Queryflatfile
*
* @license https://github.com/soosyze/queryflatfile/blob/master/LICENSE (MIT License)
*/

namespace Queryflatfile\Field;

use Queryflatfile\Field;

/**
* @author Mathieu NOËL <mathieu@soosyze.com>
*/
class DropType extends Field
{
protected $opt = self::OPT_DROP;

public function filterValue($value)
{
return null;
}

/**
* {@inheritdoc}
*/
public function toArray(): array
{
$data = parent::toArray();
$data[ 'opt' ] = $this->opt;

return $data;
}
}
Loading

0 comments on commit 69c36ac

Please sign in to comment.