PHPackages                             phore/hydrator - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. phore/hydrator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

phore/hydrator
==============

Unserialize arrays into object structure

v1.2.0(1y ago)04.5k↓33.3%2MITPHPPHP &gt;=7.2

Since Sep 2Pushed 1y ago1 watchersCompare

[ Source](https://github.com/phore/phore-hydrator)[ Packagist](https://packagist.org/packages/phore/hydrator)[ RSS](/packages/phore-hydrator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (2)Versions (5)Used By (2)

phore-hydrator
==============

[](#phore-hydrator)

serialize / unserialize plain into object structures. Hydrator parses the DocComments of public properties and instanciates the classes according to the definiton.

Examples:

- [hydrator-usage-example.php](doc/hydrator-usage-example.php)

Installation:

```
composer install phore/hydrator

```

Basic Example
-------------

[](#basic-example)

```
class UserData {
    /**
     * @var string
     */
    public $name;

    /**
    * Assoc Array
    * @var array
    */
    public $map;

    /**
     * @var int
     */
    public $age;
}

$input = ["name"=>"bob", "age"=>37];

$userData = phore_hydrate($input, UserData::class);

assert( $userData instanceof UserData);
```

`$userData` is a `UserData` Object and all properties casted correctly to desired types specified in DocComments.

Recognized Annotations
----------------------

[](#recognized-annotations)

- Simple types like `string`, `int`, `bool`, `float`, `array`
- Array types like `string[]`, `int[]`...
- Object types `OtherClass`
- Arrays of Objects `OtherClass[]`
- Nullable properties `type|null`

Guide
-----

[](#guide)

### Getters / Setters

[](#getters--setters)

On objects, hydrator will try to set property values in the following order:

1. If object has a `set($value)`-Method it will use it first
2. If the property is `public` it will be set directly
3. If there is a `__set($name, $value)` method it will be used

### Default Values

[](#default-values)

Default values will be applied if no data was found for the specific key

```
public $prop1 = []

```

### Optional Properties

[](#optional-properties)

You can define a property as optional by adding `|null` to the DocBlock.

```
/**
 * @var SomeEntity1|null
 */
public $entity1;

```

If the input data was not found, the value will be `null`.

### Filter input data before hydration

[](#filter-input-data-before-hydration)

To ease backwards compatibility issues, the magick `__hydrate()` method is called to prefilter the input data before it is hydrated.

```
class Entity1 {
    public $p1;

    public function __hydrate(array $input) : array
    {
        // .. modify input to match the object ..
        return $input;
    }
}
```

### Dealing with additional / undefined input data

[](#dealing-with-additional--undefined-input-data)

By default, on undefined input keys, hydrator will throw an exception. You can toggle this behaviour

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance41

Moderate activity, may be stable

Popularity21

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~529 days

Total

4

Last Release

493d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/878a384d056698a2400e4b7c8858db05a6caebb2c560e67151be36d46d58def0?d=identicon)[dermatthes](/maintainers/dermatthes)

---

Top Contributors

[![dermatthes](https://avatars.githubusercontent.com/u/13380559?v=4)](https://github.com/dermatthes "dermatthes (30 commits)")

### Embed Badge

![Health badge](/badges/phore-hydrator/health.svg)

```
[![Health](https://phpackages.com/badges/phore-hydrator/health.svg)](https://phpackages.com/packages/phore-hydrator)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
