Download the PHP package meritum/serialization without Composer
On this page you can find all versions of the php package meritum/serialization. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download meritum/serialization
More information about meritum/serialization
Files in meritum/serialization
Package serialization
Short Description Pluggable object serialization for the Meritum ecosystem with item, collection, and pagination support
License MIT
Informations about the package serialization
meritum/serialization
Pluggable object serialization with item, collection, and pagination support.
Requirements
- PHP 8.4+
georgeff\kernel^1.6
Installation
Overview
The package is built around three concepts:
- Serializer — transforms a model into a plain array
- Resource — wraps a model (or iterable) with its serializer and optional metadata
- Formatter — accepts a resource, applies a format strategy, and returns a
JsonSerializableenvelope
The format strategy controls the output shape. Two strategies are provided out of the box, and you can supply your own by implementing StrategyInterface.
Serializers
Implement SerializerInterface to define how a model maps to an array:
To embed a related model, call the related serializer's serialize() directly. The formatter does not recurse — related data must be resolved to a plain array inside serialize():
Formatter
Formatter is the entry point. Construct it with a StrategyInterface and call format() with an Item or Collection:
Item
Wrap a single model in an Item:
With DataArrayStrategy (the default), the output is:
Collection
Wrap an iterable of models in a Collection. Any iterable is accepted, including generators:
Output:
Metadata
Both Item and Collection accept call-site metadata via addMeta(). Multiple calls chain fluently. The meta key is omitted from the output entirely when empty:
Pagination
Cursor pagination
Implement CursorInterface and attach it to a Collection with setCursor():
Offset pagination
Implement PaginatorInterface and attach it with setPaginator():
setCursor() and setPaginator() are mutually exclusive — setting one clears the other. Pagination keys always appear before data in the output.
A bridge package for meritum/database paginators will provide concrete implementations of both interfaces.
Output strategies
DataArrayStrategy (default)
Items are wrapped under a data key. Collections always include a data key. This is the default strategy registered by SerializationModule.
ArrayStrategy
Items are returned as a bare array with no data wrapper. Collections behave the same as DataArrayStrategy — the difference only applies to items.
Custom strategies
Implement StrategyInterface to take full control of the output format:
Kernel integration
SerializationModule registers FormatterInterface in the kernel container. It uses DataArrayStrategy by default. To swap in a custom strategy, register StrategyInterface before booting:
To run multiple formatters with different strategies in the same application, bypass the module and define them manually:
License
MIT