First inital test commit
This commit is contained in:
47
src/Objects/ApiListTransformer.php
Normal file
47
src/Objects/ApiListTransformer.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Ardent\GrocyApi\Objects;
|
||||
|
||||
|
||||
class ApiListTransformer
|
||||
{
|
||||
private static function list(string $type): ?string
|
||||
{
|
||||
return match ($type) {
|
||||
Category::getType() => Category::class,
|
||||
SingleArticle::getType() => SingleArticle::class,
|
||||
SingleArticleDetails::getType() => SingleArticleDetails::class,
|
||||
Order::getType() => Order::class,
|
||||
OrderLine::getType() => OrderLine::class,
|
||||
OrderArticle::getType() => OrderArticle::class,
|
||||
Delivery::getType() => Delivery::class,
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
|
||||
public static function getObjects(array $items): array
|
||||
{
|
||||
$objects = [];
|
||||
foreach ($items as $item) {
|
||||
$object = self::getObject($item);
|
||||
if ($object) {
|
||||
$objects[] = $object;
|
||||
}
|
||||
}
|
||||
return $objects;
|
||||
}
|
||||
|
||||
public static function getObject(mixed $item): mixed
|
||||
{
|
||||
$name = self::list($item['type']);
|
||||
if ($name) {
|
||||
/** @var AbstractApiObject $name */
|
||||
return $name::fromApi($item);
|
||||
} else {
|
||||
return [];
|
||||
printf('Unknown object with type "%s"' . PHP_EOL, $item['type']);
|
||||
dd($item);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user