PHPackages                             drabek-digital/hydra - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. drabek-digital/hydra

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

drabek-digital/hydra
====================

🌊 Modern PHP serialization, deserialization and validation library using PHP 8.4 attributes

v0.0.1(1mo ago)00MITPHPPHP &gt;=8.5CI passing

Since May 28Pushed 1mo agoCompare

[ Source](https://github.com/DrabekDigital/hydra)[ Packagist](https://packagist.org/packages/drabek-digital/hydra)[ RSS](/packages/drabek-digital-hydra/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (1)Dependencies (5)Versions (2)Used By (0)

Hydra 🐉
=======

[](#hydra-)

This is a serialization &amp; deserialization &amp; validation library with minimum dependencies to allow you easily guard the app's valuable asset - data integrity. All that in PHP using top-notch PHP 8.4 language features.

The main principles of this approach are:

- Specification of serialization &amp; deserialization &amp; validation via PHP attributes in the DTOs
- First pass generates a DTOs hierarchy with hydration/dehydration logic in place through normal PHP code (`isset`, `array_key_exists`, `is_string`, `is_int`, ...) and caches generated runtime for subsequent runs.
- Validation is a separate runtime flow (`Validator`) and is not code-generated.
- Attributes should be specified above properties only when needed (in other words native typing should be used when possible)
- There should be sane defaults for most of the situations.
- Validation &amp; dehydration &amp; hydration are recursive.

Limitations:

- Circular references are not supported.
- Type coercion is prevented by design.
- `readonly` DTOs without constructor parameters are not currently supported for hydration. Use constructor-based `readonly` DTOs (promoted params), or non-`readonly` public properties for property-only DTOs.

Naming strategy
---------------

[](#naming-strategy)

Bidirectional way how the fields are mapped during serialization &amp; deserialization.

- Default (1:1 mapping)
- `UnderscoreCase`: use when in serialized format you have `underscore_case` while members are named in `camelCase`

Also there is possibility to rename a particular field (for legacy named fields) by using `Rename` attribute. `Rename` is direction-aware: use `from` for hydration (input) and `to` for dehydration (output).

Mappers
-------

[](#mappers)

- Scalars
    - `bool`
    - `float`
    - `int`
    - `mixed`
    - `string`

Special types:

- `DateTimeInterface` and `DateTimeImmutable`
- `BackedEnum`
- Array and lists
    - `array` (of anything)
    - `List`of particular value type (with discriminator)
    - `Map` of particular key &amp; value type (with discriminator)
- Shapes
    - `Object` of particular type (with discriminator)
- Custom mappers
    - `CustomMapper` of particular type (or your own attribute implementing `MapperAttribute` interface)

Validators
----------

[](#validators)

- Integer &amp; float
    - Range (min, max, inclusive, exclusive) - `NumberRange`
- Strings
    - `LengthRange` (min, max) with one particular alias `NonEmptyString`
    - Regex - `StringMatches`
    - URL
- Array
    - Count (min, max)
    - Unique items (including custom comparator)
- Date &amp; time
    - `DateTimeRange` (min, max, inclusive, exclusive)
- Custom

---

Usage
-----

[](#usage)

```
