componenta/arrayable

Array conversion contract for Componenta packages

Maintainers

Package info

github.com/componenta/arrayable

pkg:composer/componenta/arrayable

Statistics

Installs: 7

Dependents: 18

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-06-15 10:55 UTC

This package is auto-updated.

Last update: 2026-06-15 12:06:31 UTC


README

Minimal contract for objects that expose a public array representation.

Use it when a package needs to accept DTOs, view models, pagination results, or collections without depending on a serializer implementation.

Installation

composer require componenta/arrayable

Related Packages

Package Why it matters here
componenta/array to_array() consumes Arrayable objects.
componenta/http-responder Responders can turn arrayable objects into JSON responses.
componenta/paginator Pagination results can expose a public array shape.

Usage

use Componenta\Arrayable\Arrayable;

final readonly class UserView implements Arrayable
{
    public function __construct(
        public string $id,
        public string $name,
    ) {}

    public function toArray(): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
        ];
    }
}

Contract

Arrayable::toArray() returns the public array representation of an object.

The interface does not define:

  • JSON encoding
  • recursive conversion rules
  • key naming conventions
  • whether private/internal state is included

Those decisions belong to the implementing object or to a serializer package.

Typical Consumers

Packages such as paginator, iterator helpers, HTTP responders, and config exporters can depend on this contract when they only need an array shape and do not need object-specific behavior.