mpalourdio/mpa-custom-doctrine-hydrator

帮助您处理 DoctrineModule & ZF2 中日期的模块:过滤、填充、地区等。

0.5.0 2016-10-31 18:13 UTC

This package is not auto-updated.

Last update: 2024-09-28 14:01:47 UTC


README

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

MpaCustomDoctrineHydrator

帮助您处理 DoctrineORMModule & ZF2 中日期/日期时间/时间的模块:过滤、填充、地区等。扩展并替换 ZF2 日期元素、ZF2 日期时间元素、ZF2 时间元素,以便它们与 doctrine 填充兼容。

提供 DoctrineORMModule AnnotationBuilder 的扩展和一个工厂,以方便使用。为了更好地满足过滤和验证的需求,也重写了 ElementAnnotationsListener

过滤器和元素可以作为独立使用。通过 FormElementManager 提供的元素添加了日期/日期和时间/时间字符串到 DateTime 的自动转换格式。根据 Locale 提供自动过滤和验证,并添加占位符到您的表单元素中。

填充服务为您的实体中的每个日期列添加了提取和填充的策略。

要求

PHP 7.0+ - 只支持 Composer 安装

安装

运行以下命令通过 Composer 安装

composer require mpalourdio/mpa-custom-doctrine-hydrator

application.config.php 中将 "MpaCustomDoctrineHydrator" 添加到您的 模块列表

配置

mpacustomdoctrinehydrator.config.global.php.dist 复制到您的 autoload 文件夹,并删除 .dist 扩展名。

添加符合 php DateTime 的自定义日期/时间格式(如果需要)

参见 https://php.ac.cn/manual/fr/datetime.createfromformat.php

用法(简单快捷的方式)

使用提供的注解构建器创建您的表单。

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

或使用工厂

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

然后,填充您的表单

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

完成!日期/日期和时间/时间列将自动填充/提取、过滤和验证,而无需在实体中提供任何其他内容。您的表单元素将带有占位符渲染。

用法(复杂且解耦的方式)

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

在您的表单类中,当不使用 FormElementManager

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

如果您从 FEM 中拉取表单,只需获取元素作为 'Date''Zend\Form\Element\Date'。这里不需要格式选项,配置将从服务配置中获取。

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

如果需要,您也可以在其他表单元素上使用具有自定义格式的过滤器作为独立使用。为此,使用过滤器 FQCN。

如果您使用过滤器的短名称(DateToDateTime ),配置将从服务配置中获取(即选项数组将被忽略)。

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

或者简单点

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

/!\ 如果您不是通过 FormElementManager 创建字段集/表单,您必须手动注入 SL 以使 Date 元素能够获取配置

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

/!\ 提示:要在没有 FEM 捕获的表单中使用 'DateToDateTime' 过滤器短名称,您必须执行以下操作

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

您也可以使用提供的策略作为独立与您的填充器一起使用。 日期时间处理和时间处理与上面的示例相同,只需进行一些更改,例如 'format' 键的名称。