Hydrator

This library enables seamless hydration of objects to arrays - and back again. It is optimized for both developer experience and performance and works with final, readonly classes, constructor property promotion and deeply nested structures.

Hydration is handled through normalizers, especially for complex data types. The library automatically determines the appropriate normalizer based on the property type and attributes, so in most cases no manual configuration is needed. And if customization is required, it can be done easily using attributes.

Features

  • Extract objects to arrays and hydrate them back, without calling the constructor.
  • Automatic normalizer resolution for enums, date types, collections, array shapes and nested objects.
  • Rename or exclude fields with attributes.
  • Lazy hydration of objects with PHP 8.4 lazy proxies.
  • Pluggable guessers to pick normalizers for your own value objects.
  • Extensions with middlewares and metadata enrichers to hook into the process.
  • Lifecycle hooks before extracting and after hydrating.
  • Encrypt and decrypt sensitive data with the cryptography extension (crypto-shredding).
  • Upcast outdated stored data while it is hydrated.
  • Cache the metadata with any PSR-6 or PSR-16 cache.

Installation

composer require patchlevel/hydrator

Integration

  • Event Sourcing - the hydrator powers the storage and retrieval of thousands of events and aggregates.
  • ODM - a lightweight object document mapper for MongoDB and PostgreSQL that builds on the hydrator for fast object mapping and full extension support.

New here? Start with the getting started guide and build your first hydrator in a few minutes.