From 64af34904171a6b0577621322b7524e8f56cc31d Mon Sep 17 00:00:00 2001 From: Daniel Maya Date: Tue, 26 Mar 2019 17:32:02 +0100 Subject: [PATCH] added class clock Former-commit-id: ad23dd5fe0f6eb2251851cf794d0b1464d286ff5 --- .../models/VisualConsole/Items/Clock.php | 147 ++++++++++++++++++ .../models/VisualConsole/Items/Group.php | 2 +- .../models/VisualConsole/Items/Line.php | 46 +++--- 3 files changed, 175 insertions(+), 20 deletions(-) create mode 100644 pandora_console/include/rest-api/models/VisualConsole/Items/Clock.php diff --git a/pandora_console/include/rest-api/models/VisualConsole/Items/Clock.php b/pandora_console/include/rest-api/models/VisualConsole/Items/Clock.php new file mode 100644 index 0000000000..5c69b82c49 --- /dev/null +++ b/pandora_console/include/rest-api/models/VisualConsole/Items/Clock.php @@ -0,0 +1,147 @@ +extractClockType($data); + $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); + + $clockData['showClockTimezone'] = $this->extractShowClockTimezone($data); + $clockData['color'] = $this->extractColor($data); + return $clockData; + } + + + /** + * Extract a clock type value. + * + * @param array $data Unknown input data structure. + * + * @return string Digital or analogic. analogic by default. + */ + private function extractClockType(array $data): string + { + $clockType = static::notEmptyStringOr( + static::issetInArray($data, ['clockType', 'clock_animation']), + null + ); + + switch ($clockType) { + case 'digital': + case 'digital_1': + return 'digital'; + + default: + return 'analogic'; + } + } + + + /** + * Extract a clock format value. + * + * @param array $data Unknown input data structure. + * + * @return string Time or datetime. datetime by default. + */ + private function extractClockFormat(array $data): string + { + $clockFormat = static::notEmptyStringOr( + static::issetInArray($data, ['clockFormat', 'time_format']), + null + ); + + switch ($clockFormat) { + case 'time': + return 'time'; + + default: + return 'datetime'; + } + } + + + /** + * Extract a clock timezone value. + * + * @param array $data Unknown input data structure. + * + * @return string + */ + private function extractClockTimezone(array $data): string + { + $clockTimezone = static::notEmptyStringOr( + static::issetInArray($data, ['clockTimezone', 'timezone']), + null + ); + + if ($clockTimezone === null) { + throw new \InvalidArgumentException( + 'the clockTimezone property is required and should be string' + ); + } else { + return $clockTimezone; + } + } + + + /** + * Extract a clock timezone value. + * + * @param array $data Unknown input data structure. + * + * @return boolean + */ + private function extractShowClockTimezone(array $data): bool + { + return static::parseBool( + static::issetInArray($data, ['showClockTimezone']) + ); + } + + + /** + * Extract the color value. + * + * @param array $data Unknown input data structure. + * + * @return mixed returns a color or null + */ + private function extractColor(array $data) + { + return static::notEmptyStringOr( + static::issetInArray($data, ['color', 'fill_color']), + null + ); + } + + +} diff --git a/pandora_console/include/rest-api/models/VisualConsole/Items/Group.php b/pandora_console/include/rest-api/models/VisualConsole/Items/Group.php index c48c26df0a..559f8865d5 100644 --- a/pandora_console/include/rest-api/models/VisualConsole/Items/Group.php +++ b/pandora_console/include/rest-api/models/VisualConsole/Items/Group.php @@ -48,7 +48,7 @@ final class Group extends Item * * @throws \InvalidArgumentException When a valid image src can't be found. */ - private function extractImageSrc(array $data) + private function extractImageSrc(array $data): string { $imageSrc = static::notEmptyStringOr( static::issetInArray($data, ['imageSrc', 'image']), diff --git a/pandora_console/include/rest-api/models/VisualConsole/Items/Line.php b/pandora_console/include/rest-api/models/VisualConsole/Items/Line.php index 58dd866071..32682817c8 100644 --- a/pandora_console/include/rest-api/models/VisualConsole/Items/Line.php +++ b/pandora_console/include/rest-api/models/VisualConsole/Items/Line.php @@ -10,11 +10,17 @@ final class Line extends Model /** - * Validate the input data + * Validate the received data structure to ensure if we can extract the + * values required to build the model. * - * @param mixed $data + * @param array $data Input data. * * @return void + * + * @throws \InvalidArgumentException If any input value is considered + * invalid. + * + * @overrides Model::validateData. */ protected function validateData(array $data): void { @@ -37,11 +43,13 @@ final class Line extends Model /** - * Returns a valid data structure. + * Returns a valid representation of the model. * - * @param mixed $data + * @param array $data Input data. * - * @return array + * @return array Data structure representing the model. + * + * @overrides Model::decode. */ protected function decode(array $data): array { @@ -63,9 +71,9 @@ final class Line extends Model * Extract the value of startX and * return a integer. * - * @param mixed $data + * @param array $data Unknown input data structure. * - * @return integer + * @return integer Valid x axis start position of the item. */ private function extractStartX(array $data): int { @@ -81,9 +89,9 @@ final class Line extends Model * Extract the value of startY and * return a integer. * - * @param mixed $data + * @param array $data Unknown input data structure. * - * @return integer + * @return integer Valid y axis start position of the item. */ private function extractStartY(array $data): int { @@ -99,9 +107,9 @@ final class Line extends Model * Extract the value of endX and * return a integer. * - * @param mixed $data + * @param array $data Unknown input data structure. * - * @return integer + * @return integer Valid x axis end position of the item. */ private function extractEndX(array $data): int { @@ -117,9 +125,9 @@ final class Line extends Model * Extract the value of endY and * return a integer. * - * @param mixed $data + * @param array $data Unknown input data structure. * - * @return integer + * @return integer Valid y axis end position of the item. */ private function extractEndY(array $data): int { @@ -135,9 +143,9 @@ final class Line extends Model * Extract the value of isOnTop and * return a bool. * - * @param mixed $data + * @param array $data Unknown input data structure. * - * @return boolean + * @return boolean If the item is on top or not. */ private function extractIsOnTope(array $data): bool { @@ -152,9 +160,9 @@ final class Line extends Model * Extract the value of borderWidth and * return a integer. * - * @param mixed $data + * @param array $data Unknown input data structure. * - * @return integer + * @return integer Valid border width. 0 by default. */ private function extractBorderWidth(array $data): int { @@ -174,9 +182,9 @@ final class Line extends Model * Extract the value of borderColor and * return to not empty string or null. * - * @param mixed $data + * @param array $data Unknown input data structure. * - * @return void + * @return mixed String representing the border color (not empty) or null. */ private function extractBorderColor(array $data) {