2017 © Pedro Peláez
 

library fluid-hydrator

A fluid builder for MetaHydrator class

image

thecodingmachine/fluid-hydrator

A fluid builder for MetaHydrator class

  • Thursday, January 11, 2018
  • by mouf
  • Repository
  • 0 Watchers
  • 3 Stars
  • 76 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 2 Open issues
  • 1 Versions
  • 3 % Grown

The README.md

FluidHydrator

This package is based on thecodingmachine/metahydrator, and aims to ease instantiation code (ie when not using dependency injection). In order to do so, it follows fluent design pattern., (*1)

How to use

As you have probably deduced by now, the main class of this package is FluidHydrator. It implements interface Hydrator (refer to mouf/tdbm-hydrator for more details)., (*2)

$hydrator = new FluidHydrator;

Primitive types

Use method field to declare a primitive field. Then, declare its type using int(), bool(), string(), etc., (*3)

$hydrator->field('foo')->int();

To declare an unstructured array field (typically some decoded JSON), use the simpleArray function., (*4)

$hydrator->field('foo')->simpleArray();

As we are these can be chained as following:, (*5)

$hydrator
    ->field('foo')->string()->then()
    ->field('bar')->int() // Note that call o method then() is optional!
    ->field('baz')->float()
;

A type method leads to a state where you may add options to the field, mostly validators., (*6)

$hydrator
    ->field('foo')->string()->required()->maxLength(55)
;

Array

Option array() allows to change the current type from T to array, where current validators are used for validating each entry. These can be used multiple times., (*7)

$hydrator
    // 'foo' must be an array of non-empty arrays containing non-empty strings of length inferior to 55
    ->field('foo')->string()->required()->maxLength(55)->array()->required()->array()
;

Object types

You may also use a non-primitive type (ie a class) for a field(). Method object() then needs you to specify the hydrator used to parse the sub-data. You may pass an existant hydrator using method hydrator(), (*8)

$hydrator
    ->field('garply')->object(Garply::class)->hydrator($garplyHydrator)
;

or even use default hydrator (being TDBMHydrator), if you do not wish to check the data sanity, (*9)

$hydrator
    ->field('garply')->object(Garply::class)->hydrator()
;

If you want to declare the sub-hydrator on the fly, you can: declare it between a begin() and a end(), (*10)

$hydrator
    ->field('garply')->object(Garply::class)
    ->begin()
        ->field('qux')->string()
        ->field('quux')->bool()
    ->end()
;

Note that after end() or hydrator(), you are in the same state as when typing a primitive field. Therefore, you can add validators, and even switch from T to T[] with array()!, (*11)

Sub-Object

In some cases, you will want to access sub-objects from the top-level value being hydrated. This way, the existing value will not be replaced, but directly hydrated, respecting references and allowing partial edition., (*12)

The field type you're looking for here is subobject(); the writing is substantially similar to object(), except for the option array() that you should not used, since it's not (yet) supported., (*13)

$hydrator
    ->field('garply')->subobject(Garply::class)
    ->begin()
        ->field('qux')->string()
        ->field('quux')->bool()
    ->end()->required()
;

The Versions

11/01 2018

1.x-dev

1.9999999.9999999.9999999-dev

A fluid builder for MetaHydrator class

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dorian Savina