This commit is contained in:
Daniel Maya 2019-06-13 14:51:27 +02:00
parent e332c4ffba
commit e9b4dea6c5
4 changed files with 109 additions and 15 deletions

View File

@ -47,6 +47,30 @@ abstract class Model
abstract protected function decode(array $data): array; abstract protected function decode(array $data): array;
/**
* Return a valid representation of a record in database.
*
* @param array $data Input data.
*
* @return array Data structure representing a record in database.
*
* @abstract
*/
abstract protected function encode(array $data): array;
/**
* Insert or update an item in the database
*
* @param array $data Unknown input data structure.
*
* @return boolean The modeled element data structure stored into the DB.
*
* @abstract
*/
abstract public function save(array $data=[]): bool;
/** /**
* Constructor of the model. It won't be public. The instances * Constructor of the model. It won't be public. The instances
* will be created through factories which start with from*. * will be created through factories which start with from*.

View File

@ -92,6 +92,37 @@ final class Container extends Model
} }
/**
* Return a valid representation of a record in database.
*
* @param array $data Input data.
*
* @return array Data structure representing a record in database.
*
* @overrides Model::encode.
*/
protected function encode(array $data): array
{
$result = [];
return $result;
}
/**
* Insert or update an item in the database
*
* @param array $data Unknown input data structure.
*
* @return boolean The modeled element data structure stored into the DB.
*
* @overrides Model::save.
*/
public function save(array $data=[]): bool
{
return true;
}
/** /**
* Extract a group Id value. * Extract a group Id value.
* *
@ -365,6 +396,7 @@ final class Container extends Model
try { try {
array_push($items, $class::fromDB($data)); array_push($items, $class::fromDB($data));
hd($class::save($data), true);
} catch (\Throwable $e) { } catch (\Throwable $e) {
// TODO: Log this? // TODO: Log this?
} }

View File

@ -1212,17 +1212,19 @@ class Item extends CachedModel
* @param array $data Input data. * @param array $data Input data.
* *
* @return array Data structure representing a record in database. * @return array Data structure representing a record in database.
*
* @overrides Model::encode.
*/ */
protected function encode(array $data): array protected function encode(array $data): array
{ {
$result = []; $result = [];
$id = static::extractId($data); $id = static::getId($data);
if ($id) { if ($id) {
$result['id'] = $id; $result['id'] = $id;
} }
$id_layout = static::extractIdLayout($data); $id_layout = static::getIdLayout($data);
if ($id_layout) { if ($id_layout) {
$result['id_layout'] = $id_layout; $result['id_layout'] = $id_layout;
} }
@ -1243,12 +1245,12 @@ class Item extends CachedModel
$result['pos_y'] = $pos_y; $result['pos_y'] = $pos_y;
} }
$height = static::extractHeight($data); $height = static::getHeight($data);
if ($height !== null) { if ($height !== null) {
$result['height'] = $height; $result['height'] = $height;
} }
$width = static::extractWidth($data); $width = static::getWidth($data);
if ($width !== null) { if ($width !== null) {
$result['width'] = $width; $result['width'] = $width;
} }
@ -1258,7 +1260,7 @@ class Item extends CachedModel
$result['label'] = $label; $result['label'] = $label;
} }
$image = static::extractImageSrc($data); $image = static::getImageSrc($data);
if ($image !== null) { if ($image !== null) {
$result['image'] = $image; $result['image'] = $image;
} }
@ -1322,12 +1324,12 @@ class Item extends CachedModel
$result['id_custom_graph'] = $id_custom_graph; $result['id_custom_graph'] = $id_custom_graph;
} }
$border_width = static::extractBorderWidth($data); $border_width = static::getBorderWidth($data);
if ($border_width !== null) { if ($border_width !== null) {
$result['border_width'] = $border_width; $result['border_width'] = $border_width;
} }
$type_graph = static::extractTypeGraph($data); $type_graph = static::getTypeGraph($data);
if ($type_graph !== null) { if ($type_graph !== null) {
$result['type_graph'] = $type_graph; $result['type_graph'] = $type_graph;
} }
@ -1477,7 +1479,7 @@ class Item extends CachedModel
* *
* @return integer Item id. 0 by default. * @return integer Item id. 0 by default.
*/ */
private static function extractId(array $data): integer private static function getId(array $data): int
{ {
return static::parseIntOr( return static::parseIntOr(
static::issetInArray($data, ['id', 'itemId']), static::issetInArray($data, ['id', 'itemId']),
@ -1493,7 +1495,7 @@ class Item extends CachedModel
* *
* @return integer Item id. 0 by default. * @return integer Item id. 0 by default.
*/ */
private static function extractIdLayout(array $data): integer private static function getIdLayout(array $data): int
{ {
return static::parseIntOr( return static::parseIntOr(
static::issetInArray($data, ['id_layout', 'idLayout', 'layoutId']), static::issetInArray($data, ['id_layout', 'idLayout', 'layoutId']),
@ -1509,7 +1511,7 @@ class Item extends CachedModel
* *
* @return integer Item width. 0 by default. * @return integer Item width. 0 by default.
*/ */
private static function extractWidth(array $data): integer private static function getWidth(array $data)
{ {
return static::parseIntOr( return static::parseIntOr(
static::issetInArray($data, ['width', 'endX']), static::issetInArray($data, ['width', 'endX']),
@ -1525,7 +1527,7 @@ class Item extends CachedModel
* *
* @return integer Item height. 0 by default. * @return integer Item height. 0 by default.
*/ */
private static function extractHeight(array $data): integer private static function getHeight(array $data)
{ {
return static::parseIntOr( return static::parseIntOr(
static::issetInArray($data, ['height', 'endY']), static::issetInArray($data, ['height', 'endY']),
@ -1541,7 +1543,7 @@ class Item extends CachedModel
* *
* @return mixed String representing the image url (not empty) or null. * @return mixed String representing the image url (not empty) or null.
*/ */
private static function extractImageSrc(array $data): string protected static function getImageSrc(array $data)
{ {
$imageSrc = static::notEmptyStringOr( $imageSrc = static::notEmptyStringOr(
static::issetInArray($data, ['imageSrc', 'image', 'backgroundColor', 'backgroundType', 'valueType']), static::issetInArray($data, ['imageSrc', 'image', 'backgroundColor', 'backgroundType', 'valueType']),
@ -1559,7 +1561,7 @@ class Item extends CachedModel
* *
* @return integer Valid border width. * @return integer Valid border width.
*/ */
private static function extractBorderWidth(array $data) private static function getBorderWidth(array $data)
{ {
return static::parseIntOr( return static::parseIntOr(
static::issetInArray($data, ['border_width', 'borderWidth']), static::issetInArray($data, ['border_width', 'borderWidth']),
@ -1575,7 +1577,7 @@ class Item extends CachedModel
* *
* @return string One of 'vertical' or 'horizontal'. 'vertical' by default. * @return string One of 'vertical' or 'horizontal'. 'vertical' by default.
*/ */
private static function extractTypeGraph(array $data): string private static function getTypeGraph(array $data)
{ {
return static::notEmptyStringOr( return static::notEmptyStringOr(
static::issetInArray($data, ['typeGraph', 'type_graph', 'graphType']), static::issetInArray($data, ['typeGraph', 'type_graph', 'graphType']),
@ -1622,8 +1624,10 @@ class Item extends CachedModel
* @param array $data Unknown input data structure. * @param array $data Unknown input data structure.
* *
* @return boolean The modeled element data structure stored into the DB. * @return boolean The modeled element data structure stored into the DB.
*
* @overrides Model::save.
*/ */
public function save(array $data=[]): boolean public function save(array $data=[]): bool
{ {
$save = static::encode($data); $save = static::encode($data);
@ -1637,6 +1641,9 @@ class Item extends CachedModel
// static = static::fromDB(['id' => $save['id']]); // static = static::fromDB(['id' => $save['id']]);
} }
if ($result) {
}
return $result; return $result;
} }

View File

@ -206,4 +206,35 @@ final class Line extends Model
} }
/**
* Return a valid representation of a record in database.
*
* @param array $data Input data.
*
* @return array Data structure representing a record in database.
*
* @overrides Model::encode.
*/
protected function encode(array $data): array
{
$result = [];
return $result;
}
/**
* Insert or update an item in the database
*
* @param array $data Unknown input data structure.
*
* @return boolean The modeled element data structure stored into the DB.
*
* @overrides Model::save.
*/
public function save(array $data=[]): bool
{
return true;
}
} }