2019-03-18 12:47:09 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Models;
|
|
|
|
|
|
|
|
abstract class Model
|
|
|
|
{
|
|
|
|
|
|
|
|
private $data;
|
|
|
|
|
|
|
|
|
2019-03-25 12:32:06 +01:00
|
|
|
abstract protected function validateData(array $data): void;
|
2019-03-18 12:47:09 +01:00
|
|
|
|
|
|
|
|
2019-03-25 12:32:06 +01:00
|
|
|
abstract protected function decode(array $data): array;
|
2019-03-18 12:47:09 +01:00
|
|
|
|
|
|
|
|
2019-03-21 17:47:10 +01:00
|
|
|
public function __construct(array $unknownData)
|
2019-03-18 12:47:09 +01:00
|
|
|
{
|
|
|
|
$this->validateData($unknownData);
|
|
|
|
$this->data = $this->decode($unknownData);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-25 12:32:06 +01:00
|
|
|
/**
|
|
|
|
* Instance the class with the input data.
|
|
|
|
*
|
|
|
|
* @param array $data Unknown data structure.
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public static function fromArray(array $data): self
|
|
|
|
{
|
|
|
|
// The reserved word static refers to the invoked class at runtime.
|
|
|
|
return new static($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtain a data structure from the database using a filter.
|
|
|
|
*
|
|
|
|
* @param array $filter Filter to retrieve the modeled element.
|
|
|
|
*
|
|
|
|
* @return array The modeled element data structure stored into the DB.
|
|
|
|
* @throws \Exception When the data cannot be retrieved from the DB.
|
|
|
|
*/
|
|
|
|
abstract protected static function fetchDataFromDB(array $filter): array;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtain a model's instance from the database using a filter.
|
|
|
|
*
|
|
|
|
* @param array $filter Filter to retrieve the modeled element.
|
|
|
|
*
|
|
|
|
* @return self A modeled element's instance.
|
|
|
|
*/
|
|
|
|
public static function fromDB(array $filter): self
|
|
|
|
{
|
|
|
|
// The reserved word static refers to the invoked class at runtime.
|
|
|
|
return static::fromArray(static::fetchDataFromDB($filter));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-21 17:47:10 +01:00
|
|
|
/**
|
|
|
|
* Returns the JSON representation of the given value.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-03-18 12:47:09 +01:00
|
|
|
public function toJson(): string
|
|
|
|
{
|
|
|
|
return \json_encode($this->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-21 17:47:10 +01:00
|
|
|
/**
|
|
|
|
* Returns the text representation of this class.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-03-18 12:47:09 +01:00
|
|
|
public function __toString(): string
|
|
|
|
{
|
|
|
|
return $this->toJson();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-21 17:47:10 +01:00
|
|
|
/**
|
|
|
|
* Returns a Boolean of a mixed value.
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2019-03-19 12:03:02 +01:00
|
|
|
protected static function parseBool($value): bool
|
2019-03-18 12:47:09 +01:00
|
|
|
{
|
|
|
|
if (\is_bool($value) === true) {
|
|
|
|
return $value;
|
|
|
|
} else if (\is_numeric($value) === true) {
|
|
|
|
return $value > 0;
|
|
|
|
} else if (\is_string($value) === true) {
|
|
|
|
return $value === '1' || $value === 'true';
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-21 17:47:10 +01:00
|
|
|
/**
|
|
|
|
* Return a not empty string or a default value from a mixed value.
|
|
|
|
*
|
|
|
|
* @param mixed $val
|
|
|
|
* @param mixed $def Default value to use if we cannot extract a non empty string.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2019-03-19 12:03:02 +01:00
|
|
|
protected static function notEmptyStringOr($val, $def)
|
|
|
|
{
|
|
|
|
return (\is_string($val) === true && strlen($val) > 0) ? $val : $def;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-21 17:47:10 +01:00
|
|
|
/**
|
|
|
|
* Return a integer or a default value from a mixed value.
|
|
|
|
*
|
|
|
|
* @param mixed $val
|
|
|
|
* @param mixed $def
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2019-03-20 16:31:29 +01:00
|
|
|
protected static function parseIntOr($val, $def)
|
|
|
|
{
|
2019-03-25 12:32:06 +01:00
|
|
|
return (is_numeric($val) === true) ? (int) $val : $def;
|
2019-03-20 16:31:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-21 17:47:10 +01:00
|
|
|
/**
|
|
|
|
* Returns the value if it exists in the array
|
|
|
|
*
|
|
|
|
* @param array $val input array
|
|
|
|
* @param array $keys array with the keys to search
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2019-03-19 12:03:02 +01:00
|
|
|
protected static function issetInArray(array $val, array $keys)
|
2019-03-18 12:47:09 +01:00
|
|
|
{
|
2019-03-19 12:03:02 +01:00
|
|
|
foreach ($keys as $key => $value) {
|
2019-03-25 12:32:06 +01:00
|
|
|
if (isset($val[$value]) === true) {
|
2019-03-19 12:03:02 +01:00
|
|
|
return $val[$value];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2019-03-18 12:47:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|