2019-03-18 12:47:09 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Models;
|
|
|
|
|
2019-03-25 15:19:13 +01:00
|
|
|
/**
|
|
|
|
* This class should be extended to add functionalities to
|
|
|
|
* fetch, validate, transform and represent data entities.
|
|
|
|
*/
|
2019-03-18 12:47:09 +01:00
|
|
|
abstract class Model
|
|
|
|
{
|
|
|
|
|
2019-03-25 15:19:13 +01:00
|
|
|
/**
|
|
|
|
* Internal data of the model.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-03-18 12:47:09 +01:00
|
|
|
private $data;
|
|
|
|
|
|
|
|
|
2019-03-25 15:19:13 +01:00
|
|
|
/**
|
|
|
|
* Validate the received data structure to ensure if we can extract the
|
|
|
|
* values required to build the model.
|
|
|
|
*
|
|
|
|
* @param array $data Input data.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException If any input value is considered
|
|
|
|
* invalid.
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
*/
|
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 15:19:13 +01:00
|
|
|
/**
|
|
|
|
* Returns a valid representation of the model.
|
|
|
|
*
|
|
|
|
* @param array $data Input data.
|
|
|
|
*
|
|
|
|
* @return array Data structure representing the model.
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
*/
|
2019-03-25 12:32:06 +01:00
|
|
|
abstract protected function decode(array $data): array;
|
2019-03-18 12:47:09 +01:00
|
|
|
|
|
|
|
|
2019-06-13 14:51:27 +02:00
|
|
|
/**
|
|
|
|
* Return a valid representation of a record in database.
|
|
|
|
*
|
|
|
|
* @param array $data Input data.
|
|
|
|
*
|
|
|
|
* @return array Data structure representing a record in database.
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
*/
|
2022-01-13 12:16:39 +01:00
|
|
|
abstract protected static function encode(array $data): array;
|
2019-06-13 14:51:27 +02:00
|
|
|
|
|
|
|
|
2022-01-13 11:43:33 +01:00
|
|
|
/**
|
|
|
|
* Inserts a new item model 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 static function create(array $data=[]): int
|
|
|
|
{
|
|
|
|
// Insert.
|
|
|
|
$save = static::encode($data);
|
|
|
|
|
|
|
|
$result = \db_process_sql_insert('tlayout_data', $save);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-13 14:51:27 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2019-06-18 17:28:36 +02:00
|
|
|
abstract public function save(array $data=[]);
|
2019-06-13 14:51:27 +02:00
|
|
|
|
|
|
|
|
2019-07-25 17:58:37 +02:00
|
|
|
/**
|
|
|
|
* Delete an item in the database
|
|
|
|
*
|
|
|
|
* @param integer $itemId Identifier of the Item.
|
|
|
|
*
|
|
|
|
* @return boolean The modeled element data structure stored into the DB.
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
*/
|
|
|
|
abstract public function delete(int $itemId): bool;
|
|
|
|
|
|
|
|
|
2019-03-25 15:19:13 +01:00
|
|
|
/**
|
|
|
|
* Constructor of the model. It won't be public. The instances
|
|
|
|
* will be created through factories which start with from*.
|
|
|
|
*
|
|
|
|
* @param array $unknownData Input data structure.
|
|
|
|
*/
|
|
|
|
protected function __construct(array $unknownData)
|
2019-03-18 12:47:09 +01:00
|
|
|
{
|
|
|
|
$this->validateData($unknownData);
|
|
|
|
$this->data = $this->decode($unknownData);
|
2019-03-26 11:45:50 +01:00
|
|
|
// Sort alphabetically.
|
2019-04-03 17:45:56 +02:00
|
|
|
ksort($this->data, (SORT_NATURAL | SORT_FLAG_CASE));
|
2019-03-18 12:47:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-12 20:46:19 +01:00
|
|
|
/**
|
|
|
|
* Set data.
|
|
|
|
*
|
|
|
|
* @param array $data Data.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-06-18 17:28:36 +02:00
|
|
|
public function setData(array $data)
|
|
|
|
{
|
|
|
|
$this->data = $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-25 12:32:06 +01:00
|
|
|
/**
|
2019-03-25 15:19:13 +01:00
|
|
|
* Instance the class with the unknown input data.
|
2019-03-25 12:32:06 +01:00
|
|
|
*
|
|
|
|
* @param array $data Unknown data structure.
|
|
|
|
*
|
2019-03-25 15:19:13 +01:00
|
|
|
* @return self Instance of the model.
|
2019-03-25 12:32:06 +01:00
|
|
|
*/
|
2019-06-18 10:13:56 +02:00
|
|
|
public static function fromArray(array $data): self
|
2019-03-25 12:32:06 +01:00
|
|
|
{
|
|
|
|
// 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.
|
|
|
|
*
|
2022-01-12 20:46:19 +01:00
|
|
|
* @param array $filter Filter to retrieve the modeled element.
|
|
|
|
* @param float|null $ratio Ratio.
|
|
|
|
* @param float|null $widthRatio Width ratio.
|
2019-03-25 12:32:06 +01:00
|
|
|
*
|
|
|
|
* @return array The modeled element data structure stored into the DB.
|
|
|
|
* @throws \Exception When the data cannot be retrieved from the DB.
|
2019-03-25 15:19:13 +01:00
|
|
|
*
|
|
|
|
* @abstract
|
2019-03-25 12:32:06 +01:00
|
|
|
*/
|
2020-03-26 12:29:38 +01:00
|
|
|
abstract protected static function fetchDataFromDB(
|
|
|
|
array $filter,
|
2021-06-29 12:02:13 +02:00
|
|
|
?float $ratio=0,
|
|
|
|
?float $widthRatio=0
|
2020-03-26 12:29:38 +01:00
|
|
|
);
|
2019-03-25 12:32:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtain a model's instance from the database using a filter.
|
|
|
|
*
|
2022-01-12 20:46:19 +01:00
|
|
|
* @param array $filter Filter to retrieve the modeled element.
|
|
|
|
* @param float|null $ratio Ratio.
|
|
|
|
* @param float|null $widthRatio Width ratio.
|
2019-03-25 12:32:06 +01:00
|
|
|
*
|
|
|
|
* @return self A modeled element's instance.
|
|
|
|
*/
|
2021-06-29 12:02:13 +02:00
|
|
|
public static function fromDB(array $filter, ?float $ratio=0, ?float $widthRatio=0): self
|
2019-03-25 12:32:06 +01:00
|
|
|
{
|
|
|
|
// The reserved word static refers to the invoked class at runtime.
|
2021-06-29 12:02:13 +02:00
|
|
|
return static::fromArray(static::fetchDataFromDB($filter, $ratio, $widthRatio));
|
2019-03-25 12:32:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-12 09:50:39 +02:00
|
|
|
/**
|
|
|
|
* JSON representation of the model.
|
|
|
|
*
|
2022-01-12 20:46:19 +01:00
|
|
|
* @return array
|
2019-04-12 09:50:39 +02:00
|
|
|
*/
|
|
|
|
public function toArray(): array
|
|
|
|
{
|
|
|
|
return $this->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-21 17:47:10 +01:00
|
|
|
/**
|
2019-03-25 15:19:13 +01:00
|
|
|
* JSON representation of the model.
|
2019-03-21 17:47:10 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-03-18 12:47:09 +01:00
|
|
|
public function toJson(): string
|
|
|
|
{
|
2019-08-08 11:05:21 +02:00
|
|
|
return json_encode($this->data);
|
2019-03-18 12:47:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-21 17:47:10 +01:00
|
|
|
/**
|
2019-03-25 15:19:13 +01:00
|
|
|
* Text representation of the model.
|
2019-03-21 17:47:10 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-03-18 12:47:09 +01:00
|
|
|
public function __toString(): string
|
|
|
|
{
|
|
|
|
return $this->toJson();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-15 18:12:01 +01:00
|
|
|
/**
|
|
|
|
* Calculate ratio for mobile.
|
|
|
|
*
|
|
|
|
* @param array $size Size viewport.
|
|
|
|
* @param string $mode Mode calculate (dashboard or mobile).
|
|
|
|
*
|
|
|
|
* @return float Ratio.
|
|
|
|
*/
|
|
|
|
public function adjustToViewport($size, $mode='')
|
|
|
|
{
|
2022-05-13 12:54:58 +02:00
|
|
|
global $config;
|
2022-03-15 18:12:01 +01:00
|
|
|
$ratio_visualconsole = $this->getRatio();
|
|
|
|
$ratio_w = ($size['width'] / $this->data['width']);
|
|
|
|
$ratio_h = ($size['height'] / $this->data['height']);
|
2022-06-15 19:50:19 +02:00
|
|
|
$acum_height = $this->data['height'];
|
|
|
|
$acum_width = $this->data['width'];
|
2022-03-15 18:12:01 +01:00
|
|
|
|
|
|
|
$this->data['width'] = $size['width'];
|
|
|
|
$this->data['height'] = ($size['width'] * $ratio_visualconsole);
|
|
|
|
|
|
|
|
$ratio = $ratio_w;
|
|
|
|
if ($mode === 'mobile') {
|
2022-05-13 12:54:58 +02:00
|
|
|
if ((bool) $config['mobile_view_orientation_vc'] === true) {
|
|
|
|
if ($this->data['height'] < $this->data['width']) {
|
|
|
|
if ($this->data['height'] > $size['height']) {
|
|
|
|
$ratio = $ratio_h;
|
|
|
|
$this->data['height'] = $size['height'];
|
|
|
|
$this->data['width'] = ($size['height'] / $ratio_visualconsole);
|
|
|
|
}
|
2022-06-15 19:50:19 +02:00
|
|
|
} else {
|
|
|
|
$ratio = $ratio_w;
|
|
|
|
$height = (($acum_height * ($size['width'])) / $acum_width);
|
|
|
|
$this->data['height'] = $height;
|
|
|
|
$this->data['width'] = ($height / $ratio_visualconsole);
|
2022-05-13 12:54:58 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($this->data['height'] > $this->data['width']) {
|
2022-03-15 18:12:01 +01:00
|
|
|
$ratio = $ratio_h;
|
|
|
|
$this->data['height'] = $size['height'];
|
|
|
|
$this->data['width'] = ($size['height'] / $ratio_visualconsole);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($this->data['height'] > $size['height']) {
|
|
|
|
$ratio = $ratio_h;
|
|
|
|
$this->data['height'] = $size['height'];
|
|
|
|
$this->data['width'] = ($size['height'] / $ratio_visualconsole);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ratio;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate ratio
|
|
|
|
*
|
|
|
|
* @return float Ratio.
|
|
|
|
*/
|
|
|
|
public function getRatio()
|
|
|
|
{
|
|
|
|
if (isset($this->data['width']) === false
|
|
|
|
|| empty($this->data['width']) === true
|
|
|
|
) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ($this->data['height'] / $this->data['width']);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-25 15:19:13 +01:00
|
|
|
/*
|
|
|
|
* -------------
|
|
|
|
* - UTILITIES -
|
|
|
|
* -------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2019-03-21 17:47:10 +01:00
|
|
|
/**
|
2019-03-25 15:19:13 +01:00
|
|
|
* From a unknown value, it will try to extract a valid boolean value.
|
2019-03-21 17:47:10 +01:00
|
|
|
*
|
2019-03-25 15:19:13 +01:00
|
|
|
* @param mixed $value Unknown input.
|
2019-03-21 17:47:10 +01:00
|
|
|
*
|
2019-03-25 15:19:13 +01:00
|
|
|
* @return boolean Valid boolean value.
|
2019-03-21 17:47:10 +01:00
|
|
|
*/
|
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
|
|
|
/**
|
2019-03-25 15:19:13 +01:00
|
|
|
* Return a not empty string or a default value from a unknown value.
|
2019-03-21 17:47:10 +01:00
|
|
|
*
|
2019-03-25 15:19:13 +01:00
|
|
|
* @param mixed $val Input value.
|
|
|
|
* @param mixed $def Default value.
|
2019-03-21 17:47:10 +01:00
|
|
|
*
|
2019-03-25 15:19:13 +01:00
|
|
|
* @return mixed A valid string (not empty) extracted from the input
|
|
|
|
* or the default value.
|
2019-03-21 17:47:10 +01:00
|
|
|
*/
|
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
|
|
|
/**
|
2019-03-25 15:19:13 +01:00
|
|
|
* Return a valid integer or a default value from a unknown value.
|
2019-03-21 17:47:10 +01:00
|
|
|
*
|
2019-03-25 15:19:13 +01:00
|
|
|
* @param mixed $val Input value.
|
|
|
|
* @param mixed $def Default value.
|
2019-03-21 17:47:10 +01:00
|
|
|
*
|
2019-03-25 15:19:13 +01:00
|
|
|
* @return mixed A valid int extracted from the input or the default value.
|
2019-03-21 17:47:10 +01:00
|
|
|
*/
|
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-04-04 17:02:48 +02:00
|
|
|
/**
|
|
|
|
* Return a valid float or a default value from a unknown value.
|
|
|
|
*
|
|
|
|
* @param mixed $val Input value.
|
|
|
|
* @param mixed $def Default value.
|
|
|
|
*
|
|
|
|
* @return mixed A valid float extracted from the input or the
|
|
|
|
* default value.
|
|
|
|
*/
|
|
|
|
protected static function parseFloatOr($val, $def)
|
|
|
|
{
|
|
|
|
return (is_numeric($val) === true) ? (float) $val : $def;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-21 17:47:10 +01:00
|
|
|
/**
|
2019-03-25 15:19:13 +01:00
|
|
|
* Get a value from a dictionary from a possible pool of keys.
|
2019-03-21 17:47:10 +01:00
|
|
|
*
|
2019-03-25 15:19:13 +01:00
|
|
|
* @param array $dict Input array.
|
|
|
|
* @param array $keys Possible keys.
|
2019-03-21 17:47:10 +01:00
|
|
|
*
|
2019-03-25 15:19:13 +01:00
|
|
|
* @return mixed The first value found with the pool of keys or null.
|
2019-03-21 17:47:10 +01:00
|
|
|
*/
|
2019-03-25 15:19:13 +01:00
|
|
|
protected static function issetInArray(array $dict, 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 15:19:13 +01:00
|
|
|
if (isset($dict[$value]) === true) {
|
|
|
|
return $dict[$value];
|
2019-03-19 12:03:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2019-03-18 12:47:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|