diff --git a/pandora_console/include/rest-api/models/VisualConsole/Items/Clock.php b/pandora_console/include/rest-api/models/VisualConsole/Items/Clock.php index 5c69b82c49..2efc556be3 100644 --- a/pandora_console/include/rest-api/models/VisualConsole/Items/Clock.php +++ b/pandora_console/include/rest-api/models/VisualConsole/Items/Clock.php @@ -11,6 +11,14 @@ use Models\VisualConsole\Item; final class Clock extends Item { + /** + * Used to enable the fetching, validation and extraction of information + * about the linked visual console. + * + * @var boolean + */ + protected static $useLinkedVisualConsole = true; + /** * Returns a valid representation of the model. @@ -29,10 +37,13 @@ final class Clock extends Item $clockData['clockFormat'] = $this->extractClockFormat($data); $clockData['clockTimezone'] = $this->extractClockTimezone($data); - $dateTimeZoneUTC = new DateTimeZone('UTC'); - $dateTimeZoneClock = new DateTimeZone($clockData['clockTimezone']); - $dateTime = new DateTime('now', $dateTimeZoneClock); - $clockData['clockTimezoneOffset'] = $dateTimeZoneUTC->getOffset($dateTime); + try { + $timezone = new \DateTimeZone($clockData['clockTimezone']); + $timezoneUTC = new \DateTimeZone('UTC'); + $clockData['clockTimezoneOffset'] = $timezone->getOffset(new \DateTime('now', $timezoneUTC)); + } catch (Exception $e) { + throw new \InvalidArgumentException($e->getMessage()); + } $clockData['showClockTimezone'] = $this->extractShowClockTimezone($data); $clockData['color'] = $this->extractColor($data); diff --git a/pandora_console/include/rest-api/models/VisualConsole/Items/ColorCloud.php b/pandora_console/include/rest-api/models/VisualConsole/Items/ColorCloud.php new file mode 100644 index 0000000000..0107189ad8 --- /dev/null +++ b/pandora_console/include/rest-api/models/VisualConsole/Items/ColorCloud.php @@ -0,0 +1,116 @@ +extractColor($data); + $colorCloudData['colorRanges'] = ''; + + return $colorCloudData; + } + + + /** + * Extract a color value. + * + * @param array $data Unknown input data structure. + * + * @return string + */ + private function extractColor(array $data): string + { + $color = static::notEmptyStringOr( + static::issetInArray($data, ['color']), + null + ); + + if (empty($color) === true) { + $color = static::notEmptyStringOr( + static::issetInArray($data, ['label']), + null + ); + + $color_decode = \json_decode($color); + if (empty($color) === true || empty($color_decode->default_color) === true) { + throw new \InvalidArgumentException( + 'the color property is required and should be string' + ); + } else { + return $color_decode->default_color; + } + } else { + return $color; + } + } + + + /** + * Extract a color ranges value. + * + * @param array $data Unknown input data structure. + * + * @return string + */ + private function extractColorRanges(array $data): array + { + if (isset($data['colorRanges']) && \is_array($data['colorRanges'])) { + foreach ($data['colorRanges'] as $key => $value) { + if ((!isset($data['colorRanges'][$key]['fromValue']) || !\is_float($data['colorRanges'][$key]['fromValue'])) + || (!isset($data['colorRanges'][$key]['toValue']) || !\is_float($data['colorRanges'][$key]['toValue'])) + || (!isset($data['colorRanges'][$key]['color']) | !\is_string($data['colorRanges'][$key]['color']) || strlen($data['colorRanges'][$key]['color']) == 0) + ) { + throw new \InvalidArgumentException( + 'the color property is required and should be string' + ); + } + } + + return $data['colorRanges']; + } else if (isset($data['label']) === true) { + $colorRanges_decode = \json_decode($data['label']); + return $colorRanges_decode->color_ranges; + } else { + return []; + } + } + + +} diff --git a/pandora_console/tests/Functional/Models/VisualConsole/Items/ClockTest.php b/pandora_console/tests/Functional/Models/VisualConsole/Items/ClockTest.php new file mode 100644 index 0000000000..2c72106d74 --- /dev/null +++ b/pandora_console/tests/Functional/Models/VisualConsole/Items/ClockTest.php @@ -0,0 +1,173 @@ +assertInstanceOf( + Clock::class, + Clock::fromArray( + [ + 'id' => 69, + 'type' => CLOCK, + 'label' => null, + 'labelPosition' => 'up', + 'isLinkEnabled' => true, + 'isOnTop' => false, + 'parentId' => null, + 'width' => '0', + 'height' => '0', + 'x' => -666, + 'y' => 76, + 'clockType' => 'digital', + 'clockFormat' => 'time', + 'clockTimezone' => 'Europe/Madrid', + 'showClockTimezone' => false, + 'color' => 'white', + ] + ) + ); + + $this->assertInstanceOf( + Clock::class, + Clock::fromArray( + [ + 'id' => 1000, + 'type' => CLOCK, + 'width' => 100, + 'height' => 900, + 'clockType' => 'analogic', + 'clockFormat' => 'datetime', + 'clockTimezone' => 'Asia/Tokyo', + 'showClockTimezone' => true, + 'color' => 'red', + ] + ) + ); + } + + + /** + * Test if the instance is not created when using a invalid clockTimezone. + * + * @return void + */ + public function testCannotBeCreatedWithInvalidImageSrc(): void + { + $this->expectException(Exception::class); + // Invalid clockTimezone. + Clock::fromArray( + [ + 'id' => 69, + 'type' => CLOCK, + 'label' => null, + 'labelPosition' => 'up', + 'isLinkEnabled' => true, + 'isOnTop' => false, + 'parentId' => null, + 'width' => '0', + 'height' => '0', + 'x' => -666, + 'y' => 76, + 'clockType' => 'digital', + 'clockFormat' => 'time', + 'clockTimezone' => 'Europe/Tokyo', + 'showClockTimezone' => false, + 'color' => 'white', + ] + ); + + // Invalid clockTimezone. + Clock::fromArray( + [ + 'id' => 69, + 'type' => CLOCK, + 'label' => null, + 'labelPosition' => 'up', + 'isLinkEnabled' => true, + 'isOnTop' => false, + 'parentId' => null, + 'width' => '0', + 'height' => '0', + 'x' => -666, + 'y' => 76, + 'clockType' => 'digital', + 'clockFormat' => 'time', + 'clockTimezone' => 'Europe/Tokyo', + 'showClockTimezone' => false, + 'color' => 'white', + ] + ); + + // Missing clockTimezone. + Clock::fromArray( + [ + 'id' => 69, + 'type' => CLOCK, + 'label' => null, + 'labelPosition' => 'up', + 'isLinkEnabled' => true, + 'isOnTop' => false, + 'parentId' => null, + 'width' => '0', + 'height' => '0', + 'x' => -666, + 'y' => 76, + 'clockType' => 'digital', + 'clockFormat' => 'time', + 'showClockTimezone' => false, + 'color' => 'white', + ] + ); + } + + + /** + * Test if the model has a valid JSON representation. + * + * @return void + */ + public function testContainerIsRepresentedAsJson(): void + { + $this->assertEquals( + '{"aclGroupId":null,"clockFormat":"time","clockTimezone":"Europe\/Madrid","clockTimezoneOffset":3600,"clockType":"digital","color":"white","height":0,"id":69,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","parentId":null,"showClockTimezone":false,"type":19,"width":0,"x":-666,"y":76}', + (string) Clock::fromArray( + [ + 'id' => 69, + 'type' => CLOCK, + 'label' => null, + 'labelPosition' => 'up', + 'isLinkEnabled' => true, + 'isOnTop' => false, + 'parentId' => null, + 'width' => '0', + 'height' => '0', + 'x' => -666, + 'y' => 76, + 'clockType' => 'digital', + 'clockFormat' => 'time', + 'clockTimezone' => 'Europe/Madrid', + 'showClockTimezone' => false, + 'color' => 'white', + ] + ) + ); + } + + +}