getwarp/laminas-hydrator-bridge

Integration for Laminas Hydrator with other Warp components

3.1.3 2023-03-15 08:22 UTC

This package is auto-updated.

Last update: 2024-11-15 11:42:34 UTC


README

getwarp/laminas-hydrator-bridge

Integration for Laminas Hydrator with other Warp components

GitHubPackagistInstallationUsage

Installation

Via Composer

$ composer require getwarp/laminas-hydrator-bridge

Usage

use Warp\LaminasHydratorBridge\StdClassHydrator;
use Warp\LaminasHydratorBridge\NamingStrategy\AliasNamingStrategy;
use Warp\LaminasHydratorBridge\Strategy\BooleanStrategy;
use Warp\LaminasHydratorBridge\Strategy\ScalarStrategy;
use Warp\LaminasHydratorBridge\Strategy\NullableStrategy;
use Warp\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 warp repository. Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Credits

License

The MIT License (MIT). Please see license file for more information.