Download the PHP package hank21014/synth without Composer
On this page you can find all versions of the php package hank21014/synth. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hank21014/synth
More information about hank21014/synth
Files in hank21014/synth
Package synth
Short Description A lightweight PHP utility for data dehydration and hydration.
License MIT
Informations about the package synth
💧 PHP Synth: Seamless Data Hydration
Synth is a lightweight PHP utility designed to bridge the gap between complex objects and serializable arrays. By using custom "Synth," you can dehydrate objects into structured arrays and hydrate them back into their original state with type safety and zero fuss.
🌟 Why Synth?
-
Architecture Friendly: Keep your Entities/DTOs "lean" by moving transformation logic into dedicated Synth classes.
- Custom Mapping: Decouple your internal object structure from your external data schema (API, Cache, or Queue).
🛠️ Installation
Install the package via Composer:
🚀 Quick Start
1. Define your Entity
A plain PHP class. No traits or inheritance required.
2. Create a Synth
Implement the Synth interface to define how your object should be transformed.
3. Usage
Register your Synth and start transforming data.
💡 Key Concepts
Dehydration
Taking a "living" object and drying it out into a portable array format (perfect for JSON or Database storage).
Hydration
Taking that array data and "watering" it back into a PHP object.
The Metadata Wrapper
Synth automatically wraps your payload with a [SYNTH] flag and a type key. This allows the SynthManager to automatically know which class to instantiate during hydration.
[!TIP] Balance Uniqueness vs. Length
When customizing the identity key, choose a value that is unique enough to avoid data collisions but short enough to keep your dehydrated payloads efficient.
Primitive Handling
Synth handles non-object types via a built-in PrimitiveSynth. This ensures that your basic data structures remain intact while objects are being transformed.
| Type | Strategy | Detail |
|---|---|---|
| Scalar/Null | Pass-through | Values like int, float, string, bool or null are returned without modification. |
| Standard Array | Recursive | Every element inside the array is checked and processed by the SynthManager. |
| Synthesized Array | Delegated | Arrays containing the [SYNTH] flag are passed to their respective specialized Synths. |
⚙️ Advanced Configuration
Customizing the Identity Key
Configuring Global Synths
| Synth comes with built-in support for common PHP types: | Synth Class | Target Type |
|---|---|---|
| \Hank21014\Synth\Synths\DateTimeSynth | \DateTime | |
| \Hank21014\Synth\Synths\DateTimeImmutableSynth | \DateTimeImmutable |
To disable all global synths:
To customize global synths:
[!IMPORTANT] Global configuration must be performed before the SynthManager is initialized. Calling these methods after instantiation will have no effect on existing manager instances.