定制 metalinspired/laminas-mixed-collection-input-filter 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

metalinspired/laminas-mixed-collection-input-filter

最新稳定版本:1.0.0

Composer 安装命令:

composer require metalinspired/laminas-mixed-collection-input-filter

包简介

Extended collection input filter that allows using different input filter per collection item

README 文档

README

This collection input filter allows you to define multiple input filters for collection items. Practical usage would be if you have an array (collection) of items which can have different structure and, thus, would require different input filter to filter/validate each item in collection.

Example

class ExampleInputFilter extends InputFilter
{
    public function init()
    {
        $this->add((new MixedCollectionInputFilter())
            ->setNameKey('type')
            ->setInputFilters([
                'picture' => $this->picture(),
                'link' => $this->link(),
                'comment' => $this->comment(),
            ])
            ->setFactory($this->getFactory()), 'content');
    }

    private function picture() : InputFilter
    {
        return (new InputFilter())
            ->add(['name' => 'type'])
            ->add(['name' => 'alt'])
            ->add(['name' => 'src']);
    }

    private function link() : InputFilter
    {
        return (new InputFilter())
            ->add(['name' => 'type'])
            ->add(['name' => 'title'])
            ->add(['name' => 'href'])
            ->add(['name' => 'target']);
    }

    private function comment() : InputFilter
    {
        return (new InputFilter())
            ->add(['name' => 'type'])
            ->add(['name' => 'author'])
            ->add(['name' => 'email'])
            ->add(['name' => 'title'])
            ->add(['name' => 'text'])
            ->add([
                'name' => 'notifications',
                'filters' => [
                    ['name' => \Laminas\Filter\Boolean::class],
                ],
                'validators' => [
                    [
                        'name' => \Laminas\Validator\InArray::class,
                        'options' => [
                            'haystack' => ['0', '1'],
                        ],
                    ],
                ],
            ]);
    }
}

$inputFilter = new ExampleInputFilter();
$inputFilter->init();

$data = [
    'content' => [
        [
            'type' => 'picture',
            'alt' => 'Some picture',
            'src' => 'url',
            'foo' => 'This element will be filtered out',
        ],
        [
            'type' => 'link',
            'href' => 'url',
            'title' => 'Link to something',
            'target' => '_blank',
        ],
        [
            'type' => 'comment',
            'author' => 'unknown',
            'email' => 'dummy@email.com',
            'title' => 'Example',
            'text' => 'Got nothing more to say',
            'notifications' => '1',
        ],
        [
            'type' => 'picture',
            'alt' => 'Another picture',
            'src' => 'another url',
        ],
    ],
];

$inputFilter->setData($data);

if ($inputFilter->isValid()) {
    var_dump($inputFilter->getValues());
} else {
    var_dump($inputFilter->getMessages());
}

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2023-11-23