metalinspired / zend-mixed-collection-input-filter
1.0.3
2023-11-23 13:04 UTC
Requires
- php: ^7.0
- zendframework/zend-inputfilter: ^2.9.0
- zendframework/zend-validator: ^2.9.0
This package is auto-updated.
Last update: 2023-11-23 13:16:16 UTC
README
仓库弃用于2023-11-23
此集合输入过滤器允许您为集合项定义多个输入过滤器。实际应用场景可能是在您有一个包含不同结构项的数组(集合)时,每个项可能需要不同的输入过滤器来过滤/验证集合中的每个项。
示例
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' => \Zend\Filter\Boolean::class], ], 'validators' => [ [ 'name' => \Zend\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()); }