2019-03-18 12:47:39 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Models\VisualConsole;
|
|
|
|
use Models\Model;
|
|
|
|
|
|
|
|
final class Container extends Model
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2019-03-21 17:47:10 +01:00
|
|
|
/**
|
|
|
|
* Instance the class with the input data.
|
|
|
|
*
|
|
|
|
* @param mixed $data
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2019-03-19 12:03:02 +01:00
|
|
|
public static function fromArray(array $data): self
|
|
|
|
{
|
|
|
|
return new self($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-21 17:47:10 +01:00
|
|
|
/**
|
|
|
|
* Validate the input data
|
|
|
|
*
|
|
|
|
* @param mixed $data
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-03-18 13:35:30 +01:00
|
|
|
protected function validateData(array $data): void
|
2019-03-18 12:47:39 +01:00
|
|
|
{
|
|
|
|
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['name']) === false
|
|
|
|
|| \is_string($data['name']) === false
|
|
|
|
|| empty($data['name']) === true
|
|
|
|
) {
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
'the name property is required and should be string'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->extractGroupId($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-21 17:47:10 +01:00
|
|
|
/**
|
|
|
|
* Returns a valid data structure.
|
|
|
|
*
|
|
|
|
* @param mixed $data
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2019-03-18 13:35:30 +01:00
|
|
|
protected function decode(array $data): array
|
2019-03-18 12:47:39 +01:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'id' => (int) $data['id'],
|
|
|
|
'name' => $data['name'],
|
|
|
|
'groupId' => $this->extractGroupId($data),
|
|
|
|
'backgroundURL' => $this->extractBackgroundUrl($data),
|
|
|
|
'backgroundColor' => $this->extractBackgroundColor($data),
|
2019-03-19 12:03:02 +01:00
|
|
|
'isFavorite' => $this->extractFavorite($data),
|
2019-03-18 12:47:39 +01:00
|
|
|
'width' => (int) $data['width'],
|
|
|
|
'height' => (int) $data['height'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-19 12:03:02 +01:00
|
|
|
private function extractGroupId(array $data): int
|
2019-03-18 12:47:39 +01:00
|
|
|
{
|
|
|
|
if (isset($data['id_group']) === true
|
|
|
|
&& \is_numeric($data['id_group']) === true
|
2019-03-19 12:03:02 +01:00
|
|
|
&& $data['id_group'] >= 0
|
2019-03-18 12:47:39 +01:00
|
|
|
) {
|
|
|
|
return $data['id_group'];
|
|
|
|
} else if (isset($data['groupId']) === true
|
|
|
|
&& \is_numeric($data['groupId']) === true
|
2019-03-19 12:03:02 +01:00
|
|
|
&& $data['groupId'] >= 0
|
2019-03-18 12:47:39 +01:00
|
|
|
) {
|
|
|
|
return $data['groupId'];
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
'the group Id property is required and should be integer'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-19 12:03:02 +01:00
|
|
|
private function extractBackgroundUrl(array $data)
|
2019-03-18 12:47:39 +01:00
|
|
|
{
|
2019-03-19 12:03:02 +01:00
|
|
|
$background = Model::notEmptyStringOr(
|
|
|
|
Model::issetInArray($data, ['background', 'backgroundURL']),
|
|
|
|
null
|
|
|
|
);
|
|
|
|
return $background;
|
2019-03-18 12:47:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-19 12:03:02 +01:00
|
|
|
private function extractBackgroundColor(array $data)
|
2019-03-18 12:47:39 +01:00
|
|
|
{
|
2019-03-19 12:03:02 +01:00
|
|
|
$backgroundColor = Model::notEmptyStringOr(
|
|
|
|
Model::issetInArray($data, ['backgroundColor', 'background_color']),
|
|
|
|
null
|
|
|
|
);
|
|
|
|
return $backgroundColor;
|
|
|
|
}
|
2019-03-18 12:47:39 +01:00
|
|
|
|
|
|
|
|
2019-03-19 12:03:02 +01:00
|
|
|
private function extractFavorite(array $data): bool
|
|
|
|
{
|
|
|
|
$favorite = Model::parseBool(
|
|
|
|
Model::issetInArray($data, ['is_favourite', 'isFavorite']),
|
|
|
|
null
|
|
|
|
);
|
|
|
|
return $favorite;
|
2019-03-18 12:47:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|