Visual Console Refactor: added a Visual Console model

Former-commit-id: e3ae133ce6ab9c5df3c3f9c079af122e9af92e6c
This commit is contained in:
Alejandro Gallardo Escobar 2019-03-18 12:47:39 +01:00
parent 9eebf18622
commit 99a6e96ce9
1 changed files with 119 additions and 0 deletions

View File

@ -0,0 +1,119 @@
<?php
declare(strict_types=1);
namespace Models\VisualConsole;
use Models\Model;
final class Container extends Model
{
private function validateData(array $data): void
{
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);
}
private function decode(array $data): array
{
return [
'id' => (int) $data['id'],
'name' => $data['name'],
'groupId' => $this->extractGroupId($data),
'backgroundURL' => $this->extractBackgroundUrl($data),
'backgroundColor' => $this->extractBackgroundColor($data),
'isFavorite' => Model::parseBool($data['is_favourite'])
|| Model::parseBool($data['isFavorite']),
'width' => (int) $data['width'],
'height' => (int) $data['height'],
];
}
private function extractGroupId(array $data): number
{
if (isset($data['id_group']) === true
&& \is_numeric($data['id_group']) === true
) {
return $data['id_group'];
} else if (isset($data['groupId']) === true
&& \is_numeric($data['groupId']) === true
) {
return $data['groupId'];
}
throw new \InvalidArgumentException(
'the group Id property is required and should be integer'
);
}
private function extractBackgroundUrl(array $data): mixed
{
$backgroundUrl = Model::notEmptyStringOr($data['background'], null);
if ($backgroundUrl !== null) {
return $backgroundUrl;
}
$backgroundUrl = Model::notEmptyStringOr($data['backgroundURL'], null);
if ($backgroundUrl !== null) {
return $backgroundUrl;
}
return null;
}
private function extractBackgroundColor(array $data): mixed
{
$backgroundColor = Model::notEmptyStringOr($data['background_color'], null);
if ($backgroundColor !== null) {
return $backgroundColor;
}
$backgroundColor = Model::notEmptyStringOr($data['backgroundColor'], null);
if ($backgroundColor !== null) {
return $backgroundColor;
}
return null;
}
}