checks if a collection for main attribute.

@param $arrayCollection
@param $mainEntity will be set, if found

@return mixed
507509
private function hasMain($arrayCollection, &$mainEntity = null)
    {
        if ($arrayCollection && !$arrayCollection->isEmpty()) {
            return $arrayCollection->exists(
                function($index, $entity) {
                    $mainEntity = $entity;

                    return true === $entity->getMain();
                }
            );
        }

        return false;
    }
747795
protected function validateCollectionItem()
    {
        $id = $this->getUpdatingId();

        if ($id === false) {
            return null;
        }

        $data = $this->collection_item->get($id);

        if (empty($data)) {
            $this->setErrorUnavailable('update', $this->translation->text('Collection item'));
            return false;
        }

        $this->setSubmitted('update', $data);
        return true;
    }
461053
protected function collectionExists($collectionName): bool
    {
        foreach ($this->database->listCollections() as $collection) {
            if ($collection->getName() === $collectionName) {
                return true;
            }
        }

        return false;
    }
545352
private function hydrateItems(
        $daddyName,
        array $items,
        MetadataDataObjectJson $metadata,
        MetaDataCollection $mainCollection
    ) {
        $mergeArray = true;

        if ($mergeArray === true && $this->itemsAreAllOfType($items, array('object')) === true) {
            $metadata->appendRelation(
                $this->hydrateMetaDataRelationColumn(
                    $daddyName,
                    $this->mergeItems($items),
                    $metadata,
                    $mainCollection
                )
            );
        } else {
            $specialProperties = array();

            foreach ($items as $propName => $item) {
                // Relation of relation
                if (in_array($item->getType(), array('object', 'array')) === false) {
                    $specialProperties[$propName] = $item;
                    continue;
                }

                $metadata->appendRelation(
                    $this->hydrateMetaDataRelationColumn($daddyName, $item->getProperties(), $metadata, $mainCollection)
                );
            }
        }

        return $metadata;
    }
861412
protected function getInternalValues(AttributeInterface $attribute = null, array $context = null)
    {
        if ($context) {
            $context = array_merge($this->getCurrentContext(), $context);
        } else {
            $context = $this->getCurrentContext();
        }

        if (null === $attribute) {
            $values = new ArrayCollection();
            foreach ($this->values as $value) {
                if (!$this->getFamily()->hasAttribute($value->getAttributeCode())) {
                    $this->removeValue($value);
                    continue;
                }
                $attribute = $this->getFamily()->getAttribute($value->getAttributeCode());
                if ($attribute->isContextMatching($value, $context)) {
                    $values->add($value);
                }
            }

            return $values;
        }
        $this->checkAttribute($attribute);

        $values = new ArrayCollection();
        foreach ($this->getValuesByAttribute($attribute) as $value) {
            if ($value instanceof ContextualValueInterface) {
                if ($attribute->isContextMatching($value, $context)) {
                    $values->add($value);
                }
            } else {
                $values->add($value);
            }
        }

        return $values;
    }
65429
private function parseBots(MetadataCollection $collection, $content)
    {
        if (!isset($content['bots'])) {
            return;
        }

        foreach ($content['bots'] as $name => $bot) {
            $ip = isset($bot['ip']) ? $bot['ip'] : null;
            $metadata = new Metadata($name, $bot['agent'], $ip);

            if (isset($bot['type'])) {
                $metadata->setType($bot['type']);
            }

            if (isset($bot['meta'])) {
                $metadata->setMeta($bot['meta']);
            }

            if (isset($bot['agent_match'])) {
                $metadata->setAgentMatch($bot['agent_match']);
            }

            $collection->addMetadata($metadata);
        }
    }
1145438
public function setPrimary($primaryName) {
		if (is_array($primaryName)) {
			$idProperty = self::COMPOSITE_ID_PROPERTY . $this->_collectionId;
			
			foreach ($this->_data as &$dataPart) {
				$dataHashParts = [];
				
				foreach ($primaryName as $keyPart) {
					if (! isset($dataPart[$keyPart])) {
						throw new Exception('Undefined property can\'t been used as a part of a composite primary');
					}
					
					$dataHashParts[] = $dataPart[$keyPart];
				}
				
				$dataPart[$idProperty] = $this->arrayHashFunction($dataHashParts);
			}
			unset($dataPart);
			
			$this->_primaryDefinition = $primaryName;
			
			$primaryName = $idProperty;
		} else {
			$this->_primaryDefinition = $primaryName;
		}
		
		$tempData = $this->_data;
		$this->_data = [];
		
		foreach ($tempData as &$item) {
			if (! isset($item[$primaryName])) {
				throw new Exception('Undefined property can\'t been used as a primary');
			}
			
			$this->_data[$item[$primaryName]] = &$item;
		}
		unset($item, $tempData);
		
		if (! empty($this->_models)) {
			$tempData = $this->_models;
			$this->_models = [];
			
			foreach ($tempData as $item) {
				$this->_models[$item->$primaryName] = $item;
			}
			
			unset($tempData);
		}
		
		$this->_pointer = 0;
		$this->_map     = array_keys($this->_data);
		$this->_primary = $primaryName;
		
		return $this;
	}
