Skip to content

Commit

Permalink
Validation entities for keep standard convention.
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Jul 8, 2020
1 parent 0c03f6c commit 8cb3acf
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/Entity/BulkAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Baraja\StructuredApi\Entity;


interface BulkAction
{
/**
* @return string[] (action => label)
*/
public function getBulkActionsList(): array;

/**
* @param string $action
* @param string[] $ids
*/
public function postProcessBulkAction(string $action, array $ids): void;
}
65 changes: 65 additions & 0 deletions src/Entity/ItemsList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace Baraja\StructuredApi\Entity;


final class ItemsList
{

/** @var ItemsListItem[] */
private $items;


/**
* @param ItemsListItem[] $items
*/
public function __construct(array $items = [])
{
$this->items = $items;
}


/**
* @param ItemsListItem[] $items
* @return self
*/
public static function from(array $items): self
{
return new self($items);
}


/**
* @return mixed[][]
*/
public function getData(): array
{
$return = [];
foreach ($this->items as $item) {
$return[] = array_merge($item->getData(), [
'id' => $item->getId(),
]);
}

return $return;
}


public function addItem(ItemsListItem $item): self
{
$this->items[] = $item;

return $this;
}


/**
* @return ItemsListItem[]
*/
public function getItems(): array
{
return $this->items;
}
}
45 changes: 45 additions & 0 deletions src/Entity/ItemsListItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Baraja\StructuredApi\Entity;


class ItemsListItem
{

/** @var string */
private $id;

/** @var mixed[] */
private $data;


/**
* @param string $id
* @param mixed[] $data
*/
public function __construct(string $id, array $data = [])
{
$this->id = $id;
$this->data = $data;
}


/**
* @return string
*/
public function getId(): string
{
return $this->id;
}


/**
* @return mixed[]
*/
public function getData(): array
{
return $this->data;
}
}
61 changes: 61 additions & 0 deletions src/Entity/StatusCount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Baraja\StructuredApi\Entity;


use Nette\Utils\Strings;

final class StatusCount
{

/** @var string */
private $key;

/** @var string */
private $label;

/** @var int */
private $count;


/**
* @param string $key
* @param int $count
* @param string $label
*/
public function __construct(string $key, int $count, ?string $label = null)
{
$this->key = $key;
$this->count = $count;
$this->label = $label ?? Strings::firstUpper(str_replace('-', ' ', $key));
}


/**
* @return string
*/
public function getKey(): string
{
return $this->key;
}


/**
* @return string
*/
public function getLabel(): string
{
return $this->label;
}


/**
* @return int
*/
public function getCount(): int
{
return $this->count;
}
}
20 changes: 19 additions & 1 deletion src/Response/BaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Baraja\StructuredApi;


use Baraja\StructuredApi\Entity\ItemsList;
use Baraja\StructuredApi\Entity\StatusCount;
use Nette\Utils\Paginator;
use Tracy\Debugger;
use Tracy\ILogger;
Expand Down Expand Up @@ -128,8 +130,14 @@ private function process($haystack, array $trackedInstanceHashes = [])
{
if (\is_array($haystack) === true) {
$return = [];

foreach ($haystack as $key => $value) {
if ($value instanceof ItemsList && $key !== 'items') {
throw new \InvalidArgumentException('Convention error: Item list must be in key "items", but "' . $key . '" given.');
}
if ($value instanceof Paginator && $key !== 'paginator') {
throw new \InvalidArgumentException('Convention error: Paginator must be in key "paginator", but "' . $key . '" given.');
}

$return[$key] = $this->hideKey($key, $value) ? self::$hiddenKeyLabel : $this->process($value);
}

Expand All @@ -152,6 +160,16 @@ private function process($haystack, array $trackedInstanceHashes = [])
'isLastPage' => $haystack->isLast(),
];
}
if ($haystack instanceof StatusCount) {
return [
'key' => $haystack->getKey(),
'label' => $haystack->getLabel(),
'count' => $haystack->getCount(),
];
}
if ($haystack instanceof ItemsList) {
return $haystack->getData();
}
if (\method_exists($haystack, '__toString') === true) {
return (string) $haystack;
}
Expand Down

0 comments on commit 8cb3acf

Please sign in to comment.