Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge pull request zendframework/zendframework#3879 from davidwindell…
Browse files Browse the repository at this point in the history
…/feature-collection-input-filter

Feature CollectionInputFilter
  • Loading branch information
Show file tree
Hide file tree
Showing 6 changed files with 656 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/BaseInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,22 @@ public function isValid()
));
}

$inputs = $this->validationGroup ?: array_keys($this->inputs);
return $this->validateInputs($inputs);
}

/**
* Validate a set of inputs against the current data
*
* @param array $inputs
* @return boolean
*/
protected function validateInputs(array $inputs)
{
$this->validInputs = array();
$this->invalidInputs = array();
$valid = true;

$inputs = $this->validationGroup ?: array_keys($this->inputs);
foreach ($inputs as $name) {
$input = $this->inputs[$name];
if (!array_key_exists($name, $this->data)
Expand Down Expand Up @@ -495,4 +506,14 @@ public function getUnknown()

return $unknownInputs;
}

/**
* Get an array of all inputs
*
* @return array
*/
public function getInputs()
{
return $this->inputs;
}
}
236 changes: 236 additions & 0 deletions src/CollectionInputFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\InputFilter;

use Traversable;

class CollectionInputFilter extends InputFilter
{
/*
* @var array
*/
protected $collectionData;

/*
* @var array
*/
protected $collectionValidInputs;

/*
* @var array
*/
protected $collectionInvalidInputs;

/*
* @var int
*/
protected $count = null;

/*
* @var array
*/
protected $collectionValues = array();

/*
* @var array
*/
protected $collectionRawValues = array();

/**
* @var BaseInputFilter
*/
protected $inputFilter;

/**
* Set the input filter to use when looping the data
*
* @param BaseInputFilter|array|Traversable $inputFilter
* @return CollectionInputFilter
*/
public function setInputFilter($inputFilter)
{
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__,
'Zend\InputFilter\BaseInputFilter',
(is_object($inputFilter) ? get_class($inputFilter) : gettype($inputFilter))
));
}

$this->inputFilter = $inputFilter;
$this->inputs = $inputFilter->getInputs();
return $this;
}

/**
* Get the input filter used when looping the data
*
* @return BaseInputFilter
*/
public function getInputFilter()
{
if (null === $this->inputFilter) {
$this->setInputFilter(new InputFilter());
}
return $this->inputFilter;
}

/**
* Set the count of data to validate
*
* @param int $count
* @return CollectionInputFilter
*/
public function setCount($count)
{
$this->count = $count > 0 ? $count : 0;
return $this;
}

/**
* Get the count of data to validate, use the count of data by default
*
* @return int
*/
public function getCount()
{
if (null === $this->count) {
$this->count = count($this->collectionData);
}
return $this->count;
}

/**
* {@inheritdoc}
*/
public function setData($data)
{
$this->collectionData = array_values($data);
}

/**
* {@inheritdoc}
*/
public function isValid()
{
$valid = true;

if ($this->getCount() < 1) {
return $valid;
}

$inputCollection = array_fill(0, $this->getCount() , $this->validationGroup ?: array_keys($this->inputs));

foreach ($inputCollection as $key => $inputs) {
$this->data = array();
if (isset($this->collectionData[$key])) {
$this->data = $this->collectionData[$key];
}
$this->populate();

if ($this->validateInputs($inputs)) {
$this->collectionValidInputs[$key] = $this->validInputs;
} else {
$this->collectionInvalidInputs[$key] = $this->invalidInputs;
$valid = false;
}

$values = array();
$rawValues = array();
foreach ($inputs as $name) {
$input = $this->inputs[$name];

if ($input instanceof InputFilterInterface) {
$values[$name] = $input->getValues();
$rawValues[$name] = $input->getRawValues();
continue;
}
$values[$name] = $input->getValue($this->data);
$rawValues[$name] = $input->getRawValue();
}
$this->collectionValues[$key] = $values;
$this->collectionRawValues[$key] = $rawValues;
}

return $valid;
}

/**
* {@inheritdoc}
*/
public function setValidationGroup($name)
{
if ($name === self::VALIDATE_ALL) {
$this->validationGroup = null;
return $this;
}

if (is_array($name)) {
// Best effort check if the validation group was set by a form for BC
if (count($name) == count($this->collectionData) && is_array(reset($name))) {
return parent::setValidationGroup(reset($name));
}
return parent::setValidationGroup($name);
}

return parent::setValidationGroup(func_get_args());
}

/**
* {@inheritdoc}
*/
public function getInvalidInput()
{
return (is_array($this->collectionInvalidInputs) ? $this->collectionInvalidInputs : array());
}

/**
* {@inheritdoc}
*/
public function getValidInput()
{
return (is_array($this->collectionValidInputs) ? $this->collectionValidInputs : array());
}

/**
* {@inheritdoc}
*/
public function getValues()
{
return $this->collectionValues;
}

/**
* {@inheritdoc}
*/
public function getRawValues()
{
return $this->collectionRawValues;
}

/**
* {@inheritdoc}
*/
public function getMessages()
{
$messages = array();
foreach ($this->getInvalidInput() as $key => $inputs) {
foreach ($inputs as $name => $input) {
$messages[$key][$name] = $input->getMessages();
}
}
return $messages;
}
}
10 changes: 10 additions & 0 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,16 @@ public function createInputFilter($inputFilterSpecification)
'Zend\InputFilter\InputFilterInterface', $class));
}

if ($inputFilter instanceof CollectionInputFilter) {
if (isset($inputFilterSpecification['input_filter'])) {
$inputFilter->setInputFilter($inputFilterSpecification['input_filter']);
}
if (isset($inputFilterSpecification['count'])) {
$inputFilter->setCount($inputFilterSpecification['count']);
}
return $inputFilter;
}

foreach ($inputFilterSpecification as $key => $value) {

if (($value instanceof InputInterface)
Expand Down
16 changes: 16 additions & 0 deletions test/BaseInputFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,22 @@ public function testValidateUseExplodeAndInstanceOf()

$filter->setData($data);
$this->assertTrue($filter->isValid());
}

public function testGetInputs()
{
$filter = new InputFilter();

$foo = new Input('foo');
$bar = new Input('bar');

$filter->add($foo);
$filter->add($bar);

$filters = $filter->getInputs();

$this->assertCount(2, $filters);
$this->assertEquals('foo', $filters['foo']->getName());
$this->assertEquals('bar', $filters['bar']->getName());
}
}
Loading

0 comments on commit 828b3d1

Please sign in to comment.