Collection#

Meritum\Serialization\Resource\Collection

Wraps an iterable of domain objects alongside a SerializerInterface . Implements CollectionInterface. Supports cursor-based and offset-based pagination.

Constructor#

/** @param iterable<mixed> $data */
public function __construct(iterable $data, SerializerInterface $serializer)

The serializer is called once per element in $data when the formatter builds the collection.

Pagination#

public function setCursor(CursorInterface $cursor): static;

Attaches cursor pagination. Clears any previously set paginator. Produces limit, previous, and next keys in the envelope.

public function setPaginator(PaginatorInterface $paginator): static;

Attaches offset pagination. Clears any previously set cursor. Produces total, count, limit, current, and last keys in the envelope.

See CursorInterface and PaginatorInterface .

Meta#

public function addMeta(string $key, mixed $value): static;
public function getMeta(): array;

Attaches arbitrary metadata. Non-empty meta appears as a meta key in the envelope alongside data.

Example#

use Meritum\Serialization\Resource\Collection;

$resource = new Collection($this->users->paginate($page, $perPage), $this->serializer);
$resource->setPaginator($paginator);
$resource->addMeta('cached', false);

$envelope = $this->formatter->format($resource);

return new JsonResponse($envelope);