• Модуль: location
  • Путь к файлу: ~/bitrix/modules/location/lib/source/google/repository.php
  • Класс: BitrixLocationSourceGoogleRepository
  • Вызов: Repository::findParents
public function findParents(Location $location, string $languageId): ?Parents
{
	if($location->getSourceCode() !== self::$sourceCode || $location->getExternalId() == '')
	{
		return null;
	}

	$result = (new Parents())
		->setDescendant($location);

	/* Temporary. To decrease the usage of the Google API */
	return $result;
	/* */

	//We need full information about the location
	$rawData = $this->find(
		new RequestersByIdRequester($this->httpClient, $this->cachePool),
		null,
		[
			'placeid' => $location->getExternalId(),
			'language' => $languageId
		]
	);

	$ancestorDataConverter = new ConvertersAncestorDataConverter();
	$ancestorsRawData = $ancestorDataConverter->convert($rawData, $location->getType());

	//is it always available?
	$latLon = $location->getLatitude().','.$location->getLongitude();

	foreach ($ancestorsRawData as $data)
	{
		//Just searching by query taking into account lat and lon
		$res = $this->find(
			new RequestersByQueryRequester($this->httpClient, $this->cachePool),
			new ConvertersByQueryConverter($languageId),
			[
				'query' => $data['NAME'],
				//todo: may be restrict by several types?
				'location' => $latLon,
				'language' => $languageId
			]
		);

		if($res instanceof Collection && $res->count() > 0)
		{
			if(!($parentSource = $this->chooseParentFromCollection($location, $res, $result, $data['TYPES'])))
			{
				continue;
			}

			$localParent = $this->findLocalLocationByExternalId($parentSource);

			//the parent location have already been saved
			if ($localParent)
			{
				$result->addItem($localParent);

				if ($llParents = $localParent->getParents())
				{
					foreach ($llParents as $localParent)
					{
						$result->addItem($localParent);
					}
				}

				break;
			}
			else
			{
				//we need detailed info
				$detailedParent = $this->findByExternalId(
					$parentSource->getExternalId(),
					self::$sourceCode,
					$languageId
				);

				if($detailedParent)
				{
					$result->addItem($detailedParent);
				}
			}
		}
	}

	return $result;
}