93339
private function pdoMetadataToGeneratorMetadata(array $metadataCollection)
    {
        $metaDataCollection = new MetaDataCollection();

        foreach ($metadataCollection as $tableName) {
            $metaDataCollection->append(
                $this->hydrateDataObject($tableName)
            );
        }

        return $metaDataCollection;
    }
302027
private <E> boolean onCheckCollectionAttribute(PluralAttribute<? super X, ?, ?> pluralAttribute, Class<E> paramClass)
    {
        if (pluralAttribute != null)
        {
            if (isCollectionAttribute(pluralAttribute) && isBindable(pluralAttribute, paramClass))
            {
                return true;
            }

        }

        return false;
    }
1124169
public function setMainForCollection($arrayCollection)
    {
        if ($arrayCollection && !$arrayCollection->isEmpty() && !$this->hasMain($arrayCollection)) {
            $arrayCollection->first()->setMain(true);
        }
    }
1007937
public function search($mainEntityClassname, $config = [])
    {
        return Yii::$container->get(SearchQuery::class, [$mainEntityClassname, $this, $config]);
    }
1250758
public function isCollectionValuedAssociation($fieldName) : bool
    {
        return isset($this->associationMappings[$fieldName]['type']) &&
            $this->associationMappings[$fieldName]['type'] === 'many';
    }
881842
public function even(): ArrayCollection
    {
        $items = [];
        $pos = 0;

        foreach ($this->items as $key => $value) {
            if ($pos % 2 === 0) {
                $items[$key] = $value;
            }
            $pos++;
        }

        return new static($items);
    }
594046
public function hasAncestor($itemName, $ancestorName)
    {
        $ancestors = $this->getAncestors($itemName);
        return isset($ancestors[$ancestorName]);
    }
523637
public function getAccountObject(array $returnAttribs = array(), array $omitAttribs = array())
    {
        if (!$this->_authenticatedDn) {
            return false;
        }

        $returnObject = new stdClass();

        $returnAttribs = array_map('strtolower', $returnAttribs);
        $omitAttribs   = array_map('strtolower', $omitAttribs);
        $returnAttribs = array_diff($returnAttribs, $omitAttribs);

        $entry = $this->getLdap()->getEntry($this->_authenticatedDn, $returnAttribs, true);
        foreach ($entry as $attr => $value) {
            if (in_array($attr, $omitAttribs)) {
                // skip attributes marked to be omitted
                continue;
            }
            if (is_array($value)) {
                $returnObject->$attr = (count($value) > 1) ? $value : $value[0];
            } else {
                $returnObject->$attr = $value;
            }
        }
        return $returnObject;
    }
378546
protected function validateEntityCollectionItem()
    {
        if ($this->isError()) {
            return null;
        }

        $collection = $this->getSubmitted('collection');
        $collection_id = $this->getSubmitted('collection_id');

        if (empty($collection)) {

            if (!isset($collection_id)) {
                $updating = $this->getUpdating();
                if (isset($updating['collection_id'])) {
                    $collection_id = $updating['collection_id'];
                }
            }

            $collection = $this->collection->get($collection_id);
        }

        if (empty($collection['type'])) {
            $this->setErrorUnavailable('collection_id', $this->translation->text('Collection'));
            return false;
        }

        try {
            $entity_id = $this->getSubmitted('entity_id');
            $handlers = $this->collection->getHandlers();
            $result = static::call($handlers, $collection['type'], 'validate', array($entity_id));
        } catch (Exception $ex) {
            $result = $ex->getMessage();
        }

        if ($result === true) {
            return true;
        }

        $this->setError('entity_id', implode('<br>', (array) $result));
        return false;
    }
1149768
public static function keysExist(array $collection, array $keys)
    {
        $collection = array_flip($collection);

        foreach ($keys as $key) {
            if (!in_array($key, $collection)) {
                return false;
            }
        }

        return true;
    }