• Модуль: location
  • Путь к файлу: ~/bitrix/modules/location/lib/source/google/repository.php
  • Класс: BitrixLocationSourceGoogleRepository
  • Вызов: Repository::chooseParentFromCollection
protected function chooseParentFromCollection(
	Location $location,
	Collection $collection,
	Parents $parentResultCollection,
	array $parentTypes
): ?Location
{
	if($collection->count() <= 0)
	{
		return null;
	}

	$candidatesTypes = [];
	$result = null;

	for($i = 0, $l = $collection->count(); $i < $l; $i++)
	{
		$candidate = $collection[$i];

		if($location->getExternalId() === $candidate->getExternalId())
		{
			continue;
		}

		$candidateType = $candidate->getType();

		if($candidateType === LocationType::UNKNOWN)
		{
			continue;
		}

		if($location->getType() !== LocationType::UNKNOWN && $candidate->getType() >= $location->getType())
		{
			continue;
		}

		// check if we already have the same location in result parents collection
		if($this->isCollectionContainLocation($candidate, $parentResultCollection))
		{
			continue;
		}

		if(in_array($candidateType, $parentTypes, true))
		{
			return $candidate;
		}

		$candidatesTypes[] = [$i, $candidateType];
	}

	if(count($candidatesTypes) <= 0)
	{
		return null;
	}

	if(count($candidatesTypes) > 1)
	{
		$typeColumn = array_column($candidatesTypes, 1);
		array_multisort($typeColumn, SORT_ASC, $candidatesTypes);
	}

	return $collection[$candidatesTypes[0][0]];
}