Merge branch 'ent-3733-Crear-modelos-de-los-distintos-recursos-de-la-Consola-Visual' of https://brutus.artica.lan:8081/artica/pandorafms into ent-3733-Crear-modelos-de-los-distintos-recursos-de-la-Consola-Visual

Former-commit-id: 40a12a5a5331fb53fed7fcfce30ba1e327494c59
This commit is contained in:
Alejandro Gallardo Escobar 2019-03-26 18:46:52 +01:00
commit 8d95afc9ea
3 changed files with 175 additions and 20 deletions

View File

@ -0,0 +1,147 @@
<?php
declare(strict_types=1);
namespace Models\VisualConsole\Items;
use Models\VisualConsole\Item;
/**
* Model of a Clock item of the Visual Console.
*/
final class Clock extends Item
{
/**
* 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
{
$clockData = parent::decode($data);
$clockData['type'] = CLOCK;
$clockData['clockType'] = $this->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
);
}
}

View File

@ -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']),

View File

@ -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)
{