mpalourdio/mpa-custom-doctrine-hydrator

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

Installs: 176

Dependents: 1

Stars: 4

Watchers: 1

Open Issues: 1

Language: PHP


README

Build Status Scrutinizer Code Quality Code Coverage SensioLabsInsight PHP 5.5+ MIT Licensed

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.

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.

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.

The hydrator service adds a strategy to every date column in your entity for extraction and hydration.

Requirements

PHP 5.5+ - Only Composer installation supported

Installation

Run the command below to install via Composer

composer require mpalourdio/mpa-custom-doctrine-hydrator

Add "MpaCustomDoctrineHydrator" to your modules list in application.config.php

Configuration

Copy mpacustomdoctrinehydrator.config.global.php.dist in your autoload folder and rename it by removing the .dist extension.

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

see http://www.php.net/manual/fr/datetime.createfromformat.php

Usage (the easy and lazy way)

Create your forms with the provided annotation builder.

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

Or with the factory

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

Then, hydrate your form

$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.

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 :

$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.

$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.

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

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

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

$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 :

$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.