mirror of
https://github.com/pandorafms/pandorafms.git
synced 2025-07-28 00:04:37 +02:00
Added Group, Icon and line
Former-commit-id: d13c4cdfd03f299812c588aa41685296d35675f9
This commit is contained in:
parent
d3fe2908d6
commit
205c95bcb8
@ -67,7 +67,7 @@ abstract class Model
|
||||
*
|
||||
* @return self Instance of the model.
|
||||
*/
|
||||
public static function fromArray(array $data): self
|
||||
public static function fromArray(array $data)
|
||||
{
|
||||
// The reserved word static refers to the invoked class at runtime.
|
||||
return new static($data);
|
||||
@ -84,7 +84,7 @@ abstract class Model
|
||||
*
|
||||
* @abstract
|
||||
*/
|
||||
abstract protected static function fetchDataFromDB(array $filter): array;
|
||||
abstract protected static function fetchDataFromDB(array $filter);
|
||||
|
||||
|
||||
/**
|
||||
|
@ -19,6 +19,15 @@ final class Container extends Model
|
||||
* @param array $data Input data.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function fromArray(array $data)
|
||||
{
|
||||
return new self($data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Validate the input data
|
||||
*
|
||||
* @throws \InvalidArgumentException If any input value is considered
|
||||
* invalid.
|
||||
@ -179,7 +188,7 @@ final class Container extends Model
|
||||
*
|
||||
* @override Model::fetchDataFromDB.
|
||||
*/
|
||||
public static function fetchDataFromDB(array $filter): self
|
||||
public static function fetchDataFromDB(array $filter)
|
||||
{
|
||||
// Due to this DB call, this function cannot be unit tested without
|
||||
// a proper mock.
|
||||
|
@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Models\VisualConsole\items;
|
||||
use Models\VisualConsole\Item;
|
||||
use Models\Model;
|
||||
|
||||
final class Group extends Item
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Validate the input data.
|
||||
*
|
||||
* @param mixed $data
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @override
|
||||
*/
|
||||
protected function validateData(array $data): void
|
||||
{
|
||||
parent::validateData($data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns a valid data structure.
|
||||
*
|
||||
* @param mixed $data
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @override
|
||||
*/
|
||||
protected function decode(array $data): array
|
||||
{
|
||||
$return = parent::decode($data);
|
||||
$return['type'] = GROUP_ITEM;
|
||||
$return['imageSrc'] = $this->extractImageSrc($data);
|
||||
$return['groupId'] = $this->extractGroupId($data);
|
||||
return $return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* extractBackgroundUrl
|
||||
*
|
||||
* @param mixed $data
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function extractImageSrc(array $data)
|
||||
{
|
||||
$imageSrc = Model::notEmptyStringOr(
|
||||
Model::issetInArray($data, ['imageSrc', 'image']),
|
||||
null
|
||||
);
|
||||
if ($imageSrc === null) {
|
||||
throw new \InvalidArgumentException(
|
||||
'the imageSrc property is required and should be string'
|
||||
);
|
||||
} else {
|
||||
return $imageSrc;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private function extractGroupId(array $data): int
|
||||
{
|
||||
$groupId = Model::parseIntOr(
|
||||
Model::issetInArray($data, ['groupId', 'id_group']),
|
||||
0
|
||||
);
|
||||
if ($groupId === null) {
|
||||
throw new \InvalidArgumentException(
|
||||
'the group Id property is required and should be integer'
|
||||
);
|
||||
} else {
|
||||
return $groupId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Models\VisualConsole\items;
|
||||
use Models\VisualConsole\Item;
|
||||
use Models\Model;
|
||||
|
||||
final class Icon extends Item
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Validate the input data.
|
||||
*
|
||||
* @param mixed $data
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @override
|
||||
*/
|
||||
protected function validateData(array $data): void
|
||||
{
|
||||
parent::validateData($data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns a valid data structure.
|
||||
*
|
||||
* @param mixed $data
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @override
|
||||
*/
|
||||
protected function decode(array $data): array
|
||||
{
|
||||
$return = parent::decode($data);
|
||||
$return['type'] = ICON;
|
||||
$return['imageSrc'] = $this->extractImageSrc($data);
|
||||
return $return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* extractBackgroundUrl
|
||||
*
|
||||
* @param mixed $data
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function extractImageSrc(array $data)
|
||||
{
|
||||
$imageSrc = Model::notEmptyStringOr(
|
||||
Model::issetInArray($data, ['imageSrc', 'image']),
|
||||
null
|
||||
);
|
||||
if ($imageSrc === null) {
|
||||
throw new \InvalidArgumentException(
|
||||
'the imageSrc property is required and should be string'
|
||||
);
|
||||
} else {
|
||||
return $imageSrc;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,215 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Models\VisualConsole\items;
|
||||
use Models\Model;
|
||||
|
||||
final class Line extends Model
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Validate the input data
|
||||
*
|
||||
* @param mixed $data
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function validateData(array $data): void
|
||||
{
|
||||
if (isset($data['id']) === false
|
||||
|| \is_numeric($data['id']) === false
|
||||
) {
|
||||
throw new \InvalidArgumentException(
|
||||
'the Id property is required and should be integer'
|
||||
);
|
||||
}
|
||||
|
||||
if (isset($data['type']) === false
|
||||
|| \is_numeric($data['type']) === false
|
||||
) {
|
||||
throw new \InvalidArgumentException(
|
||||
'the Id property is required and should be integer'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns a valid data structure.
|
||||
*
|
||||
* @param mixed $data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function decode(array $data): array
|
||||
{
|
||||
return [
|
||||
'id' => (int) $data['id'],
|
||||
'type' => LINE_ITEM,
|
||||
'startX' => $this->extractStartX($data),
|
||||
'startY' => $this->extractStartY($data),
|
||||
'endX' => $this->extractEndX($data),
|
||||
'endY' => $this->extractEndY($data),
|
||||
'isOnTop' => $this->extractIsOnTope($data),
|
||||
'borderWidth' => $this->extractBorderWidth($data),
|
||||
'borderColor' => $this->extractBorderColor($data),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Extract the value of startX and
|
||||
* return a integer.
|
||||
*
|
||||
* @param mixed $data
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
private function extractStartX(array $data): int
|
||||
{
|
||||
$startX = Model::parseIntOr(
|
||||
Model::issetInArray($data, ['startX', 'pos_x']),
|
||||
0
|
||||
);
|
||||
return $startX;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Extract the value of startY and
|
||||
* return a integer.
|
||||
*
|
||||
* @param mixed $data
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
private function extractStartY(array $data): int
|
||||
{
|
||||
$startY = Model::parseIntOr(
|
||||
Model::issetInArray($data, ['startY', 'pos_y']),
|
||||
0
|
||||
);
|
||||
return $startY;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Extract the value of endX and
|
||||
* return a integer.
|
||||
*
|
||||
* @param mixed $data
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
private function extractEndX(array $data): int
|
||||
{
|
||||
$endX = Model::parseIntOr(
|
||||
Model::issetInArray($data, ['endX', 'width']),
|
||||
0
|
||||
);
|
||||
return $endX;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Extract the value of endY and
|
||||
* return a integer.
|
||||
*
|
||||
* @param mixed $data
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
private function extractEndY(array $data): int
|
||||
{
|
||||
$endY = Model::parseIntOr(
|
||||
Model::issetInArray($data, ['endY', 'height']),
|
||||
0
|
||||
);
|
||||
return $endY;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Extract the value of isOnTop and
|
||||
* return a bool.
|
||||
*
|
||||
* @param mixed $data
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
private function extractIsOnTope(array $data): bool
|
||||
{
|
||||
$isOnTop = Model::parseBool(
|
||||
Model::issetInArray($data, ['isOnTop', 'show_on_top'])
|
||||
);
|
||||
return $isOnTop;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Extract the value of borderWidth and
|
||||
* return a integer.
|
||||
*
|
||||
* @param mixed $data
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
private function extractBorderWidth(array $data): int
|
||||
{
|
||||
$borderWidth = Model::parseIntOr(
|
||||
Model::issetInArray($data, ['borderWidth', 'border_width']),
|
||||
0
|
||||
);
|
||||
if ($borderWidth >= 0) {
|
||||
return $borderWidth;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Extract the value of borderColor and
|
||||
* return to not empty string or null.
|
||||
*
|
||||
* @param mixed $data
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function extractBorderColor(array $data)
|
||||
{
|
||||
$borderColor = Model::notEmptyStringOr(
|
||||
Model::issetInArray($data, ['borderColor', 'border_color']),
|
||||
null
|
||||
);
|
||||
return $borderColor;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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 line 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Models\VisualConsole\items\Group;
|
||||
|
||||
/**
|
||||
* Test class
|
||||
*/
|
||||
class GroupTest extends TestCase
|
||||
{
|
||||
|
||||
|
||||
public function testCanBeCreatedFromValidUserStructure(): void
|
||||
{
|
||||
$this->assertInstanceOf(
|
||||
Group::class,
|
||||
Group::fromArray(
|
||||
[
|
||||
'id' => 13,
|
||||
'type' => GROUP_ITEM,
|
||||
'width' => '600',
|
||||
'height' => '500',
|
||||
'imageSrc' => 'image.jpg',
|
||||
'groupId' => 12,
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
Group::class,
|
||||
Group::fromArray(
|
||||
[
|
||||
'id' => 1004,
|
||||
'type' => GROUP_ITEM,
|
||||
'width' => '600',
|
||||
'height' => '500',
|
||||
'image' => 'test_image.png',
|
||||
'id_group' => 0,
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public function testCannotBeCreatedWithInvalidImageSrc(): void
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
// Invalid imageSrc.
|
||||
Group::fromArray(
|
||||
[
|
||||
'id' => 7,
|
||||
'type' => GROUP_ITEM,
|
||||
'label' => null,
|
||||
'labelPosition' => 'up',
|
||||
'isLinkEnabled' => true,
|
||||
'isOnTop' => false,
|
||||
'parentId' => null,
|
||||
'width' => '0',
|
||||
'height' => '0',
|
||||
'x' => -666,
|
||||
'y' => 76,
|
||||
'imageSrc' => '',
|
||||
'id_group' => 0,
|
||||
]
|
||||
);
|
||||
// Missing imageSrc.
|
||||
Group::fromArray(
|
||||
[
|
||||
'id' => 7,
|
||||
'type' => GROUP_ITEM,
|
||||
'label' => null,
|
||||
'labelPosition' => 'up',
|
||||
'isLinkEnabled' => true,
|
||||
'isOnTop' => false,
|
||||
'parentId' => null,
|
||||
'width' => '0',
|
||||
'height' => '0',
|
||||
'x' => -666,
|
||||
'y' => 76,
|
||||
'id_group' => 0,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public function testCannotBeCreatedWithInvalidGroupId(): void
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
// Invalid groupId.
|
||||
Group::fromArray(
|
||||
[
|
||||
'id' => 7,
|
||||
'type' => GROUP_ITEM,
|
||||
'label' => null,
|
||||
'labelPosition' => 'up',
|
||||
'isLinkEnabled' => true,
|
||||
'isOnTop' => false,
|
||||
'parentId' => null,
|
||||
'width' => '0',
|
||||
'height' => '0',
|
||||
'x' => -666,
|
||||
'y' => 76,
|
||||
'imageSrc' => 'test.jpg',
|
||||
'groupId' => 'bar',
|
||||
]
|
||||
);
|
||||
// Missing groupId.
|
||||
Group::fromArray(
|
||||
[
|
||||
'id' => 7,
|
||||
'type' => GROUP_ITEM,
|
||||
'label' => null,
|
||||
'labelPosition' => 'up',
|
||||
'isLinkEnabled' => true,
|
||||
'isOnTop' => false,
|
||||
'parentId' => null,
|
||||
'width' => '0',
|
||||
'height' => '0',
|
||||
'x' => -666,
|
||||
'y' => 76,
|
||||
'imageSrc' => 'test.jpg',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public function testContainerIsRepresentedAsJson(): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
'{"id":1,"type"11,"label":null,"labelPosition":"down","isLinkEnabled":false,"isOnTop":false,"parentId":null,"aclGroupId":null,"width":0,"height":0,"x":0,"y":0,"imageSrc":"image.jpg","groupId":0}',
|
||||
Group::fromArray(
|
||||
[
|
||||
'id' => 1,
|
||||
'type' => GROUP_ITEM,
|
||||
'width' => '0',
|
||||
'height' => '0',
|
||||
'imageSrc' => 'image.jpg',
|
||||
'groupId' => 0,
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Models\VisualConsole\items\Icon;
|
||||
|
||||
/**
|
||||
* Test class
|
||||
*/
|
||||
class IconTest extends TestCase
|
||||
{
|
||||
|
||||
|
||||
public function testCanBeCreatedFromValidUserStructure(): void
|
||||
{
|
||||
$this->assertInstanceOf(
|
||||
Icon::class,
|
||||
Icon::fromArray(
|
||||
[
|
||||
'id' => 69,
|
||||
'type' => ICON,
|
||||
'width' => '0',
|
||||
'height' => '0',
|
||||
'imageSrc' => 'image.jpg',
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public function testCannotBeCreatedWithInvalidImageSrc(): void
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
// Invalid imageSrc.
|
||||
Icon::fromArray(
|
||||
[
|
||||
'id' => 7,
|
||||
'type' => ICON,
|
||||
'label' => null,
|
||||
'labelPosition' => 'up',
|
||||
'isLinkEnabled' => true,
|
||||
'isOnTop' => false,
|
||||
'parentId' => null,
|
||||
'width' => '0',
|
||||
'height' => '0',
|
||||
'x' => -666,
|
||||
'y' => 76,
|
||||
'imageSrc' => '',
|
||||
]
|
||||
);
|
||||
// Missing imageSrc.
|
||||
Icon::fromArray(
|
||||
[
|
||||
'id' => 7,
|
||||
'type' => ICON,
|
||||
'label' => null,
|
||||
'labelPosition' => 'up',
|
||||
'isLinkEnabled' => true,
|
||||
'isOnTop' => false,
|
||||
'parentId' => null,
|
||||
'width' => '0',
|
||||
'height' => '0',
|
||||
'x' => -666,
|
||||
'y' => 76,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public function testContainerIsRepresentedAsJson(): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
'{"id":7,"type":5,"label":null,"labelPosition":"up","isLinkEnabled":true,"isOnTop":false,"parentId":null,"aclGroupId":null,"width":0,"height":0,"x":-666,"y":76,"imageSrc":"image.jpg"}',
|
||||
Icon::fromArray(
|
||||
[
|
||||
'id' => 7,
|
||||
'type' => ICON,
|
||||
'label' => null,
|
||||
'labelPosition' => 'up',
|
||||
'isLinkEnabled' => true,
|
||||
'isOnTop' => false,
|
||||
'parentId' => null,
|
||||
'width' => '0',
|
||||
'height' => '0',
|
||||
'x' => -666,
|
||||
'y' => 76,
|
||||
'imageSrc' => 'image.jpg',
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Models\VisualConsole\items\Line;
|
||||
|
||||
/**
|
||||
* Test class
|
||||
*/
|
||||
class LineTest extends TestCase
|
||||
{
|
||||
|
||||
|
||||
public function testCanBeCreatedFromValidUserStructure(): void
|
||||
{
|
||||
$this->assertInstanceOf(
|
||||
Line::class,
|
||||
Line::fromArray(
|
||||
[
|
||||
'id' => 10,
|
||||
'type' => LINE_ITEM,
|
||||
'startX' => 50,
|
||||
'startY' => 100,
|
||||
'endX' => 0,
|
||||
'endY' => 10,
|
||||
'isOnTop' => false,
|
||||
'borderWidth' => 0,
|
||||
'borderColor' => 'white',
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
Line::class,
|
||||
Line::fromArray(
|
||||
[
|
||||
'id' => 10,
|
||||
'type' => LINE_ITEM,
|
||||
'startX' => 50,
|
||||
'endY' => 10,
|
||||
'borderColor' => 'black',
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public function testCannotBeCreatedWithInvalidId(): void
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
// Invalid id.
|
||||
Line::fromArray(
|
||||
[
|
||||
'id' => 'foo',
|
||||
'type' => LINE_ITEM,
|
||||
'startX' => 50,
|
||||
'startY' => 100,
|
||||
'endX' => 0,
|
||||
'endY' => 10,
|
||||
'isOnTop' => false,
|
||||
'borderWidth' => 0,
|
||||
'borderColor' => 'white',
|
||||
]
|
||||
);
|
||||
// Missing id.
|
||||
Line::fromArray(
|
||||
[
|
||||
'type' => LINE_ITEM,
|
||||
'startX' => 50,
|
||||
'startY' => 100,
|
||||
'endX' => 0,
|
||||
'endY' => 10,
|
||||
'isOnTop' => false,
|
||||
'borderWidth' => 0,
|
||||
'borderColor' => 'white',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public function testCannotBeCreatedWithInvalidtype(): void
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
// Invalid type.
|
||||
Line::fromArray(
|
||||
[
|
||||
'id' => 13,
|
||||
'type' => 'test',
|
||||
'startX' => 50,
|
||||
'startY' => 100,
|
||||
'endX' => 0,
|
||||
'endY' => 10,
|
||||
'isOnTop' => false,
|
||||
'borderWidth' => 0,
|
||||
'borderColor' => 'white',
|
||||
]
|
||||
);
|
||||
// Missing type.
|
||||
Line::fromArray(
|
||||
[
|
||||
'id' => 13,
|
||||
'startX' => 50,
|
||||
'startY' => 100,
|
||||
'endX' => 0,
|
||||
'endY' => 10,
|
||||
'isOnTop' => true,
|
||||
'borderWidth' => 0,
|
||||
'borderColor' => 'white',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public function testContainerIsRepresentedAsJson(): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
'{"id":1,"type":13,"startX":50,"startY":100,"endX":0,"endY":10,"isOnTop":false,"borderWidth":0,"borderColor":"white"}',
|
||||
Line::fromArray(
|
||||
[
|
||||
'id' => 1,
|
||||
'type' => LINE_ITEM,
|
||||
'startX' => 50,
|
||||
'startY' => 100,
|
||||
'endX' => 0,
|
||||
'endY' => 10,
|
||||
'isOnTop' => false,
|
||||
'borderWidth' => 0,
|
||||
'borderColor' => 'white',
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user