Skip to content

Commit

Permalink
feat: manage tables and fields as objects
Browse files Browse the repository at this point in the history
  • Loading branch information
noelma committed Dec 1, 2021
1 parent aeadaad commit 7fa79f2
Show file tree
Hide file tree
Showing 21 changed files with 1,466 additions and 769 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 ERROR = 'The default value (%s) for column %s does not correspond to type %s.';

/**
* @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;
}
}
34 changes: 34 additions & 0 deletions src/Field/BoolType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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 BoolType extends Field
{
public const TYPE = 'boolean';

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

return $value;
}
}
63 changes: 63 additions & 0 deletions src/Field/CharType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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\Exception\TableBuilder\TableBuilderException;
use Queryflatfile\Field;

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

/**
* @var int
*/
private $length = 255;

public function __construct(string $name, int $length)
{
if ($length < 0) {
throw new TableBuilderException('The length passed in parameter is not of numeric type.');
}
parent::__construct($name);
$this->length = $length;
}

/**
* {@inheritdoc}
*/
public function filterValue($value)
{
if (!\is_string($value)) {
throw new ColumnsValueException(sprintf(self::ERROR, $value, $this->name, self::TYPE));
}
if (strlen($value) > $this->length) {
throw new ColumnsValueException('The default value is larger than the specified size.');
}

return $value;
}

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

return $data;
}
}
63 changes: 63 additions & 0 deletions src/Field/DateType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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 DateType extends Field
{
public const CURRENT_DATE_DEFAULT = 'current_date';

public const TYPE = 'date';

/**
* {@inheritdoc}
*/
public function filterValue($value)
{
if (!\is_string($value)) {
throw new ColumnsValueException(sprintf(self::ERROR, $value, $this->name, self::TYPE));
}
if (strtolower($value) === self::CURRENT_DATE_DEFAULT) {
return self::CURRENT_DATE_DEFAULT;
}
if ($timestamp = strtotime($value)) {
return date('Y-m-d', $timestamp);
}

throw new ColumnsValueException(sprintf(self::ERROR, $value, $this->name, self::TYPE));
}

/**
* {@inheritdoc}
*/
public function getValueDefault()
{
if (isset($this->valueDefault)) {
if ($this->valueDefault === self::CURRENT_DATE_DEFAULT) {
return date('Y-m-d', time());
}

return $this->valueDefault;
}
if ($this->isNullable) {
return null;
}

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

0 comments on commit 7fa79f2

Please sign in to comment.