/** * @param FullPlaceResult $place * @param array $addressesData */ protected function setAddress(FullPlaceResult $place, $addressesData) { $streetName = ''; $streetNumber = null; $premiseComponents = array(); foreach ($addressesData as $data) { if (in_array('street_number', $data['types'])) { $place->setStreetNumber($data['long_name']); $streetNumber = $data['long_name']; } elseif (in_array('premise', $data['types'])) { $premiseComponents[] = $data['long_name']; } elseif (in_array('subpremise', $data['types'])) { $premiseComponents[] = $data['long_name']; } elseif (in_array('route', $data['types'])) { $place->setStreetName($data['long_name']); $streetName = $data['long_name']; } elseif (in_array('locality', $data['types'])) { $place->setCity($data['long_name']); } elseif (in_array('country', $data['types'])) { $place->setCountry($data['long_name']); $place->setCountryCode($data['short_name']); } elseif (in_array('postal_code', $data['types'])) { $place->setPostalCode($data['long_name']); } elseif (in_array('administrative_area_level_1', $data['types'])) { $place->setRegion($data['long_name']); $place->setRegionCode($data['short_name']); } elseif (in_array('administrative_area_level_2', $data['types'])) { $place->setCounty($data['long_name']); $place->setCountyCode($data['short_name']); } } if ($streetNumber !== null && count($premiseComponents) > 0) { $formattedAddress = sprintf('%s - %s, %s', implode(' ', $premiseComponents), $streetNumber, $streetName); } else { if (count($premiseComponents) > 0) { $formattedAddress = sprintf('%s, %s', implode(' ', $premiseComponents), $streetName); } else { if ($streetNumber !== null) { $formattedAddress = $streetNumber . ', ' . $streetName; } else { $formattedAddress = $streetName; } } } $place->setFormattedAddress($formattedAddress); }