Skip to content
This repository has been archived by the owner on Nov 23, 2023. It is now read-only.

Commit

Permalink
Add README.md
Browse files Browse the repository at this point in the history
Add comments explaining purpose/usage of methods
Move creation of input filters into own method and make existing getter public
  • Loading branch information
metalinspired committed Mar 24, 2019
1 parent b1f6860 commit 6acc5d3
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 29 deletions.
103 changes: 103 additions & 0 deletions README.md
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());
}
```
101 changes: 72 additions & 29 deletions src/MixedCollectionInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Zend\InputFilter\BaseInputFilter;
use Zend\InputFilter\Exception;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterInterface;
use Zend\Validator\NotEmpty;

class MixedCollectionInputFilter extends InputFilter
Expand Down Expand Up @@ -86,38 +87,69 @@ public function getNameKey() : string
}

/**
* Set the input filter used for specific collection item type
*
* @param string $name
* @param array|\Traversable|InputFilterInterface $inputFilter
* @return MixedCollectionInputFilter
* @throws Exception\RuntimeException
*/
public function setInputFilter(string $name, $inputFilter) : MixedCollectionInputFilter
{
if (\is_array($inputFilter) || $inputFilter instanceof \Traversable) {
$inputFilter = $this->getFactory()->createInputFilter($inputFilter);
}

if (! $inputFilter instanceof BaseInputFilter) {
throw new Exception\RuntimeException(sprintf(
'%s expects an instance of %s; received "%s"',
__METHOD__,
BaseInputFilter::class,
(\is_object($inputFilter) ? \get_class($inputFilter) : \gettype($inputFilter))
));
}

$this->inputFilters[$name] = $inputFilter;

return $this;
}

/**
* Get the input filter used for specific collection item type
*
* @param string $name
* @return BaseInputFilter|null
*/
public function getInputFilter(string $name)
{
return $this->inputFilters[$name] ?? null;
}

/**
* Set input filters used for filtering collection items
*
* @param $inputFilters
* @return MixedCollectionInputFilter
* @throws Exception\RuntimeException
*/
public function setInputFilters($inputFilters) : MixedCollectionInputFilter
{
if (\is_array($inputFilters) || $inputFilters instanceof \Traversable) {
foreach ($inputFilters as $name => $inputFilter) {
if (! \is_string($name)) {
throw new Exception\RuntimeException('Input filter key is not string');
}

if (\is_array($inputFilter) || $inputFilter instanceof \Traversable) {
$inputFilter = $this->getFactory()->createInputFilter($inputFilter);
}

if (! $inputFilter instanceof BaseInputFilter) {
throw new Exception\RuntimeException(sprintf(
'%s expects an instance of %s; received "%s"',
__METHOD__,
BaseInputFilter::class,
(\is_object($inputFilter) ? \get_class($inputFilter) : \gettype($inputFilter))
));
throw new Exception\RuntimeException('Input filter key is not a string');
}

$this->inputFilters[$name] = $inputFilter;
$this->setInputFilter($name, $inputFilter);
}
}

return $this;
}

/**
* Get input filters used for filtering collection items
*
* @return BaseInputFilter[]
*/
public function getInputFilters() : array
Expand All @@ -126,21 +158,12 @@ public function getInputFilters() : array
}

/**
* Get the input filter used when looping the data
* Set behavior for when name key is missing in collection item
*
* If set to true and collection item is missing a key with name
* used for identifying input filter, validation will fail and
* message of missing key will be added to messages array
*
* @param string $name
* @return BaseInputFilter|null
*/
protected function getInputFilter(string $name)
{
if (! isset($this->inputFilters[$name])) {
return null;
}

return $this->inputFilters[$name];
}

/**
* @param bool $invalid
* @return MixedCollectionInputFilter
*/
Expand All @@ -152,6 +175,12 @@ public function setNameKeyMissingInvalid(bool $invalid) : MixedCollectionInputFi
}

/**
* Get behavior for when name key is missing in collection item
*
* If set to true and collection item is missing a key with name
* used for identifying input filter, validation will fail and
* missing name key message will be added to messages array
*
* @return bool
*/
public function getNameKeyMissingInvalid() : bool
Expand All @@ -160,6 +189,13 @@ public function getNameKeyMissingInvalid() : bool
}

/**
* Set behavior for when filter is missing for collection item
*
* If set to true and value of name key in collection item
* can't be mapped to input filter in input filters array,
* validation will fail and missing filter message will be
* added to messages array
*
* @param bool $invalid
* @return MixedCollectionInputFilter
*/
Expand All @@ -171,6 +207,13 @@ public function setFilterMissingInvalid(bool $invalid) : MixedCollectionInputFil
}

/**
* Get behavior for when filter is missing for collection item
*
* If set to true and value of name key in collection item
* can't be mapped to input filter in input filters array,
* validation will fail and missing filter message will be
* added to messages array
*
* @return bool
*/
public function getFilterMissingInvalid() : bool
Expand Down

0 comments on commit 6acc5d3

Please sign in to comment.