This repository has been archived by the owner on Nov 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add comments explaining purpose/usage of methods Move creation of input filters into own method and make existing getter public
- Loading branch information
1 parent
b1f6860
commit 6acc5d3
Showing
2 changed files
with
175 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# zend-mixed-collection-input-filter | ||
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 | ||
```php | ||
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()); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters