Giter Club home page Giter Club logo

mpacustomdoctrinehydrator's Introduction

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

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.

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 7.0+ - 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.

mpacustomdoctrinehydrator's People

Contributors

mpalourdio avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

stephane303

mpacustomdoctrinehydrator's Issues

Class 'Locale' not found

Hello,

just installed your module through composer but I get this error:

Fatal error: Class 'Locale' not found in C:\xampp\htdocs\directory\vendor\mpalourdio\mpa-custom-doctrine-hydrator\src\Factory\DateElementFactory.php on line 31

Any ideas ?
Thanks
Sergiu C.

Notice: Undefined index: en_US_POSIX

Hello,

Any ideas about this error?

Notice: Undefined index: en_US_POSIX in /vendor/mpalourdio/mpa-custom-doctrine-hydrator/src/Factory/TimeElementFactory.php on line 31

Thanks
Sergiu

invalid factory error

i get this error when trying to display view

While attempting to create hydrator(alias: hydrator) an invalid factory was registered for this instance type.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.