/**
* Method to bind an associative array or object to the Table instance.This
* method only binds properties that are publicly accessible and optionally
* takes an array of properties to ignore when binding.
*
* @param array|object $src An associative array or object to bind to the Table instance.
* @param array|string $ignore An optional array or space separated list of properties to ignore while binding.
*
* @return boolean True on success.
*
* @since 1.7.0
* @throws \InvalidArgumentException
*/
public function bind($src, $ignore = array())
{
// Check if the source value is an array or object
if (!\is_object($src) && !\is_array($src)) {
throw new \InvalidArgumentException(sprintf('Could not bind the data source in %1$s::bind(), the source must be an array or object but a "%2$s" was given.', \get_class($this), \gettype($src)));
}
// If the ignore value is a string, explode it over spaces.
if (!\is_array($ignore)) {
$ignore = explode(' ', $ignore);
}
$event = AbstractEvent::create('onTableBeforeBind', ['subject' => $this, 'src' => $src, 'ignore' => $ignore]);
$this->getDispatcher()->dispatch('onTableBeforeBind', $event);
// If the source value is an object, get its accessible properties.
if (\is_object($src)) {
$src = get_object_vars($src);
}
// JSON encode any fields required
if (!empty($this->_jsonEncode)) {
foreach ($this->_jsonEncode as $field) {
if (isset($src[$field]) && \is_array($src[$field])) {
$src[$field] = json_encode($src[$field]);
}
}
}
// Bind the source value, excluding the ignored fields.
foreach ($this->getProperties() as $k => $v) {
// Only process fields not in the ignore array.
if (!\in_array($k, $ignore)) {
if (isset($src[$k])) {
$this->{$k} = $src[$k];
}
}
}
$event = AbstractEvent::create('onTableAfterBind', ['subject' => $this, 'src' => $src, 'ignore' => $ignore]);
$this->getDispatcher()->dispatch('onTableAfterBind', $event);
return true;
}