/**
* Merges `$data` into `$document`.
*
* ### Options:
*
* * fieldList: A whitelist of fields to be assigned to the entity. If not present
* the accessible fields list in the entity will be used.
*
* @param \Cake\Datasource\EntityInterface $entity the entity that will get the
* data merged in
* @param array $data key value list of fields to be merged into the entity
* @param array $options List of options.
* @return \Cake\Datasource\EntityInterface
*/
public function merge(EntityInterface $entity, array $data, array $options = [])
{
list($data, $options) = $this->_prepareDataAndOptions($data, $options);
$errors = $this->_validate($data, $options, $entity->isNew());
$entity->errors($errors);
foreach (array_keys($errors) as $badKey) {
unset($data[$badKey]);
}
foreach ($this->type->embedded() as $embed) {
$property = $embed->property();
if (in_array($embed->alias(), $options['associated']) && isset($data[$property])) {
$data[$property] = $this->mergeNested($embed, $entity->{$property}, $data[$property]);
}
}
if (!isset($options['fieldList'])) {
$entity->set($data);
return $entity;
}
foreach ((array) $options['fieldList'] as $field) {
if (array_key_exists($field, $data)) {
$entity->set($field, $data[$field]);
}
}
return $entity;
}