Skip to content

Commit

Permalink
ADD: methods getProvinceCodeByName and getCitiesByProvinceName
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperjiang committed Aug 11, 2019
1 parent f36e14f commit 83d2da5
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ print_r($cities);
$city = '445200';
$counties = $divisionCode->getCountiesInCity($city);
print_r($counties);
// Get all the cities in the specified province by the province name
$province = '广东省';
$cities = $divisionCode->getCitiesByProvinceName($province);
print_r($cities);
```

## Upgrade
Expand Down
38 changes: 34 additions & 4 deletions src/DivisionCode/DivisionCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public function getSlice($offset = 0, $limit = 1): array
/**
* Get all the provinces including municipalities and autonomous regions
*
* @param bool $includeGAT include Hong Kong, Macao and Taiwan? exclude them by default
* @param bool $includeGAT include Hong Kong, Macao and Taiwan? exclude them by default
* @return array
*/
public function getAllProvinces($includeGAT = false): array
Expand All @@ -259,7 +259,7 @@ public function getAllProvinces($includeGAT = false): array

return array_filter(self::$codes, function ($k) use ($includeGAT) {
if (!$includeGAT) {
return substr($k, 2) == '0000' && $k < '710000' ;
return substr($k, 2) == '0000' && $k < '710000';
}

return substr($k, 2) == '0000';
Expand All @@ -270,7 +270,7 @@ public function getAllProvinces($includeGAT = false): array
* Get all the cities in the specified province
* Municipalities do not have cities so will return the counties(districts) instead
*
* @param string $province
* @param string $province province code
* @return array
*/
public function getCitiesInProvince($province): array
Expand Down Expand Up @@ -309,7 +309,7 @@ public function getCitiesInProvince($province): array
/**
* Get all the counties in the specified city
*
* @param string $city
* @param string $city city code
* @return array
*/
public function getCountiesInCity($city): array
Expand All @@ -336,4 +336,34 @@ public function getCountiesInCity($city): array
return substr($k, 0, 4) == $prefix && $k != $city;
}, ARRAY_FILTER_USE_KEY);
}

/**
* Get province code by its name
*
* @param string $name
* @return string
*/
public function getProvinceCodeByName($name): string
{
if ($this->supportSQLite()) {
$sql = "SELECT code FROM division_codes WHERE name = '$name'";

return (string) $this->db->querySingle($sql);
}

return array_search($name, self::$codes);
}

/**
* Get all the cities in the specified province by the province name
*
* @param string $provinceName
* @return array
*/
public function getCitiesByProvinceName($provinceName): array
{
$code = $this->getProvinceCodeByName($provinceName);

return $this->getCitiesInProvince($code);
}
}
Binary file modified src/DivisionCode/codes.db
Binary file not shown.
46 changes: 46 additions & 0 deletions tests/DivisionCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,50 @@ public function getCountiesInCityProvider(): array
[false, '110101', 0], // not a city
];
}

/**
* @dataProvider getProvinceCodeByNameProvider
*/
public function testGetProvinceCodeByName($useSQLite, $name, $expected): void
{
$this->divisionCode->useSQLite($useSQLite);
$res = $this->divisionCode->getProvinceCodeByName($name);
$this->assertEquals($expected, $res);
}

public function getProvinceCodeByNameProvider(): array
{
return [
[true, '北京市', '110000'],
[true, '', ''],
[true, '未知的地方', ''],
[false, '北京市', '110000'],
[false, '', ''],
[false, '未知的地方', ''],
];
}

/**
* @dataProvider getCitiesByProvinceNameProvider
*/
public function testgetCitiesByProvinceName($useSQLite, $province, $expected): void
{
$this->divisionCode->useSQLite($useSQLite);
$cities = $this->divisionCode->getCitiesByProvinceName($province);
$this->assertEquals($expected, count($cities));
}

public function getCitiesByProvinceNameProvider(): array
{
return [
[true, '北京市', 16],
[true, '台湾省', 0],
[true, '广东省', 21],
[true, '广州市', 0], // not a province
[false, '北京市', 16],
[false, '台湾省', 0],
[false, '广东省', 21],
[false, '广州市', 0], // not a province
];
}
}

0 comments on commit 83d2da5

Please sign in to comment.