mirror of
https://github.com/pandorafms/pandorafms.git
synced 2025-07-27 15:54:29 +02:00
Added the label and colorcloud classes
Former-commit-id: 5a68272b63169f7d36520b3d4cdead28b26209d3
This commit is contained in:
parent
58f6fac29e
commit
4893c78f77
@ -42,8 +42,8 @@ final class ColorCloud extends Item
|
|||||||
$colorCloudData = parent::decode($data);
|
$colorCloudData = parent::decode($data);
|
||||||
$colorCloudData['type'] = COLOR_CLOUD;
|
$colorCloudData['type'] = COLOR_CLOUD;
|
||||||
$colorCloudData['color'] = $this->extractColor($data);
|
$colorCloudData['color'] = $this->extractColor($data);
|
||||||
$colorCloudData['colorRanges'] = '';
|
$colorCloudData['colorRanges'] = $this->extractColorRanges($data);
|
||||||
|
$colorCloudData['label'] = null;
|
||||||
return $colorCloudData;
|
return $colorCloudData;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,12 +68,18 @@ final class ColorCloud extends Item
|
|||||||
null
|
null
|
||||||
);
|
);
|
||||||
|
|
||||||
$color_decode = \json_decode($color);
|
if (empty($color) === true) {
|
||||||
if (empty($color) === true || empty($color_decode->default_color) === true) {
|
|
||||||
throw new \InvalidArgumentException(
|
throw new \InvalidArgumentException(
|
||||||
'the color property is required and should be string'
|
'the color property is required and should be string'
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
$color_decode = \json_decode($color);
|
||||||
|
if (empty($color_decode->default_color) === true) {
|
||||||
|
throw new \InvalidArgumentException(
|
||||||
|
'the color property is required and should be string'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return $color_decode->default_color;
|
return $color_decode->default_color;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -93,12 +99,13 @@ final class ColorCloud extends Item
|
|||||||
{
|
{
|
||||||
if (isset($data['colorRanges']) && \is_array($data['colorRanges'])) {
|
if (isset($data['colorRanges']) && \is_array($data['colorRanges'])) {
|
||||||
foreach ($data['colorRanges'] as $key => $value) {
|
foreach ($data['colorRanges'] as $key => $value) {
|
||||||
if ((!isset($data['colorRanges'][$key]['fromValue']) || !\is_float($data['colorRanges'][$key]['fromValue']))
|
if ((!isset($value['fromValue']) || !\is_numeric($value['fromValue']))
|
||||||
|| (!isset($data['colorRanges'][$key]['toValue']) || !\is_float($data['colorRanges'][$key]['toValue']))
|
|| (!isset($value['toValue']) || !\is_numeric($value['toValue']))
|
||||||
|| (!isset($data['colorRanges'][$key]['color']) | !\is_string($data['colorRanges'][$key]['color']) || strlen($data['colorRanges'][$key]['color']) == 0)
|
|| (!isset($value['color']) || !\is_string($value['color'])
|
||||||
|
|| \strlen($value['color']) == 0)
|
||||||
) {
|
) {
|
||||||
throw new \InvalidArgumentException(
|
throw new \InvalidArgumentException(
|
||||||
'the color property is required and should be string'
|
'the fromValue, toValue and color properties is required'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -106,7 +113,18 @@ final class ColorCloud extends Item
|
|||||||
return $data['colorRanges'];
|
return $data['colorRanges'];
|
||||||
} else if (isset($data['label']) === true) {
|
} else if (isset($data['label']) === true) {
|
||||||
$colorRanges_decode = \json_decode($data['label']);
|
$colorRanges_decode = \json_decode($data['label']);
|
||||||
return $colorRanges_decode->color_ranges;
|
$array_out = [];
|
||||||
|
if (!empty($colorRanges_decode->color_ranges)) {
|
||||||
|
foreach ($colorRanges_decode->color_ranges as $key => $value) {
|
||||||
|
$array_aux = [];
|
||||||
|
$array_aux['fromValue'] = $value->from_value;
|
||||||
|
$array_aux['toValue'] = $value->to_value;
|
||||||
|
$array_aux['color'] = $value->color;
|
||||||
|
array_push($array_out, $array_aux);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $array_out;
|
||||||
} else {
|
} else {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ use Models\VisualConsole\Item;
|
|||||||
use Models\Model;
|
use Models\Model;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Model of a group item of the Visual Console.
|
* Model of a events history item of the Visual Console.
|
||||||
*/
|
*/
|
||||||
final class EventsHistory extends Item
|
final class EventsHistory extends Item
|
||||||
{
|
{
|
||||||
|
@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Models\VisualConsole\Items;
|
||||||
|
use Models\VisualConsole\Item;
|
||||||
|
use Models\Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Model of a label item of the Visual Console.
|
||||||
|
*/
|
||||||
|
final class Label extends Item
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to enable the fetching, validation and extraction of information
|
||||||
|
* about the linked visual console.
|
||||||
|
*
|
||||||
|
* @var boolean
|
||||||
|
*/
|
||||||
|
protected static $useLinkedVisualConsole = true;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* @overrides Item::validateData.
|
||||||
|
*/
|
||||||
|
protected function validateData(array $data): void
|
||||||
|
{
|
||||||
|
parent::validateData($data);
|
||||||
|
if (empty($data['label']) === true) {
|
||||||
|
throw new \InvalidArgumentException(
|
||||||
|
'the label property is required and should be string and not null'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a valid representation of the model.
|
||||||
|
*
|
||||||
|
* @param array $data Input data.
|
||||||
|
*
|
||||||
|
* @return array Data structure representing the model.
|
||||||
|
*
|
||||||
|
* @overrides Item::decode.
|
||||||
|
*/
|
||||||
|
protected function decode(array $data): array
|
||||||
|
{
|
||||||
|
$return = parent::decode($data);
|
||||||
|
$return['type'] = LABEL;
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -67,7 +67,7 @@ class ClockTest extends TestCase
|
|||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function testCannotBeCreatedWithInvalidImageSrc(): void
|
public function testCannotBeCreatedWithInvalidClockTimezone(): void
|
||||||
{
|
{
|
||||||
$this->expectException(Exception::class);
|
$this->expectException(Exception::class);
|
||||||
// Invalid clockTimezone.
|
// Invalid clockTimezone.
|
||||||
|
@ -0,0 +1,182 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Models\VisualConsole\Items\ColorCloud;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for the Visual Console color cloud Item model.
|
||||||
|
*/
|
||||||
|
class ColorCloudTest extends TestCase
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if the instance is created using a valid data structure.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testCanBeCreatedFromValidUserStructure(): void
|
||||||
|
{
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
ColorCloud::class,
|
||||||
|
ColorCloud::fromArray(
|
||||||
|
[
|
||||||
|
'id' => 69,
|
||||||
|
'type' => COLOR_CLOUD,
|
||||||
|
'label' => '{"default_color":"#47b042","color_ranges":[{"from_value":20,"to_value":60,"color":"#d0da27"},{"from_value":61,"to_value":100,"color":"#ec1f1f"}]}',
|
||||||
|
'labelPosition' => 'up',
|
||||||
|
'isLinkEnabled' => true,
|
||||||
|
'isOnTop' => false,
|
||||||
|
'parentId' => null,
|
||||||
|
'width' => '0',
|
||||||
|
'height' => '0',
|
||||||
|
'x' => -666,
|
||||||
|
'y' => 76,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
ColorCloud::class,
|
||||||
|
ColorCloud::fromArray(
|
||||||
|
[
|
||||||
|
'id' => 1000,
|
||||||
|
'type' => COLOR_CLOUD,
|
||||||
|
'width' => 100,
|
||||||
|
'height' => 900,
|
||||||
|
'color' => '#47b042',
|
||||||
|
'colorRanges' => [
|
||||||
|
[
|
||||||
|
'fromValue' => 50,
|
||||||
|
'toValue' => 90,
|
||||||
|
'color' => '#d0da27',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'fromValue' => 910,
|
||||||
|
'toValue' => 100,
|
||||||
|
'color' => '#ec1f1f',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if the instance is not created when using a invalid color.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testCannotBeCreatedWithInvalidColor(): void
|
||||||
|
{
|
||||||
|
$this->expectException(InvalidArgumentException::class);
|
||||||
|
// Invalid color.
|
||||||
|
ColorCloud::fromArray(
|
||||||
|
[
|
||||||
|
'id' => 69,
|
||||||
|
'type' => COLOR_CLOUD,
|
||||||
|
'label' => null,
|
||||||
|
'labelPosition' => 'up',
|
||||||
|
'isLinkEnabled' => true,
|
||||||
|
'isOnTop' => false,
|
||||||
|
'parentId' => null,
|
||||||
|
'width' => '0',
|
||||||
|
'height' => '0',
|
||||||
|
'x' => -666,
|
||||||
|
'y' => 76,
|
||||||
|
'color' => '',
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
// Invalid color.
|
||||||
|
ColorCloud::fromArray(
|
||||||
|
[
|
||||||
|
'id' => 69,
|
||||||
|
'type' => COLOR_CLOUD,
|
||||||
|
'label' => '{"default_col":"#47b042","color_ranges":[]}',
|
||||||
|
'labelPosition' => 'up',
|
||||||
|
'isLinkEnabled' => true,
|
||||||
|
'isOnTop' => false,
|
||||||
|
'parentId' => null,
|
||||||
|
'width' => '0',
|
||||||
|
'height' => '0',
|
||||||
|
'x' => -666,
|
||||||
|
'y' => 76,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
// Missing color.
|
||||||
|
ColorCloud::fromArray(
|
||||||
|
[
|
||||||
|
'id' => 69,
|
||||||
|
'type' => COLOR_CLOUD,
|
||||||
|
'labelPosition' => 'up',
|
||||||
|
'isLinkEnabled' => true,
|
||||||
|
'isOnTop' => false,
|
||||||
|
'parentId' => null,
|
||||||
|
'width' => '0',
|
||||||
|
'height' => '0',
|
||||||
|
'x' => -666,
|
||||||
|
'y' => 76,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if the model has a valid JSON representation.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testContainerIsRepresentedAsJson(): void
|
||||||
|
{
|
||||||
|
$this->assertEquals(
|
||||||
|
'{"aclGroupId":null,"agentId":null,"agentName":null,"color":"#47b042","colorRanges":[{"fromValue":20,"toValue":60,"color":"#d0da27"},{"fromValue":61,"toValue":100,"color":"#ec1f1f"}],"height":0,"id":69,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","moduleId":null,"moduleName":null,"parentId":null,"type":20,"width":0,"x":-666,"y":76}',
|
||||||
|
(string) ColorCloud::fromArray(
|
||||||
|
[
|
||||||
|
'id' => 69,
|
||||||
|
'type' => COLOR_CLOUD,
|
||||||
|
'label' => '{"default_color":"#47b042","color_ranges":[{"from_value":20,"to_value":60,"color":"#d0da27"},{"from_value":61,"to_value":100,"color":"#ec1f1f"}]}',
|
||||||
|
'labelPosition' => 'up',
|
||||||
|
'isLinkEnabled' => true,
|
||||||
|
'isOnTop' => false,
|
||||||
|
'parentId' => null,
|
||||||
|
'width' => '0',
|
||||||
|
'height' => '0',
|
||||||
|
'x' => -666,
|
||||||
|
'y' => 76,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'{"aclGroupId":null,"agentId":null,"agentName":null,"color":"#47b042","colorRanges":[{"fromValue":50,"toValue":90,"color":"#d0da27"},{"fromValue":910,"toValue":100,"color":"#ec1f1f"}],"height":900,"id":1000,"isLinkEnabled":false,"isOnTop":false,"label":null,"labelPosition":"down","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","moduleId":null,"moduleName":null,"parentId":null,"type":20,"width":100,"x":0,"y":0}',
|
||||||
|
(string) ColorCloud::fromArray(
|
||||||
|
[
|
||||||
|
'id' => 1000,
|
||||||
|
'type' => COLOR_CLOUD,
|
||||||
|
'width' => 100,
|
||||||
|
'height' => 900,
|
||||||
|
'color' => '#47b042',
|
||||||
|
'colorRanges' => [
|
||||||
|
[
|
||||||
|
'fromValue' => 50,
|
||||||
|
'toValue' => 90,
|
||||||
|
'color' => '#d0da27',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'fromValue' => 910,
|
||||||
|
'toValue' => 100,
|
||||||
|
'color' => '#ec1f1f',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,137 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Models\VisualConsole\Items\Label;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for the Visual Console label Item model.
|
||||||
|
*/
|
||||||
|
class LabelTest extends TestCase
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if the instance is created using a valid data structure.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testCanBeCreatedFromValidUserStructure(): void
|
||||||
|
{
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
Label::class,
|
||||||
|
Label::fromArray(
|
||||||
|
[
|
||||||
|
'id' => 3,
|
||||||
|
'type' => LABEL,
|
||||||
|
'width' => '600',
|
||||||
|
'height' => '500',
|
||||||
|
'label' => 'test',
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if the instance is not created when using a invalid label.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testCannotBeCreatedWithInvalidLabel(): void
|
||||||
|
{
|
||||||
|
$this->expectException(InvalidArgumentException::class);
|
||||||
|
// Invalid id.
|
||||||
|
Label::fromArray(
|
||||||
|
[
|
||||||
|
'id' => 3,
|
||||||
|
'type' => LABEL,
|
||||||
|
'width' => '600',
|
||||||
|
'height' => '500',
|
||||||
|
'label' => null,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
// Missing id.
|
||||||
|
Label::fromArray(
|
||||||
|
[
|
||||||
|
'id' => 3,
|
||||||
|
'type' => LABEL,
|
||||||
|
'width' => '600',
|
||||||
|
'height' => '500',
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if the model has a valid JSON representation.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testContainerIsRepresentedAsJson(): void
|
||||||
|
{
|
||||||
|
$this->assertEquals(
|
||||||
|
'{"aclGroupId":null,"height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":"test","labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","parentId":null,"type":4,"width":0,"x":-666,"y":76}',
|
||||||
|
(string) Label::fromArray(
|
||||||
|
[
|
||||||
|
'id' => 7,
|
||||||
|
'type' => LABEL,
|
||||||
|
'label' => 'test',
|
||||||
|
'labelPosition' => 'up',
|
||||||
|
'isLinkEnabled' => true,
|
||||||
|
'isOnTop' => false,
|
||||||
|
'parentId' => null,
|
||||||
|
'width' => '0',
|
||||||
|
'height' => '0',
|
||||||
|
'x' => -666,
|
||||||
|
'y' => 76,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'{"aclGroupId":null,"height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":"test_pandora","labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":1,"linkedLayoutStatusType":"default","parentId":null,"type":4,"width":0,"x":-666,"y":76}',
|
||||||
|
(string) Label::fromArray(
|
||||||
|
[
|
||||||
|
'id' => 7,
|
||||||
|
'type' => LABEL,
|
||||||
|
'label' => 'test_pandora',
|
||||||
|
'labelPosition' => 'up',
|
||||||
|
'isLinkEnabled' => true,
|
||||||
|
'isOnTop' => false,
|
||||||
|
'parentId' => null,
|
||||||
|
'width' => '0',
|
||||||
|
'height' => '0',
|
||||||
|
'x' => -666,
|
||||||
|
'y' => 76,
|
||||||
|
'id_layout_linked' => 1,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'{"aclGroupId":null,"height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":"test_pandora","labelPosition":"up","linkedLayoutAgentId":3,"linkedLayoutId":2,"linkedLayoutStatusType":"default","metaconsoleId":5,"parentId":null,"type":4,"width":0,"x":-666,"y":76}',
|
||||||
|
(string) Label::fromArray(
|
||||||
|
[
|
||||||
|
'id' => 7,
|
||||||
|
'type' => LABEL,
|
||||||
|
'label' => 'test_pandora',
|
||||||
|
'labelPosition' => 'up',
|
||||||
|
'isLinkEnabled' => true,
|
||||||
|
'isOnTop' => false,
|
||||||
|
'parentId' => null,
|
||||||
|
'width' => '0',
|
||||||
|
'height' => '0',
|
||||||
|
'x' => -666,
|
||||||
|
'y' => 76,
|
||||||
|
'id_metaconsole' => 5,
|
||||||
|
'linked_layout_node_id' => 3,
|
||||||
|
'linkedLayoutId' => 2,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user