PHPackages                             nutgram/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. nutgram/hydrator

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

nutgram/hydrator
================

Hydrator for PHP 8.0+

7.1.0(2w ago)12346.2k—2.1%15MITPHPPHP ^8.1CI passing

Since Apr 23Pushed 2w ago2 watchersCompare

[ Source](https://github.com/nutgram/hydrator)[ Packagist](https://packagist.org/packages/nutgram/hydrator)[ Docs](https://github.com/nutgram/hydrator)[ RSS](/packages/nutgram-hydrator/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (6)Versions (18)Used By (5)

Strongly typed hydrator for PHP 8.0+
====================================

[](#strongly-typed-hydrator-for-php-80)

---

Fork of the original project .

Installation
------------

[](#installation)

```
composer require nutgram/hydrator
```

How to use?
-----------

[](#how-to-use)

```
use SergiX44\Hydrator\Hydrator;

$hydrator = new Hydrator();

// create and hydrate an object with an array
$data = [/* the class props here */];
$object = $hydrator->hydrate(SomeDto::class, $data);

// hydrate an object with an array
$data = [/* the class props here */];
$hydrator->hydrate($object, $data);

// creates and hydrate an object with JSON
$json = '{...}';
$object = $hydrator->hydrateWithJson(SomeDto::class, $json);

// hydrate an object with JSON
$json = '{...}';
$hydrator->hydrateWithJson($object, $json);

// pass JSON decoding flags
$options = JSON_OBJECT_AS_ARRAY|JSON_BIGINT_AS_STRING;
$hydrator->hydrateWithJson($object, $json, $options);
```

Allowed property types
----------------------

[](#allowed-property-types)

### Required

[](#required)

If a property has no a default value, then the property is required.

```
public string $value;
```

### Optional

[](#optional)

If a property has a default value, then the property is optional.

```
public string $value = 'foo';
```

### Null

[](#null)

If a property is nullable, then the property can accept null.

```
public ?string $value;
```

If the property should be optional, then it must has a default value.

```
public ?string $value = null;
```

### Boolean

[](#boolean)

Accepts the following values: true, false, 1, 0, "1", "0", "yes", "no", "on" and "no".

```
public bool $value;
```

```
['value' => true];
['value' => 'yes'];
```

Integer
-------

[](#integer)

Accepts only integers (also as a string).

```
public int $value;
```

```
['value' => 42];
['value' => '42'];
```

Number&lt;int|float&gt;
-----------------------

[](#numberintfloat)

Accepts only numbers (also as a string).

```
public float $value;
```

```
['value' => 42.0];
['value' => '42.0'];
```

String
------

[](#string)

Accepts only strings.

```
public string $value;
```

```
['value' => 'foo'];
```

Array&lt;array-key, mixed&gt;
-----------------------------

[](#arrayarray-key-mixed)

Accepts only arrays.

```
public array $value;
```

```
['value' => [1, 2, 'foo']];
```

Array&lt;array-key, class&gt;
-----------------------------

[](#arrayarray-key-class)

Accept a list of objects.

```
final class SomeDto {
    public readonly string $value;
}
```

```
use SergiX44\Hydrator\Annotation\ArrayType;

#[ArrayType(SomeDto::class)]
public array $value;
```

```
[
    'value' => [
        [
            'value' => 'foo',
        ],
        [
            'value' => 'bar',
        ],
    ],
],
```

Object
------

[](#object)

Accepts only objects.

```
public object $value;
```

```
['value' => new stdClass];
```

DateTime/DateTimeImmutable
--------------------------

[](#datetimedatetimeimmutable)

Integers (also as a string) will be handled as a timestamp, otherwise accepts only valid date-time strings.

```
public DateTimeImmutable $value;
```

```
// 2010-01-01
['value' => 1262304000];
// 2010-01-01
['value' => '1262304000'];
// normal date
['value' => '2010-01-01'];
```

DateInterval
------------

[](#dateinterval)

Accepts only valid date-interval strings based on ISO 8601.

```
public DateInterval $value;
```

```
['value' => 'P1Y']
```

Enum
----

[](#enum)

Accepts only values that exist in an enum.

```
enum SomeEnum: int {
    case foo = 0;
    case bar = 1;
}
```

```
public SomeEnum $value;
```

```
['value' => 0]
['value' => '1']
```

Association
-----------

[](#association)

Accepts a valid structure for an association

```
final class SomeDto {
    public string $value;
}
```

```
public SomeDto $value;
```

```
[
    'value' => [
        'value' => 'foo',
    ],
]
```

Property alias
--------------

[](#property-alias)

If you need to get a non-normalized key, use aliases.

For example, the Google Recaptcha API returns the following response:

```
{
    "success": false,
    "error-codes": []
}
```

To correctly map the response, use the following model:

```
use SergiX44\Hydrator\Annotation\Alias;

final class RecaptchaVerificationResult {
    public bool $success;

    #[Alias('error-codes')]
    public array $errorCodes = [];
}
```

Examples
--------

[](#examples)

```
final class Product {
    public string $name;
    public Category $category;
    #[ArrayType(Tag::class)]
    public array $tags;
    public Status $status;
}

final class Category {
    public string $name;
}

final class Tag {
    public string $name;
}

enum Status: int {
    case ENABLED = 1;
    case DISABLED = 0;
}
```

```
$product = $hydrator->hydrate(Product::class, [
    'name' => 'Stool',
    'category' => [
        'name' => 'Furniture',
    ],
    'tags' => [
        [
            'name' => 'Wood',
        ],
        [
            'name' => 'Lacquered',
        ],
    ],
    'status' => 0,
]);
```

---

Test run
--------

[](#test-run)

```
composer test
```

###  Health Score

56

—

FairBetter than 97% of packages

Maintenance82

Actively maintained with recent releases

Popularity43

Moderate usage in the ecosystem

Community20

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 67.7% 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 ~100 days

Recently: every ~123 days

Total

16

Last Release

19d ago

Major Versions

2.0.0 → 3.0.02022-11-14

3.0.0 → 4.0.02023-02-07

4.0.1 → 5.0.02023-06-27

5.0.1 → 6.0.02024-01-07

6.0.2 → 7.0.02025-06-21

PHP version history (2 changes)1.0.0PHP ^8.0

5.0.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/ba98a3b5b1a03bc4c37de57b9d2aefbba31c73570e10df5833a50ad4106fc583?d=identicon)[Lukasss93](/maintainers/Lukasss93)

![](https://www.gravatar.com/avatar/08bc14acf1f7db717de09e715047f5b5684841d6b9712129334931fa8833beda?d=identicon)[SergiX44](/maintainers/SergiX44)

---

Top Contributors

[![sergix44](https://avatars.githubusercontent.com/u/4172890?v=4)](https://github.com/sergix44 "sergix44 (86 commits)")[![Lukasss93](https://avatars.githubusercontent.com/u/4071613?v=4)](https://github.com/Lukasss93 "Lukasss93 (41 commits)")

---

Tags

data-transfer-objectdtoenumhacktoberfesthydratorjsonjsonmappermapperphpphp8mapperhydratordata mapperdtophp8jsonmapper

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[symfony/dependency-injection

Allows you to standardize and centralize the way objects are constructed in your application

4.2k455.6M9.6k](/packages/symfony-dependency-injection)[illuminate/contracts

The Illuminate Contracts package.

706130.3M13.3k](/packages/illuminate-contracts)[illuminate/container

The Illuminate Container package.

31182.0M2.4k](/packages/illuminate-container)[cuyz/valinor

Dependency free PHP library that helps to map any input into a strongly-typed structure.

1.5k13.2M174](/packages/cuyz-valinor)[ecotone/ecotone

Enterprise architecture layer for Laravel and Symfony — CQRS, Event Sourcing, Durable Workflows (Sagas, Orchestrators), Projections, and Outbox messaging via PHP attributes.

564576.7k52](/packages/ecotone-ecotone)[civicrm/civicrm-core

Open source constituent relationship management for non-profits, NGOs and advocacy organizations.

751291.4k43](/packages/civicrm-civicrm-core)

PHPackages © 2026

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