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.0.2(5mo ago)12265.2k—7.7%15MITPHPPHP ^8.1CI passing

Since Apr 23Pushed 5mo 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 1mo ago

READMEChangelog (10)Dependencies (3)Versions (17)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

52

—

FairBetter than 96% of packages

Maintenance70

Regular maintenance activity

Popularity42

Moderate usage in the ecosystem

Community19

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 69.9% 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 ~93 days

Recently: every ~124 days

Total

15

Last Release

176d 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 (37 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

[cuyz/valinor

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

1.5k9.2M108](/packages/cuyz-valinor)[eventsauce/object-hydrator

Converts structured data into strict objects.

3322.3M20](/packages/eventsauce-object-hydrator)[symfonycasts/micro-mapper

A tiny, underwhelming data mapper to map one object to another!

82597.6k](/packages/symfonycasts-micro-mapper)[butschster/cron-expression-generator

Cron expression generator

511.4M2](/packages/butschster-cron-expression-generator)[acelot/automapper

Powerful declarative data mapper for PHP 8

694.2k1](/packages/acelot-automapper)[wayofdev/laravel-cycle-orm-adapter

🔥 A Laravel adapter for CycleORM, providing seamless integration of the Cycle DataMapper ORM for advanced database handling and object mapping in PHP applications.

3516.7k3](/packages/wayofdev-laravel-cycle-orm-adapter)

PHPackages © 2026

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