Created
May 22, 2012 15:16
-
-
Save ezimuel/2769699 to your computer and use it in GitHub Desktop.
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
/** | |
* Set data to use when validating and filtering | |
* | |
* @param array|Traversable $data | |
* @return InputFilterInterface | |
*/ | |
public function setData($data) | |
{ | |
if (!is_array($data) && !$data instanceof Traversable) { | |
throw new Exception\InvalidArgumentException(sprintf( | |
'%s expects an array or Traversable argument; received %s', | |
__METHOD__, | |
(is_object($data) ? get_class($data) : gettype($data)) | |
)); | |
} | |
if (is_object($data) && !$data instanceof ArrayAccess) { | |
$data = ArrayUtils::iteratorToArray($data); | |
} | |
$this->data = $data; | |
foreach (array_keys($this->inputs) as $name) { | |
$input = $this->inputs[$name]; | |
if (isset($data[$name])) { | |
if ($input instanceof InputFilterInterface) { | |
$input->setData($data[$name]); | |
} elseif ($input instanceof InputInterface) { | |
$input->setValue($data[$name]); | |
} | |
} else { | |
if ($input instanceof InputFilterInterface) { | |
$input->setData(array()); | |
} | |
} | |
} | |
return $this; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment