/** * Merge an embedded document. * * @param \Cake\ElasticSearch\Association\Embedded $embed The embed definition. * @param \Cake\ElasticSearch\Document|array $existing The existing entity or entities. * @param array $data The data to marshal * @return array|Cake\ElasticSearch\Document Either a document or an array of documents. */ protected function mergeNested(Embedded $embed, $existing, array $data) { $class = $embed->entityClass(); if ($embed->type() === Embedded::ONE_TO_ONE) { if (!$existing instanceof EntityInterface) { $existing = new $class(); } $existing->set($data); return $existing; } if ($embed->type() === Embedded::ONE_TO_MANY) { foreach ($existing as $i => $row) { if (isset($data[$i])) { $row->set($data[$i]); } unset($data[$i]); } foreach ($data as $row) { if (is_array($row)) { $new = new $class(); $new->set($row); $existing[] = $new; } } return $existing; } }