Expands a collection of entities by adding their related data.

@param  mixed $collection The collection to expand.
@return array             The collection of related entities.
163141
public function embed(&$collection, $options = [])
    {
        $name = $this->name();
        $through = $this->through();
        $using = $this->using();

        $from = $this->from();
        $relThrough = $from::definition()->relation($through);

        // Embedding twice is unstable
        // $middle = $relThrough->embed($collection, $options);

        $pivot = $relThrough->to();
        $relUsing = $pivot::definition()->relation($using);

        // Embedding twice is unstable
        // $related = $relUsing->embed($middle, $options);

        $to = $relUsing->to();
        $toSchema = $to::definition();

        foreach ($collection as $index => $entity) {
            if ($entity instanceof Document) {
                $entity->{$name} = new Through([
                    'parent'  => $entity,
                    'schema'  => $toSchema,
                    'through' => $through,
                    'using'   => $using
                ]);
            } elseif (is_object($entity)) {
                $entity->{$name} = [];
            } else {
                $collection[$index][$name] = [];
            }
        }

        foreach ($collection as $index => $entity) {
            if ($entity instanceof Document) {
                continue;
            } elseif (is_object($entity)) {
                foreach ($entity->{$through} as $key => $item) {
                    $entity->{$name}[] = $item->{$using};
                }
            } else {
                foreach ($entity[$through] as $key => $item) {
                    $collection[$index][$name][] = &$entity[$through][$key][$using];
                }
            }
        }
    }
1322205
public function merge(Collection $collection) {
		if ($collection->isEmpty()) {
			return $this;
		}
		
		$primary = $this->getPrimary();
		
		if ($primary !== null) {
			$sourcePrimary = $collection->getPrimary();
		
			if ($primary !== $sourcePrimary) {
				$collection = clone $collection;
				$collection->setPrimary($primary);
			}
		}
		
		$data   = &$collection->getDataLinked();
		$models = $collection->getInstancedModels();
		
		if ($primary === null) {
			$this->_data = array_merge($this->_data, $data);
			
			if (! empty($models)) {
				$this->_models = array_merge($this->_models , $models);
			}
		} else {
			$this->_data = $data + $this->_data;
			
			if (! empty($models)) {
				$this->_models = $models + $this->_models;
			}
		}
		
		$this->_pointer = 0;
		$this->_map     = array_keys($this->_data);
		
		return $this;
	}
361640
public function loadCollectionItemsData(Collection $collection): array
    {
        $query = $this->getItemSelectQuery();
        $query->where(
            $query->expr()->eq('collection_id', ':collection_id')
        )
        ->setParameter('collection_id', $collection->id, Type::INTEGER);

        $this->applyStatusCondition($query, $collection->status);

        $query->addOrderBy('position', 'ASC');

        return $query->execute()->fetchAll(PDO::FETCH_ASSOC);
    }
624611
private function deriveDtoAndEntityFqnFromCollection(Collection $collection): array
    {
        if (0 === $collection->count()) {
            throw new \RuntimeException('Collection is empty');
        }
        $dtoFqn              = null;
        $collectionEntityFqn = null;
        foreach ($collection as $dto) {
            if ($dto instanceof EntityInterface) {
                $collectionEntityFqn = \get_class($dto);
                continue;
            }
            if (false === ($dto instanceof DataTransferObjectInterface)) {
                throw new \InvalidArgumentException(
                    'Found none DTO item in collection, was instance of ' . \get_class($dto)
                );
            }
            if (null === $dtoFqn) {
                $dtoFqn = \get_class($dto);
                continue;
            }
            if (false === ($dto instanceof $dtoFqn)) {
                throw new \InvalidArgumentException(
                    'Mismatched collection, expecting dtoFqn ' .
                    $dtoFqn .
                    ' but found ' .
                    \get_class($dto)
                );
            }
        }
        if (null === $dtoFqn && null === $collectionEntityFqn) {
            throw new \RuntimeException('Failed deriving either the DTO or Entity FQN from the collection');
        }
        if (null === $collectionEntityFqn) {
            $collectionEntityFqn = $this->namespaceHelper->getEntityFqnFromEntityDtoFqn($dtoFqn);
        }
        if (null === $dtoFqn) {
            $dtoFqn = $this->namespaceHelper->getEntityDtoFqnFromEntityFqn($collectionEntityFqn);
        }

        return [$dtoFqn, $collectionEntityFqn];
    }
1052666
function giveCollection(MongoDB\Collection $collection)
    {
        if ($this->collection)
            throw new exImmutable;


        $this->collection = $collection;
        return $this;
    }
