46 if (!$this->isDetailsValid())
51 $addressFieldCollection = $this->makeAddressFieldCollection($languageId);
52 $locationTypeField = $this->getLocationTypeField($addressFieldCollection);
54 if ($locationTypeField ===
null)
59 list($latitude, $longitude) = $this->getCoordinates();
61 $address = (
new Address($languageId))
62 ->setLatitude($latitude)
63 ->setLongitude($longitude)
64 ->setFieldCollection($addressFieldCollection);
66 if ($addressLine1 = $this->createAddressLine1($address))
68 $address->setFieldValue(FieldType::ADDRESS_LINE_1, $addressLine1);
72 $this->details[
'osm_type'],
73 $this->details[
'osm_id']
84 ->setExternalId($externalId)
85 ->setType($locationTypeField->getType())
86 ->setName($locationTypeField->getValue())
87 ->setLatitude($latitude)
88 ->setLongitude($longitude)
89 ->setLanguageId($languageId)
90 ->setAddress($address);
92 if($address->isFieldExist(FieldType::POSTAL_CODE))
94 $location->setFieldValue(
95 FieldType::POSTAL_CODE,
96 $address->getFieldValue(FieldType::POSTAL_CODE)
106 private function isDetailsValid(): bool
108 if (!isset($this->details[
'osm_type']))
113 if (!isset($this->details[
'osm_id']))
118 if (!isset($this->details[
'address']) || !is_array($this->details[
'address']))
126 $this->addressComponents = array_filter(
127 $this->details[
'address'],
128 static function (array $addressComponent)
130 if (!isset($addressComponent[
'isaddress']))
135 return (
bool)$addressComponent[
'isaddress'];
139 if (empty($this->details[
'address']))
144 if (!isset($this->details[
'country_code']))
149 if (!$this->getCountry())
154 if (!$this->getCoordinates())
166 private function makeAddressFieldCollection(
string $languageId): Address\FieldCollection
168 $result =
new Address\FieldCollection();
170 $postalCode = $this->getPostalCode();
174 (
new Field(FieldType::POSTAL_CODE))->setValue($postalCode)
178 $country = $this->getCountry();
179 if ($country && isset($country[
'localname']))
182 (
new Field(FieldType::COUNTRY))->setValue($country[
'localname'])
187 if ($adminLevel1 && isset($adminLevel1[
'localname']))
190 (
new Field(FieldType::ADM_LEVEL_1))->setValue($adminLevel1[
'localname'])
195 if ($adminLevel2 && isset($adminLevel2[
'localname']))
198 (
new Field(FieldType::ADM_LEVEL_2))->setValue($adminLevel2[
'localname'])
203 if ($adminLevel3 && isset($adminLevel3[
'localname']))
206 (
new Field(FieldType::ADM_LEVEL_3))->setValue($adminLevel3[
'localname'])
211 if ($adminLevel4 && isset($adminLevel4[
'localname']))
214 (
new Field(FieldType::ADM_LEVEL_4))->setValue($adminLevel4[
'localname'])
219 if ($locality && isset($locality[
'localname']))
222 (
new Field(FieldType::LOCALITY))->setValue($locality[
'localname'])
227 if ($subLocality && isset($subLocality[
'localname']))
230 (
new Field(FieldType::SUB_LOCALITY))->setValue($subLocality[
'localname'])
235 if ($subLocalityLevel1 && isset($subLocalityLevel1[
'localname']))
238 (
new Field(FieldType::SUB_LOCALITY_LEVEL_1))->setValue($subLocalityLevel1[
'localname'])
243 if ($subLocalityLevel2 && isset($subLocalityLevel2[
'localname']))
246 (
new Field(FieldType::SUB_LOCALITY_LEVEL_2))->setValue($subLocalityLevel2[
'localname'])
251 if ($street && isset($street[
'localname']))
254 (
new Field(FieldType::STREET))->setValue($street[
'localname'])
259 if ($house && isset($house[
'localname']))
262 (
new Field(FieldType::BUILDING))->setValue($house[
'localname'])
267 if ($addressLine2 && isset($addressLine2[
'localname']))
270 (
new Field(FieldType::ADDRESS_LINE_2))->setValue($addressLine2[
'localname'])
281 private function createAddressLine1(Address $address): ?string
283 $format = FormatService::getInstance()->findDefault($address->getLanguageId());
285 return StringConverter::convertToStringTemplate(
288 StringConverter::STRATEGY_TYPE_TEMPLATE,
289 StringConverter::CONTENT_TYPE_TEXT
296 private function getCountry(): ?array
305 foreach ($this->addressComponents as $addressComponent)
307 if ($addressComponent[
'class'] ===
'boundary'
308 && $addressComponent[
'type'] ===
'administrative'
309 && $addressComponent[
'admin_level'] === static::COUNTRY_ADMIN_LEVEL
312 return $addressComponent;
323 foreach ($this->addressComponents as $addressComponent)
325 if ($addressComponent[
'class'] ===
'place' && $addressComponent[
'type'] ===
'country')
327 return $addressComponent;
372 foreach ($this->addressComponents as $addressComponent)
377 $this->details[
'osm_type'] === $addressComponent[
'osm_type']
378 && $this->details[
'osm_id'] === $addressComponent[
'osm_id']
381 if ($isItself && in_array($componentPlaceType, $settlementTypePriorityList,
true))
383 return $addressComponent;
388 if ($addressComponent)
390 return $addressComponent;
409 $adminLevel1[
'osm_type'] ===
'R'
420 if ($addressComponent)
422 return $addressComponent;
426 if ($addressComponent)
428 return $addressComponent;
474 foreach ($this->addressComponents as $addressComponent)
476 if (in_array($addressComponent[
'rank_address'], [26, 27],
true))
478 return $addressComponent;
482 if (!empty($this->details[
'addresstags'][
'street']))
485 'localname' => $this->details[
'addresstags'][
'street'],
500 foreach ($this->addressComponents as $addressComponent)
502 if ($addressComponent[
'rank_address'] == 28)
504 return $addressComponent;
508 if (!empty($this->details[
'addresstags'][
'housenumber']))
511 'localname' => $this->details[
'addresstags'][
'housenumber'],
526 foreach ($this->addressComponents as $addressComponent)
528 if ($addressComponent[
'rank_address'] >= 29)
530 return $addressComponent;
540 private function getPostalCode(): ?string
542 return $this->details[
'calculated_postcode'] ??
null;
552 $items = array_reverse($addressFieldCollection->getSortedItems());
554 foreach ($items as $item)
556 if (!
Location\Type::isValueExist($item->getType()))
570 private function getCoordinates(): ?array
572 if (!isset($this->details[
'centroid'])
573 || !isset($this->details[
'centroid'][
'type'])
574 || $this->details[
'centroid'][
'type'] !==
'Point'
575 || !isset($this->details[
'centroid'][
'coordinates'])
576 || !is_array($this->details[
'centroid'][
'coordinates'])
577 || count($this->details[
'centroid'][
'coordinates']) != 2
584 (float)$this->details[
'centroid'][
'coordinates'][1],
585 (
float)$this->details[
'centroid'][
'coordinates'][0],
596 $addressComponent[
'class'] ===
'boundary'
597 && $addressComponent[
'type'] ===
'administrative'
607 foreach ($this->addressComponents as $addressComponent)
610 && $addressComponent[
'admin_level'] == $level
613 return $addressComponent;
645 if (isset($addressComponent[
'class']) && $addressComponent[
'class'] ===
'place')
647 return $addressComponent[
'type'];
650 if (isset($addressComponent[
'place_type']))
652 return $addressComponent[
'place_type'];
666 foreach ($settlementTypePriorityList as $settlementType)
668 foreach ($this->addressComponents as $addressComponent)
670 if (!in_array($addressComponent[
'osm_type'], $types,
true))
676 if (!$componentPlaceType)
681 if ($componentPlaceType === $settlementType)
683 return $addressComponent;