abacaphiliac/zend-transformer

Extract and Hydrate, plus secret sauce.

0.1.1 2017-03-06 03:33 UTC

This package is auto-updated.

Last update: 2024-12-25 12:26:13 UTC


README

Scrutinizer Code Quality Code Coverage Build Status

abacaphiliac/zend-transformer

An object-to-object ETL package, based on Zend Framework extraction, hydration, and validation abstractions.

Includes a PluginManager for registering transformation specs via application config, and a ZF2 module to wire up all configuration.

Requires >=php55, and supports ZF2 but not ZF3 at this time.

Installation

composer require abacaphiliac/zend-transformer

Usage

Register transformers in your application config:

return [
    'abacaphiliac/zend-transformer' => [
        'transformers' => [
            'SimpleFooBarToFizBuz' => [
                'inputClass' => \AbacaphiliacTest\FooBar::class,
                'keyMap' => [
                    'foo' => 'fiz',
                    'bar' => 'buz',
                ],
                'outputClass' => \AbacaphiliacTest\FizBuz::class,
            ],
        ],
    ],
];

Transform some data!

$transformers = $serviceLocator->get('TransformerManager');
$transformer = $transformers->get('SimpleFooBarToFizBuz');

$input = new \AbacaphiliacTest\FooBar('Foo', 'Bar');
$output = $transformer->transform($input, \AbacaphiliacTest\FizBuz::class);

Complex configuration:

return [
    'abacaphiliac/zend-transformer' => [
        'transformers' => [
            'ComplexFooBarToFizBuz' => [
                'input_validator' => 'MyInputValidatorFromValidatorManager',
                'extractor' => 'MyExractorFromHydratorManager',
                'transformer' => 'MyTransformerFromServiceManager',
                'hydrator' => 'MyHydratorFromHydratorManager',
                'output_validator' => 'MyOutputValidatorFromValidatorManager',
            ],
        ],
    ],
    'service_manager' => [
        'invokables' => [
            'MyTransformerFromServiceManager' => function (array $data) {
                // Don't do this in production, as the config cannot be cached.
                return [];
            },
        ],
    ],
    'validators' => [
        'invokables' => [
            'MyInputValidatorFromValidatorManager' => \Zend\Validator\ValidatorChain::class,
            'MyOutputValidatorFromValidatorManager' => \Zend\Validator\ValidatorChain::class,
        ],
    ],
    'hydrators' => [
        'invokables' => [
            'MyExractorFromHydratorManager' => \Zend\Hydrator\ClassMethods::class,
            'MyHydratorFromHydratorManager' => \Zend\Hydrator\ClassMethods::class,
        ],
    ],
];

Contributing

composer update && vendor/bin/phpunit

This library attempts to comply with PSR-1, PSR-2, and PSR-4. If you notice compliance oversights, please send a patch via pull request.