2017 © Pedro Peláez
 

library mpa-custom-doctrine-hydrator

Module that helps you deal with dates for DoctrineModule & ZF2 : filtering, hydration, Locale etc.

image

mpalourdio/mpa-custom-doctrine-hydrator

Module that helps you deal with dates for DoctrineModule & ZF2 : filtering, hydration, Locale etc.

  • Saturday, February 4, 2017
  • by mpalourdio
  • Repository
  • 1 Watchers
  • 4 Stars
  • 210 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 12 Versions
  • 0 % Grown

The README.md

Build Status Scrutinizer Code Quality Code Coverage SensioLabsInsight PHP 7.0+ MIT Licensed, (*1)

MpaCustomDoctrineHydrator

Module that helps you deal with date/datetime/time for DoctrineORMModule & ZF2 : filtering, hydration, Locale etc. Extends and replace the ZF2 Date Element, ZF2 DateTime Element, ZF2 Time Element to make them compliant 'out-of-the-box' with doctrine hydration., (*2)

Provides an extension of the DoctrineORMModule AnnotationBuilder and a factory for more ease. The ElementAnnotationsListener is overridden too in order to better suit needs regarding filtering and validation., (*3)

The filters and the elements can be used as standalone. Using the provided elements via the FormElementManager adds automatic conversion formats for date/date and time/time strings to DateTime. Automatic filtering and validation are provided regarding the date format (Y-m-d, Y-m-d H:i:s, H:i:s, etc.) that depends of the Locale. A placeholder is added to your form element too when rendered., (*4)

The hydrator service adds a strategy to every date column in your entity for extraction and hydration., (*5)

Requirements

PHP 7.0+ - Only Composer installation supported, (*6)

Installation

Run the command below to install via Composer, (*7)

composer require mpalourdio/mpa-custom-doctrine-hydrator

Add "MpaCustomDoctrineHydrator" to your modules list in application.config.php, (*8)

Configuration

Copy mpacustomdoctrinehydrator.config.global.php.dist in your autoload folder and rename it by removing the .dist extension., (*9)

Add your own date / time formats (if needed) that are compliant with php DateTime, (*10)

see http://www.php.net/manual/fr/datetime.createfromformat.php, (*11)

Usage (the easy and lazy way)

Create your forms with the provided annotation builder., (*12)

$builder       = new \MpaCustomDoctrineHydrator\Form\Annotation\AnnotationBuilder($this->entityManager, $this->formElementManager);
$form = $builder->createForm('Application\Entity\Myentity');

Or with the factory, (*13)

$form = $this->sm->get('annotationbuilder')->createForm('Application\Entity\Myentity');

Then, hydrate your form, (*14)

$hydrator = $this->sm->get('hydrator')->setEntity('Application\Entity\Myentity');
$form->setHydrator($hydrator);

You're done! Date/Date & Time/ Time colums will be hydrated/extracted, filtered and validated automatically, without providing anything else in your entities. Your form elements will be rendered with a placeholder., (*15)

Usage (the hard and decoupled way)

$hydrator = $this->sm->get('hydrator')->setEntity('Application\Entity\Myentity');
$form->setHydrator($hydrator);

In your forms classes, when not using the FormElementManager :, (*16)


$this->add( [ 'name' => 'mydate', 'type' => 'MpaCustomDoctrineHydrator\Form\Element\Date', 'attributes' => [ 'id' => 'mydate', ], 'options' => [ 'label' => 'My date', 'format' => 'd/m/Y' // format needed ], ] );

If you pull your forms from the FEM, just grab the element as a 'Date' or 'Zend\Form\Element\Date'. The format option is not needed here, config will be pulled from service config., (*17)

$this->add(
            [
                'name'       => 'mydate',
                'type'       => 'Date',
                'attributes' => [
                    'id'    => 'mydate',
                ],
                'options'    => [
                    'label'  => 'My date',
                ],
            ]
        );

You can too use the filter as standalone on other form elements with custom formats, if needed. For this, use the filter FQCN., (*18)

If you use the filter shortname (DateToDateTime), the config will be pulled from the service config (ie. The options array will be ignored)., (*19)

public function getInputFilterSpecification()
{
        $filters = [
            'otherdate' => [
                'filters' => [
                    [
                        'name' => 'MpaCustomDoctrineHydrator\Filter\DateToDateTime',
                        'options' => [
                            'format' => 'd/m/Y' ('date_format' key is also accepted)
                        ]
                    ],
                ],
            ],
        ];
        return $filters;
}

or simply, (*20)

public function getInputFilterSpecification()
{
        $filters = [
            'otherdate' => [
                'filters' => [
                    [
                        'name' => 'DateToDateTime',
                    ], // no options needed here, would be ignored anyway
                ],
            ],
        ];
        return $filters;
}

/!\ If you don't create your fieldsets/forms via the FormElementManager, you must manually inject the SL so the Date element can fetch the configuration, (*21)

$this->getFormFactory()->getFormElementManager()->setServiceLocator($this->sm);

/!\ Tip : To use the 'DateToDateTime' filter short name in a form grabbed without the FEM, you must do the following :, (*22)

$plugins = $this->sm ->get('FilterManager');
$chain   = new FilterChain;
$chain->setPluginManager($plugins);
$myForm->getFormFactory()->getInputFilterFactory()->setDefaultFilterChain($chain);

You can use the provided strategy as standalone with your hydrators too. Date Time and Time handling work the same way as the example above, with only few changes, like the 'format' keys names., (*23)

The Versions

27/07 2014

0.4.0

0.4.0.0 https://github.com/mpalourdio/MpaCustomDoctrineHydrator

Module that helps you deal with dates for DoctrineModule & ZF2 : filtering, hydration, Locale etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm zf2 doctrine module

21/05 2014

0.3.4

0.3.4.0 https://github.com/mpalourdio/MpaCustomDoctrineHydrator

Module that helps you deal with dates for DoctrineModule & ZF2 : filtering, hydration, Locale etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm zf2 doctrine module

20/05 2014

0.3.3

0.3.3.0 https://github.com/mpalourdio/MpaCustomDoctrineHydrator

Module that helps you deal with dates for DoctrineModule & ZF2 : filtering, hydration, Locale etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm zf2 doctrine module

18/05 2014

0.3.2

0.3.2.0 https://github.com/mpalourdio/MpaCustomDoctrineHydrator

Module that helps you deal with dates for DoctrineModule & ZF2 : filtering, hydration, Locale etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm zf2 doctrine module

12/05 2014

0.3.1

0.3.1.0 https://github.com/mpalourdio/MpaCustomDoctrineHydrator

Module that helps you deal with dates for DoctrineModule & ZF2 : filtering, hydration, Locale etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm zf2 doctrine module

09/05 2014

0.3.0

0.3.0.0 https://github.com/mpalourdio/MpaCustomDoctrineHydrator

Module that helps you deal with dates for DoctrineModule & ZF2 : filtering, hydration, Locale etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm zf2 doctrine module

08/05 2014

0.2.1

0.2.1.0 https://github.com/mpalourdio/MpaCustomDoctrineHydrator

Module that helps you deal with dates for DoctrineModule & ZF2 : filtering, hydration, Locale etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm zf2 doctrine module

04/05 2014

0.2.0

0.2.0.0 https://github.com/mpalourdio/MpaCustomDoctrineHydrator

Module that helps you deal with dates for DoctrineModule & ZF2 : filtering, hydration, Locale etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm zf2 doctrine module