Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
byidconverter.php
1<?php
2
4
10
18{
23 public function convert(array $data)
24 {
25 if(isset($data['status']) && $data['status'] !== 'OK')
26 {
27 $errorMessage = $data['error_message'].' ('.$data['status'].')' ?? $data['status'];
28 throw new RuntimeException($errorMessage, Google\ErrorCodes::CONVERTER_BYID_ERROR);
29 }
30
31 if(!isset($data['result']))
32 {
33 return null;
34 }
35
36 $data = $data['result'];
37
38 if(isset($data['types']) && is_array($data['types']))
39 {
40 $type = $this->convertTypes($data['types'], Location\Type::class);
41 }
42 else
43 {
44 $type = Location\Type::UNKNOWN;
45 }
46
47 $result = (new Location())
48 ->setSourceCode(Google\Repository::getSourceCode())
49 ->setExternalId((string)$data['place_id'])
50 ->setName((string)$data['name'])
51 ->setLongitude((string)$data['geometry']['location']['lng'])
52 ->setLatitude((string)$data['geometry']['location']['lat'])
53 ->setType($type)
54 ->setLanguageId($this->languageId);
55
56 if(is_array($data['address_components']))
57 {
58 if ($address = $this->createAddress($data['address_components']))
59 {
60 $address->setLatitude($result->getLatitude());
61 $address->setLongitude($result->getLongitude());
62 $result->setAddress($address);
63
64 if($address->isFieldExist(FieldType::POSTAL_CODE))
65 {
66 $result->setFieldValue(
67 FieldType::POSTAL_CODE,
68 $address->getFieldValue(FieldType::POSTAL_CODE)
69 );
70 }
71
72 if(!$address->isFieldExist(FieldType::ADDRESS_LINE_2) && $type === Location\Type::UNKNOWN)
73 {
74 $address->setFieldValue(FieldType::ADDRESS_LINE_2, (string)$data['name']);
75 }
76 }
77 }
78
79 return $result;
80 }
81}
82