Updated doc and created box class

Former-commit-id: b369630638f809f82bedb1745914538b24cb4922
This commit is contained in:
Daniel Maya 2019-03-21 17:47:10 +01:00
parent ca1b6f1ee4
commit 282a816c01
5 changed files with 329 additions and 8 deletions

View File

@ -16,25 +16,42 @@ abstract class Model
protected abstract function decode(array $data): array;
protected function __construct(array $unknownData)
public function __construct(array $unknownData)
{
$this->validateData($unknownData);
$this->data = $this->decode($unknownData);
}
/**
* Returns the JSON representation of the given value.
*
* @return string
*/
public function toJson(): string
{
return \json_encode($this->data);
}
/**
* Returns the text representation of this class.
*
* @return string
*/
public function __toString(): string
{
return $this->toJson();
}
/**
* Returns a Boolean of a mixed value.
*
* @param mixed $value
*
* @return boolean
*/
protected static function parseBool($value): bool
{
if (\is_bool($value) === true) {
@ -49,24 +66,42 @@ abstract class Model
}
/**
* Return a not empty string or a default value from a mixed value.
*
* @param mixed $val
* @param mixed $def Default value to use if we cannot extract a non empty string.
*
* @return mixed
*/
protected static function notEmptyStringOr($val, $def)
{
return (\is_string($val) === true && strlen($val) > 0) ? $val : $def;
}
/**
* Return a integer or a default value from a mixed value.
*
* @param mixed $val
* @param mixed $def
*
* @return mixed
*/
protected static function parseIntOr($val, $def)
{
if (\is_integer($val)) {
return $val;
} else if ((\is_string($val) && strlen($val) > 0) && ($val === '0' || (int) $val != 0)) {
return (int) $val;
} else {
return $def;
}
return is_numeric($val) ? (int) $val : $def;
}
/**
* Returns the value if it exists in the array
*
* @param array $val input array
* @param array $keys array with the keys to search
*
* @return mixed
*/
protected static function issetInArray(array $val, array $keys)
{
foreach ($keys as $key => $value) {

View File

@ -9,12 +9,26 @@ final class Container extends Model
{
/**
* Instance the class with the input data.
*
* @param mixed $data
*
* @return self
*/
public static function fromArray(array $data): self
{
return new self($data);
}
/**
* Validate the input data
*
* @param mixed $data
*
* @return void
*/
protected function validateData(array $data): void
{
if (isset($data['id']) === false
@ -56,6 +70,13 @@ final class Container extends Model
}
/**
* Returns a valid data structure.
*
* @param mixed $data
*
* @return array
*/
protected function decode(array $data): array
{
return [

View File

@ -9,12 +9,26 @@ class Item extends Model
{
/**
* Instance the class with the input data.
*
* @param mixed $data
*
* @return self
*/
public static function fromArray(array $data): self
{
return new self($data);
}
/**
* Validate the input data.
*
* @param mixed $data
*
* @return void
*/
protected function validateData(array $data): void
{
if (isset($data['id']) === false
@ -53,6 +67,13 @@ class Item extends Model
}
/**
* Returns a valid data structure.
*
* @param mixed $data
*
* @return array
*/
protected function decode(array $data): array
{
return [
@ -72,6 +93,13 @@ class Item extends Model
}
/**
* extractX
*
* @param mixed $data
*
* @return integer
*/
private function extractX(array $data): int
{
if (isset($data['pos_x']) === true
@ -88,6 +116,13 @@ class Item extends Model
}
/**
* extractY
*
* @param mixed $data
*
* @return integer
*/
private function extractY(array $data): int
{
if (isset($data['pos_y']) === true
@ -104,6 +139,14 @@ class Item extends Model
}
/**
* Extract the value of id_group and
* return a integer or null.
*
* @param mixed $data
*
* @return void
*/
private function extractAclGroupId(array $data)
{
$aclGroupId = Model::parseIntOr(
@ -118,6 +161,14 @@ class Item extends Model
}
/**
* Extract the value of parentId and
* return a integer or null.
*
* @param mixed $data
*
* @return void
*/
private function extractParentId(array $data)
{
$parentId = Model::parseIntOr(
@ -132,6 +183,14 @@ class Item extends Model
}
/**
* Extract the value of isOnTop and
* return a Boolean.
*
* @param mixed $data
*
* @return boolean
*/
private function extractIsOnTop(array $data): bool
{
$isOnTop = Model::parseBool(
@ -142,6 +201,14 @@ class Item extends Model
}
/**
* Extract the value of isLinkEnabled and
* return a Boolean.
*
* @param mixed $data
*
* @return boolean
*/
private function extractIsLinkEnabled(array $data): bool
{
$isLinkEnabled = Model::parseBool(
@ -152,6 +219,14 @@ class Item extends Model
}
/**
* Extract the value of label and
* return to not empty string or null.
*
* @param mixed $data
*
* @return void
*/
private function extractLabel(array $data)
{
$label = Model::notEmptyStringOr(
@ -162,6 +237,14 @@ class Item extends Model
}
/**
* Extract the value of labelPosition and
* return to not empty string or null.
*
* @param mixed $data
*
* @return string
*/
private function extractLabelPosition(array $data): string
{
$labelPosition = Model::notEmptyStringOr(

View File

@ -0,0 +1,108 @@
<?php
declare(strict_types=1);
namespace Models\VisualConsole\items;
use Models\VisualConsole\Item;
use Models\Model;
final class Box extends Item
{
/**
* Validate the input data.
*
* @param mixed $data
*
* @return void
*
* @override
*/
protected function validateData(array $data): void
{
parent::validateData($data);
}
/**
* Returns a valid data structure.
*
* @param mixed $data
*
* @return array
*
* @override
*/
protected function decode(array $data): array
{
$return = parent::decode($data);
$return['type'] = BOX_ITEM;
$return['parentId'] = null;
$return['aclGroupId'] = null;
$return['borderWidth'] = $this->extractBorderWidth($data);
$return['borderColor'] = $this->extractBorderColor($data);
$return['fillColor'] = $this->extractFillColor($data);
return $return;
}
/**
* Extract the value of borderWidth and
* return a integer.
*
* @param mixed $data
*
* @return integer
*/
private function extractBorderWidth(array $data): int
{
$borderWidth = Model::parseIntOr(
Model::issetInArray($data, ['borderWidth', 'border_width']),
0
);
if ($borderWidth >= 0) {
return $borderWidth;
} else {
return 0;
}
}
/**
* Extract the value of borderColor and
* return to not empty string or null.
*
* @param mixed $data
*
* @return void
*/
private function extractBorderColor(array $data)
{
$borderColor = Model::notEmptyStringOr(
Model::issetInArray($data, ['borderColor', 'border_color']),
null
);
return $borderColor;
}
/**
* Extract the value of fillColor and
* return to not empty string or null.
*
* @param mixed $data
*
* @return void
*/
private function extractFillColor(array $data)
{
$borderColor = Model::notEmptyStringOr(
Model::issetInArray($data, ['fillColor', 'fill_color']),
null
);
return $borderColor;
}
}

View File

@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use Models\VisualConsole\items\Box as Vox;
/**
* Test class
*/
class BoxTest extends TestCase
{
public function testCanBeCreatedFromValidUserStructure(): void
{
$this->assertInstanceOf(
Vox::class,
new Vox(
[
'id' => 69,
'type' => 12,
'label' => null,
'labelPosition' => 'up',
'isLinkEnabled' => true,
'isOnTop' => false,
'parentId' => null,
'width' => '0',
'height' => '0',
'x' => -666,
'y' => 76,
]
)
);
// $this->assertInstanceOf(
// Vox::class,
// Vox::fromArray(
// [
// 'id' => 1000,
// 'type' => 8,
// 'name' => 'test',
// 'width' => 100,
// 'height' => 900,
// ]
// )
// );
}
public function testContainerIsRepresentedAsJson(): void
{
$this->assertEquals(
'{"id":7,"type":12,"label":null,"labelPosition":"up","isLinkEnabled":true,"isOnTop":false,"parentId":null,"aclGroupId":null,"width":0,"height":0,"x":-666,"y":76,"borderWidth":0,"borderColor":null,"fillColor":null}',
new Vox(
[
'id' => 7,
'type' => 10,
'label' => null,
'labelPosition' => 'up',
'isLinkEnabled' => true,
'isOnTop' => false,
'parentId' => null,
'width' => '0',
'height' => '0',
'x' => -666,
'y' => 76,
]
)
);
}
}