Visual Console Refactor: added DB fetching and fixed a context problem

Former-commit-id: 44889e60cb6f45ab8245da93746c4ca688de97a7
This commit is contained in:
Alejandro Gallardo Escobar 2019-03-25 12:32:06 +01:00
parent 282a816c01
commit f6f868ddfe
4 changed files with 122 additions and 33 deletions

View File

@ -10,10 +10,10 @@ abstract class Model
private $data;
protected abstract function validateData(array $data): void;
abstract protected function validateData(array $data): void;
protected abstract function decode(array $data): array;
abstract protected function decode(array $data): array;
public function __construct(array $unknownData)
@ -23,6 +23,45 @@ abstract class Model
}
/**
* 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));
}
/**
* Returns the JSON representation of the given value.
*
@ -90,7 +129,7 @@ abstract class Model
*/
protected static function parseIntOr($val, $def)
{
return is_numeric($val) ? (int) $val : $def;
return (is_numeric($val) === true) ? (int) $val : $def;
}
@ -105,7 +144,7 @@ abstract class Model
protected static function issetInArray(array $val, array $keys)
{
foreach ($keys as $key => $value) {
if (isset($val[$value])) {
if (isset($val[$value]) === true) {
return $val[$value];
}
}

View File

@ -142,4 +142,29 @@ final class Container extends Model
}
/**
* Obtain a container data structure from the database using a filter.
*
* @param array $filter Filter of the Visual Console.
*
* @return self A Visual Console Container instance.
* @throws \Exception When the data cannot be retrieved from the DB.
*
* @override Model::fetchDataFromDB.
*/
public static function fetchDataFromDB(array $filter): self
{
// Due to this DB call, this function cannot be unit tested without
// a proper mock.
$row = \db_get_row_filter('tlayout', $filter);
if ($row === false) {
throw new \Exception('error fetching the data from the DB');
}
// Return a new instance.
return new self($row);
}
}

View File

@ -9,19 +9,6 @@ class Item extends Model
{
/**
* Instance the class with the input data.
*
* @param mixed $data
*
* @return self
*/
public static function fromArray(array $data): self
{
return new self($data);
}
/**
* Validate the input data.
*
@ -264,4 +251,42 @@ class Item extends Model
}
/**
* Obtain a vc item data structure from the database using a filter.
*
* @param array $filter Filter of the Visual Console Item.
*
* @return array The Visual Console Item data structure stored into the DB.
* @throws \Exception When the data cannot be retrieved from the DB.
*
* @override Model::fetchDataFromDB.
*/
protected static function fetchDataFromDB(array $filter): array
{
// Due to this DB call, this function cannot be unit tested without
// a proper mock.
$row = \db_get_row_filter('tlayout_data', $filter);
if ($row === false) {
throw new \Exception('error fetching the data from the DB');
}
return $row;
}
/**
* Obtain a vc item instance from the database using an identifier.
*
* @param integer $id Identifier of the Visual Console Item.
*
* @return array The Visual Console Item data structure stored into the DB.
* @throws \Exception When the data cannot be retrieved from the DB.
*/
protected static function fromDBWithId(int $id): array
{
return static::fromDB(['id' => $id]);
}
}

View File

@ -3,7 +3,7 @@
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use Models\VisualConsole\items\Box as Vox;
use Models\VisualConsole\items\Box;
/**
* Test class
@ -15,8 +15,8 @@ class BoxTest extends TestCase
public function testCanBeCreatedFromValidUserStructure(): void
{
$this->assertInstanceOf(
Vox::class,
new Vox(
Box::class,
Box::fromArray(
[
'id' => 69,
'type' => 12,
@ -33,18 +33,18 @@ class BoxTest extends TestCase
)
);
// $this->assertInstanceOf(
// Vox::class,
// Vox::fromArray(
// [
// 'id' => 1000,
// 'type' => 8,
// 'name' => 'test',
// 'width' => 100,
// 'height' => 900,
// ]
// )
// );
$this->assertInstanceOf(
Box::class,
Box::fromArray(
[
'id' => 1000,
'type' => 8,
'name' => 'test',
'width' => 100,
'height' => 900,
]
)
);
}
@ -52,7 +52,7 @@ class BoxTest extends TestCase
{
$this->assertEquals(
'{"id":7,"type":12,"label":null,"labelPosition":"up","isLinkEnabled":true,"isOnTop":false,"parentId":null,"aclGroupId":null,"width":0,"height":0,"x":-666,"y":76,"borderWidth":0,"borderColor":null,"fillColor":null}',
new Vox(
Box::fromArray(
[
'id' => 7,
'type' => 10,