• Модуль: location
  • Путь к файлу: ~/bitrix/modules/location/lib/source/google/converters/byidconverter.php
  • Класс: BitrixLocationSourceGoogleConvertersByIdConverter
  • Вызов: ByIdConverter::convert
public function convert(array $data)
{
	if(isset($data['status']) && $data['status'] !== 'OK')
	{
		$errorMessage = $data['error_message'].' ('.$data['status'].')' ?? $data['status'];
		throw new RuntimeException($errorMessage, GoogleErrorCodes::CONVERTER_BYID_ERROR);
	}

	if(!isset($data['result']))
	{
		return null;
	}

	$data = $data['result'];

	if(isset($data['types']) && is_array($data['types']))
	{
		$type = $this->convertTypes($data['types'], LocationType::class);
	}
	else
	{
		$type = LocationType::UNKNOWN;
	}

	$result = (new Location())
		->setSourceCode(GoogleRepository::getSourceCode())
		->setExternalId((string)$data['place_id'])
		->setName((string)$data['name'])
		->setLongitude((string)$data['geometry']['location']['lng'])
		->setLatitude((string)$data['geometry']['location']['lat'])
		->setType($type)
		->setLanguageId($this->languageId);

	if(is_array($data['address_components']))
	{
		if ($address = $this->createAddress($data['address_components']))
		{
			$address->setLatitude($result->getLatitude());
			$address->setLongitude($result->getLongitude());
			$result->setAddress($address);

			if($address->isFieldExist(FieldType::POSTAL_CODE))
			{
				$result->setFieldValue(
					FieldType::POSTAL_CODE,
					$address->getFieldValue(FieldType::POSTAL_CODE)
				);
			}

			if(!$address->isFieldExist(FieldType::ADDRESS_LINE_2) && $type === LocationType::UNKNOWN)
			{
				$address->setFieldValue(FieldType::ADDRESS_LINE_2, (string)$data['name']);
			}
		}
	}

	return $result;
}