ArrayableInterface
Raxos\Contract\Collection\ArrayableInterface is the smallest collection contract in Raxos. Anything that can be reduced to a plain array, such as models, collections and value objects, implements it, so other code can accept it without knowing the concrete type.
Signature
php
interface ArrayableInterface
{
public function toArray(): array;
}The interface is templated (@template TKey of array-key, @template TValue), so toArray is documented as returning array<TKey, TValue>.
Methods
toArray(): array
Returns a plain array representation of the object.
Notes
- It is the base contract of the
Raxos\Contract\Collectionnamespace, and richer contracts such asArrayListInterfaceandMapInterfacebuild on it. See raxos/collection. - It is accepted as an input type across the query builder in raxos/database, for example anywhere a
whereIncall accepts anArrayableInterfaceof values instead of a raw array.
Example
php
<?php
declare(strict_types=1);
use Raxos\Contract\Collection\ArrayableInterface;
final readonly class Coordinates implements ArrayableInterface
{
public function __construct(
public float $latitude,
public float $longitude,
) {}
public function toArray(): array
{
return [
'latitude' => $this->latitude,
'longitude' => $this->longitude,
];
}
}