Skip to content

Instantly share code, notes, and snippets.

@ezimuel
Created May 22, 2012 15:16
Show Gist options
  • Save ezimuel/2769699 to your computer and use it in GitHub Desktop.
Save ezimuel/2769699 to your computer and use it in GitHub Desktop.
/**
* 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