mirror of
https://github.com/pandorafms/pandorafms.git
synced 2025-07-28 08:14:38 +02:00
created item class and test
Former-commit-id: b3a0b052f3556c5bafdac492718f9c7c63f9e85b
This commit is contained in:
parent
53d00d3c89
commit
ca1b6f1ee4
@ -55,6 +55,18 @@ abstract class Model
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected static function parseIntOr($val, $def)
|
||||||
|
{
|
||||||
|
if (\is_integer($val)) {
|
||||||
|
return $val;
|
||||||
|
} else if ((\is_string($val) && strlen($val) > 0) && ($val === '0' || (int) $val != 0)) {
|
||||||
|
return (int) $val;
|
||||||
|
} else {
|
||||||
|
return $def;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
protected static function issetInArray(array $val, array $keys)
|
protected static function issetInArray(array $val, array $keys)
|
||||||
{
|
{
|
||||||
foreach ($keys as $key => $value) {
|
foreach ($keys as $key => $value) {
|
||||||
|
184
pandora_console/include/rest-api/models/VisualConsole/Item.php
Normal file
184
pandora_console/include/rest-api/models/VisualConsole/Item.php
Normal file
@ -0,0 +1,184 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Models\VisualConsole;
|
||||||
|
use Models\Model;
|
||||||
|
|
||||||
|
class Item extends Model
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
public static function fromArray(array $data): self
|
||||||
|
{
|
||||||
|
return new self($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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 Type property is required and should be integer'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($data['width']) === false
|
||||||
|
|| \is_numeric($data['width']) === false
|
||||||
|
|| $data['width'] < 0
|
||||||
|
) {
|
||||||
|
throw new \InvalidArgumentException(
|
||||||
|
'the width property is required and should greater than 0'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($data['height']) === false
|
||||||
|
|| \is_numeric($data['height']) === false
|
||||||
|
|| $data['height'] < 0
|
||||||
|
) {
|
||||||
|
throw new \InvalidArgumentException(
|
||||||
|
'the height property is required and should greater than 0'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected function decode(array $data): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => (int) $data['id'],
|
||||||
|
'type' => (int) $data['type'],
|
||||||
|
'label' => $this->extractLabel($data),
|
||||||
|
'labelPosition' => $this->extractLabelPosition($data),
|
||||||
|
'isLinkEnabled' => $this->extractIsLinkEnabled($data),
|
||||||
|
'isOnTop' => $this->extractIsOnTop($data),
|
||||||
|
'parentId' => $this->extractParentId($data),
|
||||||
|
'aclGroupId' => $this->extractAclGroupId($data),
|
||||||
|
'width' => (int) $data['width'],
|
||||||
|
'height' => (int) $data['height'],
|
||||||
|
'x' => $this->extractX($data),
|
||||||
|
'y' => $this->extractY($data),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private function extractX(array $data): int
|
||||||
|
{
|
||||||
|
if (isset($data['pos_x']) === true
|
||||||
|
&& \is_numeric($data['pos_x']) === true
|
||||||
|
) {
|
||||||
|
return $data['pos_x'];
|
||||||
|
} else if (isset($data['x']) === true
|
||||||
|
&& \is_numeric($data['x']) === true
|
||||||
|
) {
|
||||||
|
return $data['x'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private function extractY(array $data): int
|
||||||
|
{
|
||||||
|
if (isset($data['pos_y']) === true
|
||||||
|
&& \is_numeric($data['pos_y']) === true
|
||||||
|
) {
|
||||||
|
return $data['pos_y'];
|
||||||
|
} else if (isset($data['y']) === true
|
||||||
|
&& \is_numeric($data['y']) === true
|
||||||
|
) {
|
||||||
|
return $data['y'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private function extractAclGroupId(array $data)
|
||||||
|
{
|
||||||
|
$aclGroupId = Model::parseIntOr(
|
||||||
|
Model::issetInArray($data, ['aclGroupId', 'id_group']),
|
||||||
|
null
|
||||||
|
);
|
||||||
|
if ($aclGroupId >= 0) {
|
||||||
|
return $aclGroupId;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private function extractParentId(array $data)
|
||||||
|
{
|
||||||
|
$parentId = Model::parseIntOr(
|
||||||
|
Model::issetInArray($data, ['parentId', 'parent_item']),
|
||||||
|
null
|
||||||
|
);
|
||||||
|
if ($parentId >= 0) {
|
||||||
|
return $parentId;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private function extractIsOnTop(array $data): bool
|
||||||
|
{
|
||||||
|
$isOnTop = Model::parseBool(
|
||||||
|
Model::issetInArray($data, ['isOnTop', 'show_on_top']),
|
||||||
|
null
|
||||||
|
);
|
||||||
|
return $isOnTop;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private function extractIsLinkEnabled(array $data): bool
|
||||||
|
{
|
||||||
|
$isLinkEnabled = Model::parseBool(
|
||||||
|
Model::issetInArray($data, ['isLinkEnabled', 'enable_link']),
|
||||||
|
null
|
||||||
|
);
|
||||||
|
return $isLinkEnabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private function extractLabel(array $data)
|
||||||
|
{
|
||||||
|
$label = Model::notEmptyStringOr(
|
||||||
|
Model::issetInArray($data, ['label']),
|
||||||
|
null
|
||||||
|
);
|
||||||
|
return $label;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private function extractLabelPosition(array $data): string
|
||||||
|
{
|
||||||
|
$labelPosition = Model::notEmptyStringOr(
|
||||||
|
Model::issetInArray($data, ['labelPosition', 'label_position']),
|
||||||
|
null
|
||||||
|
);
|
||||||
|
|
||||||
|
switch ($labelPosition) {
|
||||||
|
case 'up':
|
||||||
|
case 'right':
|
||||||
|
case 'left':
|
||||||
|
return $labelPosition;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return 'down';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,268 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Models\VisualConsole\Item as ItemConsole;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test class
|
||||||
|
*/
|
||||||
|
class ItemTest extends TestCase
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
public function testCanBeCreatedFromValidUserStructure(): void
|
||||||
|
{
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
ItemConsole::class,
|
||||||
|
ItemConsole::fromArray(
|
||||||
|
[
|
||||||
|
'id' => 1,
|
||||||
|
'type' => 5,
|
||||||
|
'label' => 'test',
|
||||||
|
'labelPosition' => 'down',
|
||||||
|
'isLinkEnabled' => false,
|
||||||
|
'isOnTop' => true,
|
||||||
|
'parentId' => 0,
|
||||||
|
'aclGroupId' => 12,
|
||||||
|
'width' => 800,
|
||||||
|
'height' => 600,
|
||||||
|
'x' => 0,
|
||||||
|
'y' => 0,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
ItemConsole::class,
|
||||||
|
ItemConsole::fromArray(
|
||||||
|
[
|
||||||
|
'id' => 1,
|
||||||
|
'type' => 5,
|
||||||
|
'width' => 0,
|
||||||
|
'height' => 0,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function testCannotBeCreatedWithInvalidId(): void
|
||||||
|
{
|
||||||
|
$this->expectException(InvalidArgumentException::class);
|
||||||
|
// Invalid id.
|
||||||
|
ItemConsole::fromArray(
|
||||||
|
[
|
||||||
|
'id' => 'foo',
|
||||||
|
'type' => 5,
|
||||||
|
'label' => 'test',
|
||||||
|
'labelPosition' => 'down',
|
||||||
|
'isLinkEnabled' => false,
|
||||||
|
'isOnTop' => true,
|
||||||
|
'parentId' => 0,
|
||||||
|
'aclGroupId' => 12,
|
||||||
|
'width' => 800,
|
||||||
|
'height' => 600,
|
||||||
|
'x' => 0,
|
||||||
|
'y' => 0,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
// Missing id.
|
||||||
|
ItemConsole::fromArray(
|
||||||
|
[
|
||||||
|
'type' => 5,
|
||||||
|
'label' => 'test',
|
||||||
|
'labelPosition' => 'down',
|
||||||
|
'isLinkEnabled' => false,
|
||||||
|
'isOnTop' => true,
|
||||||
|
'parentId' => 0,
|
||||||
|
'aclGroupId' => 12,
|
||||||
|
'width' => 800,
|
||||||
|
'height' => 600,
|
||||||
|
'x' => 0,
|
||||||
|
'y' => 0,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function testCannotBeCreatedWithInvalidType(): void
|
||||||
|
{
|
||||||
|
$this->expectException(InvalidArgumentException::class);
|
||||||
|
// Invalid id.
|
||||||
|
ItemConsole::fromArray(
|
||||||
|
[
|
||||||
|
'id' => 15,
|
||||||
|
'type' => 'clock',
|
||||||
|
'label' => 'test',
|
||||||
|
'labelPosition' => 'down',
|
||||||
|
'isLinkEnabled' => false,
|
||||||
|
'isOnTop' => true,
|
||||||
|
'parentId' => 0,
|
||||||
|
'aclGroupId' => 12,
|
||||||
|
'width' => 800,
|
||||||
|
'height' => 600,
|
||||||
|
'x' => 0,
|
||||||
|
'y' => 0,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
// Missing id.
|
||||||
|
ItemConsole::fromArray(
|
||||||
|
[
|
||||||
|
'id' => 6,
|
||||||
|
'label' => 'test',
|
||||||
|
'labelPosition' => 'down',
|
||||||
|
'isLinkEnabled' => false,
|
||||||
|
'isOnTop' => true,
|
||||||
|
'parentId' => 0,
|
||||||
|
'aclGroupId' => 12,
|
||||||
|
'width' => 800,
|
||||||
|
'height' => 600,
|
||||||
|
'x' => 0,
|
||||||
|
'y' => 0,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function testCannotBeCreatedWithInvalidWidth(): void
|
||||||
|
{
|
||||||
|
$this->expectException(InvalidArgumentException::class);
|
||||||
|
// Invalid id.
|
||||||
|
ItemConsole::fromArray(
|
||||||
|
[
|
||||||
|
'id' => 15,
|
||||||
|
'type' => 3,
|
||||||
|
'label' => 'test',
|
||||||
|
'labelPosition' => 'down',
|
||||||
|
'isLinkEnabled' => false,
|
||||||
|
'isOnTop' => true,
|
||||||
|
'parentId' => 0,
|
||||||
|
'aclGroupId' => 12,
|
||||||
|
'width' => -1,
|
||||||
|
'height' => 600,
|
||||||
|
'x' => 0,
|
||||||
|
'y' => 0,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
// Missing id.
|
||||||
|
ItemConsole::fromArray(
|
||||||
|
[
|
||||||
|
'id' => 15,
|
||||||
|
'type' => 3,
|
||||||
|
'label' => 'test',
|
||||||
|
'labelPosition' => 'down',
|
||||||
|
'isLinkEnabled' => false,
|
||||||
|
'isOnTop' => true,
|
||||||
|
'parentId' => 0,
|
||||||
|
'aclGroupId' => 12,
|
||||||
|
'height' => 600,
|
||||||
|
'x' => 0,
|
||||||
|
'y' => 0,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function testCannotBeCreatedWithInvalidHeight(): void
|
||||||
|
{
|
||||||
|
$this->expectException(InvalidArgumentException::class);
|
||||||
|
// Invalid id.
|
||||||
|
ItemConsole::fromArray(
|
||||||
|
[
|
||||||
|
'id' => 15,
|
||||||
|
'type' => 3,
|
||||||
|
'label' => 'test',
|
||||||
|
'labelPosition' => 'down',
|
||||||
|
'isLinkEnabled' => false,
|
||||||
|
'isOnTop' => true,
|
||||||
|
'parentId' => 0,
|
||||||
|
'aclGroupId' => 12,
|
||||||
|
'width' => 800,
|
||||||
|
'height' => -1,
|
||||||
|
'x' => 0,
|
||||||
|
'y' => 0,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
// Missing id.
|
||||||
|
ItemConsole::fromArray(
|
||||||
|
[
|
||||||
|
'id' => 15,
|
||||||
|
'type' => 3,
|
||||||
|
'label' => 'test',
|
||||||
|
'labelPosition' => 'down',
|
||||||
|
'isLinkEnabled' => false,
|
||||||
|
'isOnTop' => true,
|
||||||
|
'parentId' => 0,
|
||||||
|
'aclGroupId' => 12,
|
||||||
|
'width' => 600,
|
||||||
|
'x' => 0,
|
||||||
|
'y' => 0,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function testItemIsRepresentedAsJson(): void
|
||||||
|
{
|
||||||
|
$this->assertEquals(
|
||||||
|
'{"id":15,"type":3,"label":"test","labelPosition":"down","isLinkEnabled":false,"isOnTop":true,"parentId":0,"aclGroupId":12,"width":800,"height":600,"x":0,"y":0}',
|
||||||
|
ItemConsole::fromArray(
|
||||||
|
[
|
||||||
|
'id' => 15,
|
||||||
|
'type' => 3,
|
||||||
|
'label' => 'test',
|
||||||
|
'labelPosition' => 'down',
|
||||||
|
'isLinkEnabled' => false,
|
||||||
|
'isOnTop' => true,
|
||||||
|
'parentId' => 0,
|
||||||
|
'aclGroupId' => 12,
|
||||||
|
'width' => 800,
|
||||||
|
'height' => 600,
|
||||||
|
'x' => 0,
|
||||||
|
'y' => 0,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'{"id":15,"type":3,"label":null,"labelPosition":"down","isLinkEnabled":false,"isOnTop":false,"parentId":0,"aclGroupId":12,"width":800,"height":600,"x":0,"y":0}',
|
||||||
|
ItemConsole::fromArray(
|
||||||
|
[
|
||||||
|
'id' => 15,
|
||||||
|
'type' => 3,
|
||||||
|
'label' => '',
|
||||||
|
'labelPosition' => 'test',
|
||||||
|
'parentId' => 0,
|
||||||
|
'aclGroupId' => 12,
|
||||||
|
'width' => 800,
|
||||||
|
'height' => 600,
|
||||||
|
'x' => 0,
|
||||||
|
'y' => 0,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'{"id":69,"type":20,"label":null,"labelPosition":"up","isLinkEnabled":true,"isOnTop":false,"parentId":null,"aclGroupId":null,"width":0,"height":0,"x":-666,"y":76}',
|
||||||
|
ItemConsole::fromArray(
|
||||||
|
[
|
||||||
|
'id' => 69,
|
||||||
|
'type' => 20,
|
||||||
|
'label' => null,
|
||||||
|
'labelPosition' => 'up',
|
||||||
|
'isLinkEnabled' => true,
|
||||||
|
'isOnTop' => false,
|
||||||
|
'parentId' => null,
|
||||||
|
'width' => '0',
|
||||||
|
'height' => '0',
|
||||||
|
'x' => -666,
|
||||||
|
'y' => 76,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user