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#5479 from saltoledo/fo…
Browse files Browse the repository at this point in the history
…rm-input-filter-fix

Add element input filters before form input filters
  • Loading branch information
weierophinney committed Mar 3, 2014
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/BaseInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
* @todo How should we deal with required input when data is missing?
* should a message be returned? if so, what message?
*/
class BaseInputFilter implements InputFilterInterface, UnknownInputsCapableInterface, InitializableInterface
class BaseInputFilter implements
InputFilterInterface,
UnknownInputsCapableInterface,
InitializableInterface,
ReplaceableInputInterface
{
protected $data;
protected $inputs = array();
Expand Down Expand Up @@ -84,6 +88,38 @@ public function add($input, $name = null)
return $this;
}

/**
* Replace a named input
*
* @param InputInterface|InputFilterInterface $input
* @param string $name Name of the input to replace
* @throws Exception\InvalidArgumentException
* @return self
*/
public function replace($input, $name)
{
if (!$input instanceof InputInterface && !$input instanceof InputFilterInterface) {
throw new Exception\InvalidArgumentException(sprintf(
'%s expects an instance of %s or %s as its first argument; received "%s"',
__METHOD__,
'Zend\InputFilter\InputInterface',
'Zend\InputFilter\InputFilterInterface',
(is_object($input) ? get_class($input) : gettype($input))
));
}

if (!array_key_exists($name, $this->inputs)) {
throw new Exception\InvalidArgumentException(sprintf(
'%s: no input found matching "%s"',
__METHOD__,
$name
));
}

$this->inputs[$name] = $input;
return $this;
}

/**
* Retrieve a named input
*
Expand Down
15 changes: 15 additions & 0 deletions src/ReplaceableInputInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?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;

interface ReplaceableInputInterface
{
public function replace($input, $name);
}

0 comments on commit 4f8f645

Please sign in to comment.