Skip to content

Commit 36a36e8

Browse files
committed
[IpStack] Provide Administrative area
1 parent b722a3c commit 36a36e8

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

src/Provider/Ipstack/Ipstack.php

+27-20
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@ public function geocodeQuery(GeocodeQuery $query): Collection
7373
$url = sprintf('%s&language=%s', $url, $query->getLocale());
7474
}
7575

76+
return $this->executeQuery($url);
77+
78+
}
79+
80+
public function reverseQuery(ReverseQuery $query): Collection
81+
{
82+
throw new UnsupportedOperation('The Ipstack provider is not able to do reverse geocoding.');
83+
}
84+
85+
public function getName(): string
86+
{
87+
return 'ipstack';
88+
}
89+
90+
private function executeQuery(string $url): AddressCollection
91+
{
7692
$body = $this->getUrlContents($url);
7793
$data = json_decode($body, true);
7894

@@ -99,26 +115,17 @@ public function geocodeQuery(GeocodeQuery $query): Collection
99115
return new AddressCollection([]);
100116
}
101117

102-
$locations[] = Address::createFromArray([
103-
'providedBy' => $this->getName(),
104-
'latitude' => $data['latitude'] ?: null,
105-
'longitude' => $data['longitude'] ?: null,
106-
'locality' => $data['city'] ?: null,
107-
'postalCode' => $data['zip'] ?: null,
108-
'country' => $data['country_name'] ?: null,
109-
'countryCode' => $data['country_code'] ?: null,
118+
return new AddressCollection([
119+
Address::createFromArray([
120+
'providedBy' => $this->getName(),
121+
'latitude' => $data['latitude'] ?? null,
122+
'longitude' => $data['longitude'] ?? null,
123+
'locality' => $data['city'] ?? null,
124+
'postalCode' => $data['zip'] ?? null,
125+
'country' => $data['country_name'] ?? null,
126+
'adminLevels' => isset($data['region_name']) ? [['name' => $data['region_name'], 'level' => 1]] : [],
127+
'countryCode' => $data['country_code'] ?? null,
128+
])
110129
]);
111-
112-
return new AddressCollection($locations);
113-
}
114-
115-
public function reverseQuery(ReverseQuery $query): Collection
116-
{
117-
throw new UnsupportedOperation('The Ipstack provider is not able to do reverse geocoding.');
118-
}
119-
120-
public function getName(): string
121-
{
122-
return 'ipstack';
123130
}
124131
}

src/Provider/Ipstack/Tests/IpstackTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function testGeocodeWithLocalhostIPv4(): void
6464
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
6565
$this->assertEquals('localhost', $result->getLocality());
6666
$this->assertEquals('localhost', $result->getCountry()->getName());
67+
$this->assertEmpty($result->getAdminLevels());
6768
}
6869

6970
public function testGeocodeWithLocalhostIPv6(): void
@@ -79,6 +80,7 @@ public function testGeocodeWithLocalhostIPv6(): void
7980
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
8081
$this->assertEquals('localhost', $result->getLocality());
8182
$this->assertEquals('localhost', $result->getCountry()->getName());
83+
$this->assertEmpty($result->getAdminLevels());
8284
}
8385

8486
public function testGeocodeWithRealIPv4(): void
@@ -96,6 +98,8 @@ public function testGeocodeWithRealIPv4(): void
9698
$this->assertEqualsWithDelta(-97.822, $result->getCoordinates()->getLongitude(), 0.01);
9799
$this->assertEquals('United States', $result->getCountry()->getName());
98100
$this->assertEquals('US', $result->getCountry()->getCode());
101+
$this->assertCount(1, $result->getAdminLevels());
102+
$this->assertEquals('New Jersey', $result->getAdminLevels()->get(1)->getName());
99103
}
100104

101105
public function testGeocodeWithRealIPv4InFrench(): void
@@ -113,6 +117,8 @@ public function testGeocodeWithRealIPv4InFrench(): void
113117
$this->assertEqualsWithDelta(-97.822, $result->getCoordinates()->getLongitude(), 0.01);
114118
$this->assertEquals('États-Unis', $result->getCountry()->getName());
115119
$this->assertEquals('US', $result->getCountry()->getCode());
120+
$this->assertCount(1, $result->getAdminLevels());
121+
$this->assertEquals('New Jersey', $result->getAdminLevels()->get(1)->getName());
116122
}
117123

118124
public function testReverse(): void

0 commit comments

Comments
 (0)