После основного flush'а проверять содержимое коллекции...
symfony/property-info
- нужен для десериализации, при сериализации, в вашем случае, он бесполезен. Все эти пакеты ставятся автоматически, если используете flex. В этом случае используется symfony/serializer-pack composer create-project symfony/skeleton toster-827449
cd toster-827449
composer req serialzier
class PaginatedCollection
{
private $items;
private $total;
private $count;
private $links = [];
public function __construct($items, $totalItems)
{
$this->items = $items;
$this->total = $totalItems;
$this->count = count($items);
}
public function addLink($ref, $url)
{
$this->links[$ref] = $url;
}
public function getItems()
{
return $this->items;
}
public function getTotal()
{
return $this->total;
}
public function getCount(): int
{
return $this->count;
}
public function getLinks(): array
{
return $this->links;
}
}
class Item
{
private $id;
private $name;
public function __construct(string $name)
{
$this->id = uniqid();
$this->name = $name;
}
public function getId(): string
{
return $this->id;
}
public function getName(): string
{
return $this->name;
}
}
class SiteController extends AbstractController
{
public function indexAction(): JsonResponse
{
$items = [
new Item('Item 1'),
new Item('Item 2'),
new Item('Item 3'),
];
$collection = new PaginatedCollection($items, count($items));
return $this->json($collection);
}
}
{
"items": [
{
"id": "5f33b6f964898",
"name": "Item 1"
},
{
"id": "5f33b6f96489e",
"name": "Item 2"
},
{
"id": "5f33b6f96489f",
"name": "Item 3"
}
],
"total": 3,
"count": 3,
"links": []
}
api_controllers:
resource: ../../src/Controller/Api
defaults:
_format: json