eventsauce/object-hydrator
Magic-less object hydration and serialization for PHP. Map arrays/decoded JSON to DTOs, commands, queries, and events by inspecting constructors and public getters—no private-property reflection. Supports custom keys, casting, and optional code generation for speed.
PropertyAccess, which Laravel already uses (via symfony/property-access), minimizing dependency bloat.Order, UserProfile) rather than Eloquent models (which already handle hydration).HydratorInterface, HydrationContext) and custom mappings for edge cases.fillable, guarded).FormRequest to use this hydrator?PropertyAccess?User/Post models.Invoice, Payment).new Class($data) with hydrator in one service layer.Request->validate() + manual assignment).FormRequest to auto-hydrate payloads:
$request->hydrate(Payment::class); // Returns Payment object
Model classes unless extending a hybrid domain model.FormRequest::rules() for complex objects.| Phase | Task | Dependencies | Risk |
|---|---|---|---|
| 1 | Add package + basic hydrator | PHP 8.0+, Composer | Low |
| 2 | Hydrate API requests | Laravel validation rules | Medium |
| 3 | Replace manual object creation in services | Domain model stability | High |
| 4 | Integrate with event sourcing (if applicable) | EventSaucePHP/EventSourcing | High |
| 5 | Deprecate legacy hydration patterns | Full test coverage | Medium |
unset($obj->readOnlyField) or new Class($data).| Scenario | Impact | Mitigation |
|---|---|---|
| Missing Required Field | Hydration fails silently (unless validated) | Wrap in try-catch or use Symfony Validator pre-hydration. |
| Type Mismatch | Invalid object state (e.g., string instead of DateTime) |
Use PHP 8.1+ union types or runtime assertions. |
| Circular References | Infinite recursion in nested objects | Implement cyclic reference detection in hydrator. |
| Schema Drift | Hydrator breaks if DB/API changes | Automated tests for hydration + migration checks. |
| Laravel Upgrade | Breaks if using PHP 8.0+ features not backward-compatible | Pin PHP version in composer.json. |
How can I help you explore Laravel packages today?