2017 © Pedro Peláez
 

library magento-zend-hydrator-adapter

A bridge to use the Zend Hydrator implementation instead of the Magento2 implementation.

image

widefocus/magento-zend-hydrator-adapter

A bridge to use the Zend Hydrator implementation instead of the Magento2 implementation.

  • Monday, May 22, 2017
  • by sjokki
  • Repository
  • 1 Watchers
  • 0 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

WideFocus Magento Zend Hydrator Adapter

An adapter to use the Zend Hydrator implementation instead of the Magento2 implementation., (*1)

Installation

Use composer to install the package., (*2)

$ composer require widefocus/magento-zend-hydrator-adapter

Usage

The hydrator implementation in Magento 2.1 works alright with models that do not contain any objects. But when a model uses for example DateTimeInterface exceptions occur., (*3)

This package solves that by making it possible to use the Zend Framework Hydrator., (*4)

Example class to be hydrated:, (*5)

<?php
namespace Foo\Model;

use Foo\Api\Data\FooModelInterface;

class FooModel implements FooModelInterface
{
    private $date;

    public function setDate(DateTimeInterface $date)
    {
        $this->date = $date;
    }

    public function getDate(): DateTimeInterface
    {
        return $this->date;
    }
}

DI configuration (etc/di.xml), (*6)

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <virtualType name="FooHydratorDateTimeStrategy"
                 type="Zend\Stdlib\Hydrator\Strategy\DateTimeFormatterStrategy">
        <arguments>
            <argument name="format" xsi:type="string">Y-m-d H:i:s</argument>
        </arguments>
    </virtualType>

    <virtualType name="FooModelHydrator"
                 type="WideFocus\Magento\ZendHydratorAdapter\ReflectionHydratorAdapter">
        <arguments>
            <argument name="strategies" xsi:type="array">
                <item name="date"
                      xsi:type="object">FooHydratorDateTimeStrategy</item>
            </argument>
        </arguments>
    </virtualType>

    <type name="Magento\Framework\EntityManager\HydratorPool">
        <arguments>
            <argument name="hydrators" xsi:type="array">
                <item name="Foo\Api\Data\FooModelInterface"
                      xsi:type="string">FooModelHydrator</item>
            </argument>
        </arguments>
    </type>

</config>

The hydrator will now be used by magento, for example when using the Magento Entity Manager., (*7)

The following shows how to get the hydrator from the hydrator pool., (*8)

<?php

namespace Foo\Controller;

use Magento\Framework\EntityManager\HydratorPool;
use Foo\Api\Data\FooModelInterface;
use Foo\Model\FooModel;

class Save
{
    private $hydratorPool;

    public function __construct(HydratorPool $hydratorPool)
    {
        $this->hydratorPool = $hydratorPool;
    }

    public function execute()
    {
        $model = new FooModel();
        $this->hydratorPool
            ->getHydrator(FooModelInterface::class)
            ->hydrate($model, ['date' => '2025-12-31 23:59:59']);
    }
}

The Versions

22/05 2017

dev-master

9999999-dev

A bridge to use the Zend Hydrator implementation instead of the Magento2 implementation.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ashoka de Wit

13/05 2017

1.1.0

1.1.0.0

A bridge to use the Zend Hydrator implementation instead of the Magento2 implementation.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ashoka de Wit

13/05 2017

1.0.0

1.0.0.0

A bridge to use the Zend Hydrator implementation instead of the Magento2 implementation.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ashoka de Wit

13/05 2017

1.0.1

1.0.1.0

A bridge to use the Zend Hydrator implementation instead of the Magento2 implementation.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ashoka de Wit