Repository navigation
ArraySerializableData
Viames Marino edited this page Apr 22, 2026
·
1 revision
Pair\Data\ArraySerializableData is a tiny trait for read models whose JSON representation should be the same as toArray().
It implements jsonSerialize() by returning toArray().
use Pair\Data\ArraySerializableData;
use Pair\Data\ReadModel;
/**
* Public status payload for JSON responses and page state.
*/
final readonly class StatusReadModel implements ReadModel {
use ArraySerializableData;
/**
* Build the status payload.
*/
public function __construct(
public string $status,
public int $count
) {}
/**
* Export the status payload.
*
* @return array<string, mixed>
*/
public function toArray(): array {
return [
'status' => $this->status,
'count' => $this->count,
];
}
}Then both paths produce the same public shape:
$status = new StatusReadModel('ok', 3);
$status->toArray();
json_encode($status);Use the trait when:
- the read model implements
ReadModel -
toArray()is already the public JSON contract - no custom
jsonSerialize()behavior is needed
Avoid it when JSON should intentionally differ from the array representation.
See also: ReadModel, MapsFromRecord, JsonResponse.
Home · Setup · Configuration
Build with Pair 4
- Generator
- Controllers and responses
- Read models
- ActiveRecord and queries
- Forms and validation
- PairUI.js and HTML fragments
APIs and authentication
- API guide and CRUD
- Middleware and rate limits
- Users, sessions, and ACL
- Passkeys and social login
- Native mobile setup
Integrate and operate
- Integrations
- Runtime extensions and adapter keys
- Cache and cache stores
- Logs and observability
- PWA and push
- Packages and distribution
- Upgrade to v4