Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Latest commit

 

History

History
executable file
·
96 lines (73 loc) · 3.44 KB

README.md

File metadata and controls

executable file
·
96 lines (73 loc) · 3.44 KB

Laminas Hydrator Bridge by spaceonfire

Latest Version on Packagist Software License Total Downloads Code Coverage

Provides integration for Laminas Hydrator with some spaceonfire libraries.

Install

Via Composer

$ composer require spaceonfire/laminas-hydrator-bridge

Usage

use spaceonfire\LaminasHydratorBridge\StdClassHydrator;
use spaceonfire\LaminasHydratorBridge\NamingStrategy\AliasNamingStrategy;
use spaceonfire\LaminasHydratorBridge\Strategy\BooleanStrategy;
use spaceonfire\LaminasHydratorBridge\Strategy\ScalarStrategy;
use spaceonfire\LaminasHydratorBridge\Strategy\NullableStrategy;
use spaceonfire\Type\BuiltinType;

$hydrator = new StdClassHydrator();

$hydrator->setNamingStrategy(new AliasNamingStrategy([
    'firstName' => ['first_name', 'firstname'],
    'lastName' => ['last_name', 'lastname'],
    'rulesAccepted' => ['rules_accepted'],
]));

$hydrator->addStrategy('age', new NullableStrategy(new ScalarStrategy(BuiltinType::INT)));
$hydrator->addStrategy('rulesAccepted', new BooleanStrategy(['Y', 'y', 1], 'N', false));

$john = $hydrator->hydrate([
    'first_name' => 'John',
    'last_name' => 'Doe',
    'rules_accepted' => 'y',
    'age' => '25',
], new stdClass());

// $john->firstName === 'John';
// $john->lastName === 'Doe';
// $john->rulesAccepted === true;
// $john->age === 25;

$jane = $hydrator->hydrate([
    'firstname' => 'Jane',
    'lastname' => 'Doe',
    'rules_accepted' => '',
    'age' => null,
], new stdClass());

// $jane->firstName === 'John';
// $jane->lastName === 'Doe';
// $jane->rulesAccepted === false;
// $jane->age === null;

Change log

Please see CHANGELOG for more information on what has changed recently.

Contributing

Report issues and send Pull Requests in the main spaceonfire repository. Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Credits

License

The MIT License (MIT). Please see License File for more information.