1356193
public static function get_metadata(collection $collection) : collection {
        $data = [
            'assignment' => 'privacy:metadata:assignmentid',
            'grade' => 'privacy:metadata:gradepurpose',
            'commenttext' => 'privacy:metadata:commentpurpose'
        ];
        $collection->add_database_table('assignfeedback_comments', $data, 'privacy:metadata:tablesummary');
        $collection->link_subsystem('core_files', 'privacy:metadata:filepurpose');

        return $collection;
    }
781202
protected function buildDictionary(RecordCollection $collection)
    {
        if ($collection->isEmpty()) {
            return [];
        }
        $dictionary = [];
        foreach ($collection as $record) {
            $dictionary[$this->getDictionaryKey($record)] = $record;
        }

        return $dictionary;
    }
166227
public function loadCollection(PersistentCollection $collection)
    {
        $association = $collection->getMapping();
        $persister   = $this->getEntityPersister($association->getTargetEntity());

        if ($association instanceof OneToManyAssociationMetadata) {
            $persister->loadOneToManyCollection($association, $collection->getOwner(), $collection);
        } else {
            $persister->loadManyToManyCollection($association, $collection->getOwner(), $collection);
        }

        $collection->setInitialized(true);
    }
1224695
public function collection(Collection $collection)
    {
        $entries = new Entries();

        foreach ($collection as $category) {
            $entry = new Entry($category);

            $entry
                ->field('id')
                ->field('label')
                ->field('slug')
                ->field('parent')
                ->field('position')
            ;

            $entries->addEntry($entry);
        }

        return $entries->toArray();
    }
801919
public function collection($collection, $wrap = true)
    {
        $collection = $this->database->selectCollection($collection);
        $wrap && $collection = new Collection($collection);

        return $collection;
    }
566964
public function newObject($collection)
    {
        /** @var MongoObject $obj */
        $obj =$this->container->get($this->classMap->getClassForResult($collection, []));
        $obj->setCollection($collection);

        return $obj;
    }
471240
public function loadCollectionQueryData(Collection $collection): array
    {
        $query = $this->getQuerySelectQuery();
        $query->where(
            $query->expr()->eq('q.collection_id', ':collection_id')
        )
        ->setParameter('collection_id', $collection->id, Type::INTEGER);

        $this->applyStatusCondition($query, $collection->status, 'q.status');

        return $query->execute()->fetchAll(PDO::FETCH_ASSOC);
    }
158690
public function transform($collection)
    {
        if (null === $collection) {
            return [];
        }

        if (!$collection instanceof Collection) {
            throw new TransformationFailedException(sprintf('Expected a %s object.', Collection::class));
        }

        $values = [];
        foreach ($collection as $field) {
            $values[$field->getId()] = $field;
        }

        return $values;
    }
1240916
public function keys($collection=null) {
    list($collection) = self::_wrapArgs(func_get_args(), 1);
    
    if(!is_object($collection) && !is_array($collection)) throw new Exception('Invalid object');
    
    return self::_wrap(array_keys((array) $collection));
  }
1079005
public static function getTypes($collection = false)
    {
        return array_merge($collection ? self::getCompoundTypes() : self::getSimpleTypes(), self::getEmptyTypes());
    }
807834
public function collection(Collection $collection)
    {
        $entries = new Entries();

        foreach ($collection as $order) {
            $entry = new Entry($order);

            $entry
                ->conditionalField('id', function () {
                    return Subbly::api('subbly.user')->hasAccess('subbly.backend.auth');
                })

                ->field('uid')
                ->field('status')
                ->decimal('total_price', (float) $order->total_price)
                ->integer('total_items', (int) $order->total_items)
                ->decimal('shipping_cost', (float) $order->shipping_cost)

                ->relationshipField('user', 'Subbly\\Presenter\\V1\\UserPresenter')
                ->relationshipField('billing_address', 'Subbly\\Presenter\\V1\\OrderAddressPresenter')
                ->relationshipField('shipping_address', 'Subbly\\Presenter\\V1\\OrderAddressPresenter')
                ->relationshipField('products', 'Subbly\\Presenter\\V1\\OrderProductPresenter')

                ->dateField('created_at')
                ->dateField('updated_at')
            ;

            $entries->addEntry($entry);
        }

        return $entries->toArray();
    }
1180573
public function decorate(Collection $collection)
    {
        if ($this->storeManager->isSingleStoreMode()) {
            return;
        }

        $this->addLinkedWebsiteIds($collection);
        $this->ensureWebsiteLinkedSet($collection);
    }