battis/hydratable

Hydrate serialized objects with defaults and overrides

Maintainers

👁 battis

Package info

github.com/battis/hydratable

pkg:composer/battis/hydratable

Statistics

Installs: 654

Dependents: 2

Suggesters: 0

Stars: 0

Open Issues: 0

v0.1.1 2023-07-04 21:21 UTC

Requires

  • php: >=7.3

Requires (Dev)

None

Suggests

None

Provides

None

Conflicts

None

Replaces

None

GPL-3.0 a8ab2428b070dbe1f2143c7783898237727f3c79

  • Seth Battis <seth.woop@battis.net>

This package is auto-updated.

Last update: 2026-06-17 02:31:57 UTC


README

👁 Latest Version
👁 codecov

Hydrate serialized objects with defaults and overrides

This is meant to make it easier to take DRY arguments and hydrate them based on preset defaults.

Install

composer require battis/hydratable

Use

This can either be added as a trait to a class or as an invokable class.

use Battis\Hydratable\Hydratable;

class MyObject
{
 use Hydratable;

 private static $DEFAULTS = [
 'foo' => 'bar',
 'argle' => 'bargle'
 ]

 private $options;

 public function __construct(array $params = [])
 {
 $this->options = $this->hydrate($params, self::$DEFAULTS);
 }
}

One could then instantiate an instance of MyObject:

$o = new MyObject(['baz' => 123, 'argle' = 'BaRgLe']);

/*
$o->options = [
 'foo' => 'bar',
 'baz' => 123,
 'argle' => 'BaRgLe'
]
*/

Alternatively, we could simply instantiate Hydrate and use it as a one-off:

$hydrate = new Battis\Hydratable\Hydrate();
$options = $hydrate($params, $defaults);