44 lines
1.1 KiB
PHP
44 lines
1.1 KiB
PHP
<?php
|
|
|
|
|
|
namespace Ardent\PicnicApi\Objects;
|
|
|
|
|
|
class ApiListTransformer
|
|
{
|
|
private static function list(string $type): ?string
|
|
{
|
|
return match ($type) {
|
|
Category::getType() => Category::class,
|
|
SingleArticle::getType() => SingleArticle::class,
|
|
Order::getType() => Order::class,
|
|
OrderLine::getType() => OrderLine::class,
|
|
OrderArticle::getType() => OrderArticle::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 {
|
|
printf('Unknown object with type "%s"' . PHP_EOL, $item['type']);
|
|
dd($item);
|
|
}
|
|
}
|
|
} |