From 76c146f3269ff07fdad66cb1d2d74c2acbc2af91 Mon Sep 17 00:00:00 2001 From: Artem Ziablytskyi Date: Tue, 12 Oct 2021 17:35:15 +0200 Subject: [PATCH] add `state_code` field to response address details --- docs/api/Output.md | 5 ++++- lib-php/AddressDetails.php | 18 ++++++++++++++++++ test/php/Nominatim/AddressDetailsTest.php | 1 + .../address_details_10_downing_street.json | 4 ++-- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/docs/api/Output.md b/docs/api/Output.md index 31dd5b3f..3c541d33 100644 --- a/docs/api/Output.md +++ b/docs/api/Output.md @@ -28,6 +28,7 @@ a single place (for reverse) of the following format: "city": "London", "state_district": "Greater London", "state": "England", + "state_code": "ENG", "postcode": "SW1A 2DU", "country": "United Kingdom", "country_code": "gb" @@ -126,6 +127,7 @@ formats depending on the API call. Bavaria + BY Germany de @@ -179,6 +181,7 @@ Additional information requested with `addressdetails=1`, `extratags=1` and London Greater London England + ENG SW1A 2DU United Kingdom gb @@ -279,7 +282,7 @@ with a designation label. Per default the following labels may appear: * continent * country, country_code - * region, state, state_district, county + * region, state, state_code, state_district, county * municipality, city, town, village * city_district, district, borough, suburb, subdivision * hamlet, croft, isolated_dwelling diff --git a/lib-php/AddressDetails.php b/lib-php/AddressDetails.php index 1bd475ad..a1a2a636 100644 --- a/lib-php/AddressDetails.php +++ b/lib-php/AddressDetails.php @@ -92,6 +92,10 @@ class AddressDetails || $aLine['class'] == 'place') ) { $aAddress[$sTypeLabel] = $sName; + + if ('state' === $sTypeLabel && !empty($aLine['name'])) { + $this->addStateCode($aAddress, $aLine['name']); + } } } @@ -174,4 +178,18 @@ class AddressDetails { return $this->aAddressLines; } + + /** + * Add `state_code` field to address details to represent second part of ISO 3166-2 country subdivision code + */ + private function addStateCode(&$aAddress, $nameDetails) + { + if (is_string($nameDetails)) { + $nameDetails = json_decode('{' . str_replace('"=>"', '":"', $nameDetails) . '}', true); + } + + if (!empty($nameDetails['ref'])) { + $aAddress['state_code'] = $nameDetails['ref']; + } + } } diff --git a/test/php/Nominatim/AddressDetailsTest.php b/test/php/Nominatim/AddressDetailsTest.php index 1425be2d..4cf36344 100644 --- a/test/php/Nominatim/AddressDetailsTest.php +++ b/test/php/Nominatim/AddressDetailsTest.php @@ -86,6 +86,7 @@ class AddressDetailsTest extends \PHPUnit\Framework\TestCase 'city' => 'London', 'state_district' => 'Greater London', 'state' => 'England', + 'state_code' => 'ENG', 'postcode' => 'SW1A 2AA', 'country' => 'United Kingdom', 'country_code' => 'gb' diff --git a/test/php/fixtures/address_details_10_downing_street.json b/test/php/fixtures/address_details_10_downing_street.json index 853a6c07..f56cfd9a 100644 --- a/test/php/fixtures/address_details_10_downing_street.json +++ b/test/php/fixtures/address_details_10_downing_street.json @@ -157,7 +157,7 @@ {"place_id":194325361, "osm_type":"R", "osm_id":58447, - "name":{"name": "England", "name:be": "Англія", "name:br": "Bro-Saoz", "name:ca": "Anglaterra", "name:cs": "Anglie", "name:cy": "Lloegr", "name:de": "England", "name:el": "Αγγλία", "name:en": "England", "name:eo": "Anglujo", "name:es": "Inglaterra", "name:fi": "Englanti", "name:fr": "Angleterre", "name:fy": "Ingelân", "name:ga": "Sasana", "name:gd": "Sasainn", "name:gv": "Sostyn", "name:he": "אנגליה", "name:hu": "Anglia", "name:ia": "Anglaterra", "name:io": "Anglia", "name:it": "Inghilterra", "name:la": "Anglia", "name:lt": "Anglija", "name:nl": "Engeland", "name:pl": "Anglia", "name:pt": "Inglaterra", "name:ru": "Англия", "name:sk": "Anglicko", "name:sv": "England", "name:tr": "İngiltere", "name:uk": "Англія", "name:vi": "Anh", "name:vo": "Linglän", "name:zh": "英格蘭", "name:hsb": "Jendźelska", "name:nds": "England", "name:tok": "ma Inli", "name:tzl": "Anglatzara", "alt_name:eo": "Anglio", "alt_name:ia": "Anglia", "old_name:vi": "Anh Quốc", "alt_name:nds": "Ingland", "name:be-tarask": "Ангельшчына", "name:zh-classical": "英格蘭", "name:zh-simplified": "英格兰", "name:zh-traditional": "英格蘭"}, + "name":{"ref": "ENG", "name": "England", "name:be": "Англія", "name:br": "Bro-Saoz", "name:ca": "Anglaterra", "name:cs": "Anglie", "name:cy": "Lloegr", "name:de": "England", "name:el": "Αγγλία", "name:en": "England", "name:eo": "Anglujo", "name:es": "Inglaterra", "name:fi": "Englanti", "name:fr": "Angleterre", "name:fy": "Ingelân", "name:ga": "Sasana", "name:gd": "Sasainn", "name:gv": "Sostyn", "name:he": "אנגליה", "name:hu": "Anglia", "name:ia": "Anglaterra", "name:io": "Anglia", "name:it": "Inghilterra", "name:la": "Anglia", "name:lt": "Anglija", "name:nl": "Engeland", "name:pl": "Anglia", "name:pt": "Inglaterra", "name:ru": "Англия", "name:sk": "Anglicko", "name:sv": "England", "name:tr": "İngiltere", "name:uk": "Англія", "name:vi": "Anh", "name:vo": "Linglän", "name:zh": "英格蘭", "name:hsb": "Jendźelska", "name:nds": "England", "name:tok": "ma Inli", "name:tzl": "Anglatzara", "alt_name:eo": "Anglio", "alt_name:ia": "Anglia", "old_name:vi": "Anh Quốc", "alt_name:nds": "Ingland", "name:be-tarask": "Ангельшчына", "name:zh-classical": "英格蘭", "name:zh-simplified": "英格兰", "name:zh-traditional": "英格蘭"}, "class":"boundary", "type":"administrative", "admin_level":4, @@ -214,4 +214,4 @@ "rank_address":4, "distance":0, "localname":"gb"} -] \ No newline at end of file +] -- 2.39.5