Prepares and formats a has-many embed for proper insertion into the database.

@param   EmbeddedPropMetadata        $embeddedPropMeta
@param   EmbedCollection|array|null  $embeds
@return  mixed
423520
public function getEmbedManyDbValue(EmbeddedPropMetadata $embeddedPropMeta, $embeds = null)
    {
        if (null === $embeds) {
            return;
        }
        if (!is_array($embeds) && !$embeds instanceof \Traversable) {
            throw new \InvalidArgumentException('Unable to traverse the embed collection');
        }

        $created = [];
        foreach ($embeds as $embed) {
            $created[] = $this->createEmbed($embeddedPropMeta, $embed);
        }
        return empty($created) ? null : $created;
    }
325817
private function buildNestedParams($nestedParams, $origParam, $isProto = false)
    {
        $paramsArray = [];

        foreach ($nestedParams as $param) {
            $nestedParam = explode(' ', trim($param), 3);

            // START proto nested arg missing description workaround
            if (count($nestedParam) < 3 && !$isProto) {
                throw new \Exception('nested param is in an invalid format: '. $param);
            }
            // END proto nested arg missing description workaround

            list($type, $name, $description) = $nestedParam;
            $name = substr($name, 1);
            $description = preg_replace('/\s+/', ' ', $description);

            $types = new Collection(
                array($type),
                $origParam->getDocBlock() ? $origParam->getDocBlock()->getContext() : null
            );

            $paramsArray[] = [
                'name' => substr($origParam->getVariableName(), 1) . '.' . $name,
                'description' => $this->buildDescription($origParam->getDocBlock(), $description, $origParam),
                'types' => $this->handleTypes($types),
                'optional' => null, // @todo
                'nullable' => null //@todo
            ];
        }

        return $paramsArray;
    }
46731
protected function buildEmbeddedObject(array $attributes)
    {
        $resultBuilder = new ResultBuilder($this->getRelatedMapper(), true);

        // TODO : find a way to support eager load within an embedded
        // object.
        $eagerLoads = [];

        return $resultBuilder->build([$attributes], $eagerLoads)[0];
    }
108396
public function embedsMany($parent, string $relatedClass, string $relation = null): EmbedsMany
    {
        if (is_null($relation)) {
            list(, $caller) = debug_backtrace(false);
            $relation = $caller['function'];
        }

        $this->addEmbeddedRelation($relation);
        $this->addNonProxyRelation($relation);

        return new EmbedsMany($parent, $relatedClass, $relation);
    }
255613
public function embed(&$collection, $options = [])
    {
        $indexes = $this->_index($collection, $this->keys('from'));
        $related = $this->_find(array_keys($indexes), $options);

        $name = $this->name();
        $this->_cleanup($collection);

        foreach ($related as $index => $entity) {
            $values = is_object($entity) ? $entity->{$this->keys('to')} : $entity[$this->keys('to')];
            $values = is_array($values) || $values instanceof Traversable ? $values : [$values];
            foreach ($values as $value) {
                $value = (string) $value;
                if (isset($indexes[$value])) {
                    if (is_object($collection[$indexes[$value]])) {
                        $source = $collection[$indexes[$value]];
                        if ($entity instanceof Document) {
                            $source->__get($name)->push($entity); // Too Many Magic Kill The Magic.
                        } else {
                            $source->{$name}[] = $entity;
                        }
                    } else {
                        $collection[$indexes[$value]][$name][] = &$related[$index];
                    }
                }
            }
        }
        return $related;
    }
505915
public function prepareAssociatedCollectionValue(PersistentCollectionInterface $coll, $includeNestedCollections = false)
    {
        $mapping  = $coll->getMapping();
        $pb       = $this;
        $callback = isset($mapping['embedded'])
            ? static function ($v) use ($pb, $mapping, $includeNestedCollections) {
                return $pb->prepareEmbeddedDocumentValue($mapping, $v, $includeNestedCollections);
            }
            : static function ($v) use ($pb, $mapping) {
                return $pb->prepareReferencedDocumentValue($mapping, $v);
            };

        $setData = $coll->map($callback)->toArray();
        if (CollectionHelper::isList($mapping['strategy'])) {
            $setData = array_values($setData);
        }

        $this->uow->unscheduleCollectionDeletion($coll);
        $this->uow->unscheduleCollectionUpdate($coll);

        return $setData;
    }
487611
public function embed(IStagedForm $embeddedForm) : StagedForm
    {
        foreach ($embeddedForm->getAllStages() as $stage) {
            $this->followingStages[] = $stage;
        }

        return $this;
    }
569020
public function withAllowEmbed($allowEmbed)
    {
        $choices = [
            PlayerLocationInterface::ALLOW_EMBED_NO,
            PlayerLocationInterface::ALLOW_EMBED_YES,
        ];

        Assertion::choice($allowEmbed, $choices);

        $instance = clone $this;

        $instance->allowEmbed = $allowEmbed;

        return $instance;
    }
1054853
protected function getEmbeddedEntityFor($attribute_name, $embedded_entity_id)
    {
        $found_entity = null;
        foreach ($this->getValue($attribute_name) as $embedded_entity) {
            if ($embedded_entity->getIdentifier() === $embedded_entity_id) {
                $found_entity = $embedded_entity;
                break;
            }
        }

        return $found_entity;
    }
1319114
protected function findEmbeddedObjectRelations(array $relations): array
    {
        $embeddedRelations = [];

        foreach ($relations as $relation) {
            if ($relation->getRelation() instanceof EmbeddedObjectRelation) {
                $embeddedRelations[] = $relation->getRelation();

                $embeddedRelations = array_merge(
                    $embeddedRelations,
                    $this->findEmbeddedObjectRelations($relation->getRelation()->getMapper()->getDefinition()->getToOneRelationMappings())
                );
            }
        }

        return $embeddedRelations;
    }
