meow/hydrator

Fund package maintenance!
maymeow
Ko Fi

v1.3.0 2023-11-04 22:02 UTC

README

namespace: Meow\Hydrator

Library that can hydrate (fill object with data from array) and extract data from object back to array.

ko-fi

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