PHP code example of metalinspired / zend-mixed-collection-input-filter
1. Go to this page and download the library: Download metalinspired/zend-mixed-collection-input-filter library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
metalinspired / zend-mixed-collection-input-filter example snippets
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' => '[email protected]',
'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());
}