917081
public static Collection getEmbeddedCollectionInstance(Field embeddedCollectionField)
    {
        Collection embeddedCollection = null;
        Class embeddedCollectionFieldClass = embeddedCollectionField.getType();

        if (embeddedCollection == null || embeddedCollection.isEmpty())
        {
            if (embeddedCollectionFieldClass.equals(List.class))
            {
                embeddedCollection = new ArrayList<Object>();
            }
            else if (embeddedCollectionFieldClass.equals(Set.class))
            {
                embeddedCollection = new HashSet<Object>();
            }
            else
            {
                throw new InvalidEntityDefinitionException("Field " + embeddedCollectionField.getName()
                        + " must be either instance of List or Set");
            }
        }
        return embeddedCollection;
    }
300316
protected function getLinkParameters(DOMElement $embed)
    {
        if (!$embed->hasAttribute('url')) {
            return null;
        }

        $target = $embed->getAttribute(EmbedLinking::TEMP_PREFIX . 'target');
        $title = $embed->getAttribute(EmbedLinking::TEMP_PREFIX . 'title');
        $id = $embed->getAttribute(EmbedLinking::TEMP_PREFIX . 'id');
        $class = $embed->getAttribute(EmbedLinking::TEMP_PREFIX . 'class');
        $resourceFragmentIdentifier = $embed->getAttribute(EmbedLinking::TEMP_PREFIX . 'anchor_name');
        $resourceType = static::LINK_RESOURCE_URL;
        $resourceId = null;

        if ($embed->hasAttribute(EmbedLinking::TEMP_PREFIX . 'object_id')) {
            $resourceType = static::LINK_RESOURCE_CONTENT;
            $resourceId = $embed->getAttribute(EmbedLinking::TEMP_PREFIX . 'object_id');
        } elseif ($embed->hasAttribute(EmbedLinking::TEMP_PREFIX . 'node_id')) {
            $resourceType = static::LINK_RESOURCE_LOCATION;
            $resourceId = $embed->getAttribute(EmbedLinking::TEMP_PREFIX . 'node_id');
        }

        $parameters = [
            'href' => $embed->getAttribute('url'),
            'resourceType' => $resourceType,
            'resourceId' => $resourceId,
            'wrapped' => $this->isLinkWrapped($embed),
        ];

        if (!empty($resourceFragmentIdentifier)) {
            $parameters['resourceFragmentIdentifier'] = $resourceFragmentIdentifier;
        }

        if (!empty($target)) {
            $parameters['target'] = $target;
        }

        if (!empty($title)) {
            $parameters['title'] = $title;
        }

        if (!empty($id)) {
            $parameters['id'] = $id;
        }

        if (!empty($class)) {
            $parameters['class'] = $class;
        }

        return $parameters;
    }
190118
protected function setEmbedHasOne($key, Embed $embed = null)
    {
        if (null !== $embed) {
            $this->validateEmbedSet($key, $embed->getName());
        }
        $this->touch();
        $this->hasOneEmbeds->set($key, $embed);
        $this->doDirtyCheck();
        return $this;
    }
793182
public function loadCollection(PersistentCollectionInterface $collection)
    {
        $mapping = $collection->getMapping();
        switch ($mapping->association) {
            case PropertyMetadata::ASSOCIATION_TYPE_EMBED_MANY:
                $this->loadEmbedManyCollection($collection);
                break;

            case PropertyMetadata::ASSOCIATION_TYPE_REFERENCE_MANY:
                if ($mapping->repositoryMethod !== null) {
                    $this->loadReferenceManyWithRepositoryMethod($collection);
                } else {
                    if ($mapping->isOwningSide) {
                        $this->loadReferenceManyCollectionOwningSide($collection);
                    } else {
                        $this->loadReferenceManyCollectionInverseSide($collection);
                    }
                }
                break;
        }
    }
78758
public void prepareEmbeddedId(TopDocs docs, Map<String, Object> indexCol, IndexSearcher searcher,
            EntityMetadata metadata, MetamodelImpl metaModel)
    {
        try
        {
            for (ScoreDoc sc : docs.scoreDocs)
            {
                Document doc = searcher.doc(sc.doc);
                Map<String, Object> embeddedIdFields = new HashMap<String, Object>();
                EmbeddableType embeddableId = metaModel.embeddable(metadata.getIdAttribute().getBindableJavaType());
                Set<Attribute> embeddedAttributes = embeddableId.getAttributes();

                prepareEmbeddedIdFields(embeddedAttributes, metaModel, embeddedIdFields, doc, metadata);

                String entityId = doc.get(IndexingConstants.ENTITY_ID_FIELD);
                indexCol.put(entityId, embeddedIdFields);
            }
        }
        catch (Exception e)
        {
            log.error("Error while parsing Lucene Query {} ", e);
            throw new LuceneIndexingException(e);
        }
    }
305022
protected function parseRelationships($includedData)
    {
        $relationships = [];

        foreach ($includedData as $key => $inclusion) {
            foreach ($inclusion as $includeKey => $includeObject) {
                $relationships = $this->buildRelationships($includeKey, $relationships, $includeObject, $key);
                if (isset($includedData[0][$includeKey]['meta'])) {
                    $relationships[$includeKey][0]['meta'] = $includedData[0][$includeKey]['meta'];
                }
            }
        }

        return $relationships;
    }
27733
protected function buildEmbeddedForm()
    {
        $form = new EmbeddedForm($this->column);

        $form->setParent($this->form);

        call_user_func($this->builder, $form);

        $form->fill($this->getEmbeddedData());

        return $form;
    }