Skip to content

Commit

Permalink
Merge pull request #2 from CasperLaiTW/fix-regex-pattern-bug
Browse files Browse the repository at this point in the history
Fix regex pattern
  • Loading branch information
recca0120 authored Aug 23, 2016
2 parents 78dec73 + c2984a8 commit db7c0bb
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ public function toArray()
$at = null;
$shortAddress = null;

if (preg_match('/(?<zipcode>(^\d{5}|^\d{3})?)(?<county>\D{2}[縣市])?(?<district>\D+[鄉鎮市區])?(?<town>\D+[村里])?(?<lin>.+[鄰])?(?<road>\D+[路街大道])?(?<sec>.+[段])?(?<len>.+[巷])?(?<non>.+[弄])?(?<no>.+[號])?(?<floor>.+[樓Ff])?(?<at>[之-].+)?/u', $address, $matches) !== 0) {
if (preg_match('/(?<zipcode>(^\d{5}|^\d{3})?)(?<county>\D{2}[縣市])?(?<district>(\D{1,3}[^鄉鎮市區新](鄉|鎮|市|區)|\D{1,3}(鄉|鎮|市|區)))?(?<town>\D+[村里])?(?<lin>.+[鄰])?(?<road>\D+[路街大道])?(?<sec>.+[段])?(?<len>.+[巷])?(?<non>.+[弄])?(?<no>.+[號])?(?<floor>.+[樓Ff])?(?<at>[之-].+)?/u', $address, $matches) !== 0) {
$zipcode = $this->arrayGet($matches, 'zipcode');
$county = $this->arrayGet($matches, 'county');
$district = $this->arrayGet($matches, 'district');
Expand Down
72 changes: 72 additions & 0 deletions tests/NormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,76 @@ public function test_normailize_taitung()
'at' => null,
]);
}

public function test_normailize_taipei_datong_district_civic_blvd()
{
$normalizer = new Normalizer('台北市大同區市民大道一段209號5樓');
$this->assertSame($normalizer->toArray(), [
'county' => '臺北市',
'district' => '大同區',
'town' => '',
'lin' => '',
'road' => '市民大道',
'sec' => '一段',
'len' => '',
'non' => '',
'no' => '209號',
'floor' => '5樓',
'at' => null,
]);
}

public function test_normailize_district_only_two_words()
{
$normalizer = new Normalizer('新竹市東區西大路323號8樓');
$this->assertSame($normalizer->toArray(), [
'county' => '新竹市',
'district' => '東區',
'town' => '',
'lin' => '',
'road' => '西大路',
'sec' => '',
'len' => '',
'non' => '',
'no' => '323號',
'floor' => '8樓',
'at' => null,
]);
}

public function test_normailize_kaohsiung_qianzhen()
{
$normalizer = new Normalizer('高雄市前鎮區中華五路789號');
$this->assertSame($normalizer->toArray(), [
'county' => '高雄市',
'district' => '前鎮區',
'town' => '',
'lin' => '',
'road' => '中華五路',
'sec' => '',
'len' => '',
'non' => '',
'no' => '789號',
'floor' => null,
'at' => null,
]);
}

public function test_normailize_kinmen()
{
$normalizer = new Normalizer('金門縣金湖鎮新市里太湖路二段198號6樓');
$this->assertSame($normalizer->toArray(), [
'county' => '金門縣',
'district' => '金湖鎮',
'town' => '新市里',
'lin' => '',
'road' => '太湖路',
'sec' => '二段',
'len' => '',
'non' => '',
'no' => '198號',
'floor' => '6樓',
'at' => null,
]);
}
}
16 changes: 16 additions & 0 deletions tests/TwzipcodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,20 @@ public function test_taitung()
$this->assertSame($twzipcode->getAddress(true), '臺東縣臺東市中正路100號');
$this->assertSame($twzipcode->getShortAddress(true), '中正路100號');
}

public function test_taipei_datong_district_civic_blvd()
{
$twzipcode = new Twzipcode('台北市大同區市民大道一段209號5樓');
$this->assertSame($twzipcode->getZipcode(), '103');
$this->assertSame($twzipcode->getCounty(), '臺北市');
$this->assertSame($twzipcode->getDistrict(), '大同區');
$this->assertSame($twzipcode->getAddress(), '臺北市大同區市民大道一段209號5樓');
$this->assertSame($twzipcode->getShortAddress(), '市民大道一段209號5樓');

$this->assertSame($twzipcode->getZipcode(true), '103');
$this->assertSame($twzipcode->getCounty(true), '臺北市');
$this->assertSame($twzipcode->getDistrict(true), '大同區');
$this->assertSame($twzipcode->getAddress(true), '臺北市大同區市民大道一段209號5樓');
$this->assertSame($twzipcode->getShortAddress(true), '市民大道一段209號5樓');
}
}

0 comments on commit db7c0bb

Please sign in to comment.