https://github.com/yiisoft/hydrator
Create and populate objects with type casting, mapping and dependencies resolving support.
https://github.com/yiisoft/hydrator
hydrator yii3
Last synced: about 1 month ago
JSON representation
Create and populate objects with type casting, mapping and dependencies resolving support.
- Host: GitHub
- URL: https://github.com/yiisoft/hydrator
- Owner: yiisoft
- License: bsd-3-clause
- Created: 2023-04-27T10:01:18.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2025-04-21T11:22:21.000Z (about 2 months ago)
- Last Synced: 2025-04-30T19:39:42.367Z (about 1 month ago)
- Topics: hydrator, yii3
- Language: PHP
- Homepage:
- Size: 223 KB
- Stars: 27
- Watchers: 15
- Forks: 3
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
Yii Hydrator
[](https://packagist.org/packages/yiisoft/hydrator)
[](https://packagist.org/packages/yiisoft/hydrator)
[](https://github.com/yiisoft/hydrator/actions/workflows/build.yml)
[](https://codecov.io/gh/yiisoft/hydrator)
[](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/hydrator/master)
[](https://github.com/yiisoft/hydrator/actions?query=workflow%3A%22static+analysis%22)
[](https://shepherd.dev/github/yiisoft/hydrator)
[](https://shepherd.dev/github/yiisoft/hydrator)The package provides a way to create and hydrate objects from a set of raw data.
Features are:
- supports properties of any visibility;
- uses constructor arguments to create objects;
- resolves dependencies when creating objects using [PSR-11](https://www.php-fig.org/psr/psr-11/) compatible DI container
provided;
- supports nested objects;
- supports mapping;
- allows fine-tuning hydration via PHP attributes.## Requirements
- PHP 8.1 or higher.
## Installation
The package could be installed with [Composer](https://getcomposer.org):
```shell
composer require yiisoft/hydrator
```## General usage
To hydrate existing object:
```php
use Yiisoft\Hydrator\Hydrator;$hydrator = new Hydrator();
$hydrator->hydrate($object, $data);
```To create a new object and fill it with the data:
```php
use Yiisoft\Hydrator\Hydrator;$hydrator = new Hydrator();
$object = $hydrator->create(MyClass::class, $data);
```To pass arguments to the constructor of a nested object, use nested array or dot-notation:
```php
final class Engine
{
public function __construct(
private string $name,
) {}
}final class Car
{
public function __construct(
private string $name,
private Engine $engine,
) {}
}// nested array
$object = $hydrator->create(Car::class, [
'name' => 'Ferrari',
'engine' => [
'name' => 'V8',
]
]);// or dot-notation
$object = $hydrator->create(Car::class, [
'name' => 'Ferrari',
'engine.name' => 'V8',
]);
```That would pass the `name` constructor argument of the `Car` object and create a new `Engine` object for `engine`
argument passing `V8` as the `name` argument to its constructor.## Documentation
- Guide: [English](docs/guide/en/README.md), [Português - Brasil](docs/guide/pt-BR/README.md), [Русский](docs/guide/ru/README.md)
- [Internals](docs/internals.md)If you need help or have a question, the [Yii Forum](https://forum.yiiframework.com/c/yii-3-0/63) is a good place for that.
You may also check out other [Yii Community Resources](https://www.yiiframework.com/community).## License
The Yii Hydrator is free software. It is released under the terms of the BSD License.
Please see [`LICENSE`](./LICENSE.md) for more information.Maintained by [Yii Software](https://www.yiiframework.com/).
## Support the project
[](https://opencollective.com/yiisoft)
## Follow updates
[](https://www.yiiframework.com/)
[](https://twitter.com/yiiframework)
[](https://t.me/yii3en)
[](https://www.facebook.com/groups/yiitalk)
[](https://yiiframework.com/go/slack)