meow / hydrator
v1.3.0
2023-11-04 22:02 UTC
Requires
- php: >=8.0
Requires (Dev)
- phpstan/phpstan: ^1.1
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.6
This package is auto-updated.
Last update: 2024-12-05 10:35:38 UTC
README
namespace: Meow\Hydrator
Library that can hydrate (fill object with data from array) and extract data from object back to array.
Installation
To install this library into your project use composer script:
composer require meow/hydrator
Usage
Hydrate
Filling object from array
protected array $testModelData = [ "name" => "May", 'email' => 'may@locahlost.tld' ]; // ... $hydrator = new Hydrator(); $testModel = $hydrator->hydrate(TestModel::class, $this->testModelData);
Extract
Extraction properties and their values back to the array.
$testModelDataArray = $hydrator->extract($testModel, ['name', 'email']);
Arrays of objects
You can now parse arrays of objects by defining them with the attribute ArrayOf
. Check following snippet
// ... use Meow\Hydrator\Attributes\ArrayOf; class Character { // ... protected Equipment $equipment; #[ArrayOf(Weapon::class)] protected array $inventory; // ... }
License: MIT