From d7c4beaaffd6ddd466bd730567bd9a8a503e7527 Mon Sep 17 00:00:00 2001 From: recca0120 Date: Fri, 19 Aug 2022 18:36:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=AC=AC=E5=85=AD=E9=83=BD=E6=A1=83?= =?UTF-8?q?=E5=9C=92=E8=A2=AB=E5=BF=BD=E7=95=A5=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- src/Normalizer.php | 2 +- src/Zipcode.php | 33 ++++++++++++++------------------- tests/AddressTest.php | 7 +++---- tests/Moskytw/AddressTest.php | 11 ++++------- tests/Moskytw/RuleTest.php | 2 +- 6 files changed, 24 insertions(+), 33 deletions(-) diff --git a/.gitignore b/.gitignore index 547b524..a91aac1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -./idea +.idea /vendor /resources/assets/vendor /node_modules diff --git a/src/Normalizer.php b/src/Normalizer.php index eda1ecb..5ac0ee7 100644 --- a/src/Normalizer.php +++ b/src/Normalizer.php @@ -71,7 +71,7 @@ public function normalizeAddress() ]) ->replace('/^北縣/', '臺北縣') ->replace('/^北市/', '臺北市') - ->replace('/^(臺北|臺中|臺南|高雄)縣(?:(\w{2})[市鄉鎮])?(?:(\w{2})村)?/u', function ($m) { + ->replace('/^(桃園|臺北|臺中|臺南|高雄)縣(?:(\w{2})[市鄉鎮])?(?:(\w{2})村)?/u', function ($m) { return ($m[1] === '臺北' ? '新北' : $m[1]).'市'. (isset($m[2]) === true ? $m[2].'區' : ''). (isset($m[3]) === true ? $m[3].'里' : ''); diff --git a/src/Zipcode.php b/src/Zipcode.php index 29e390d..62d27b4 100644 --- a/src/Zipcode.php +++ b/src/Zipcode.php @@ -28,21 +28,16 @@ class Zipcode * __construct. * * @param string $address - * @param \Recca0120\Twzipcode\Rules $rules + * @param Rules $rules */ public function __construct($address, Rules $rules = null) { $rules = $rules ?: new Rules(); $this->address = new Address($address); - $zip = $rules->match($this->address); + $zip = (string) $rules->match($this->address); - if (strlen($zip) === 5) { - $this->attributes['zip3'] = substr($zip, 0, 3); - $this->attributes['zip5'] = $zip; - } else { - $this->attributes['zip3'] = $zip; - $this->attributes['zip5'] = $zip; - } + $this->attributes['zip3'] = strlen($zip) === 5 ? substr($zip, 0, 3) : $zip; + $this->attributes['zip5'] = $zip; $this->attributes['county'] = empty($this->attributes['zip3']) === false ? $this->address->flat(1, 0) @@ -59,6 +54,16 @@ public function __construct($address, Rules $rules = null) $this->attributes['address'] = $this->address->flat(); } + /** + * parse. + * + * @return static + */ + public static function parse($address, Rules $rules = null) + { + return new static($address, $rules); + } + /** * zip3. * @@ -118,14 +123,4 @@ public function shortAddress() { return $this->attributes['shortAddress']; } - - /** - * parse. - * - * @return static - */ - public static function parse($address, Rules $rules = null) - { - return new static($address, $rules); - } } diff --git a/tests/AddressTest.php b/tests/AddressTest.php index 3b7360d..10aad0a 100644 --- a/tests/AddressTest.php +++ b/tests/AddressTest.php @@ -2,10 +2,9 @@ namespace Recca0120\Twzipcode\Tests; -use Mockery as m; +use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; use PHPUnit\Framework\TestCase; use Recca0120\Twzipcode\Address; -use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; class AddressTest extends TestCase { @@ -52,8 +51,8 @@ public function testGetTokensWithTricky() $address = new Address('桃園縣中壢市普義10號'); $this->assertSame([ - ['', '', '桃園', '縣'], - ['', '', '中壢', '市'], + ['', '', '桃園', '市'], + ['', '', '中壢', '區'], ['', '', '普義', ''], ['10', '', '', '號'], ], (array) $address->tokens()); diff --git a/tests/Moskytw/AddressTest.php b/tests/Moskytw/AddressTest.php index 522e8fe..0870984 100644 --- a/tests/Moskytw/AddressTest.php +++ b/tests/Moskytw/AddressTest.php @@ -4,22 +4,19 @@ require __DIR__.'/stubs/Address.php'; -use Mockery as m; +use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; use Moskytw\Address; use PHPUnit\Framework\TestCase; -use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; class AddressTest extends TestCase { - use MockeryPHPUnitIntegration; - public function test_address_init() { $expected = [['', '', '臺北', '市'], ['', '', '大安', '區'], ['', '', '市府', '路'], ['1', '', '', '號']]; $this->assertSame($expected, (array) (new Address('臺北市大安區市府路1號'))->tokens()); } - public function test_address_init_subno() + public function test_address_init_sub_no() { $expected = [['', '', '臺北', '市'], ['', '', '大安', '區'], ['', '', '市府', '路'], ['1', '之1', '', '號']]; $this->assertSame($expected, (array) (new Address('臺北市大安區市府路1之1號'))->tokens()); @@ -27,10 +24,10 @@ public function test_address_init_subno() public function test_address_init_tricky_input() { - $expected = [['', '', '桃園', '縣'], ['', '', '中壢', '市'], ['', '', '普義', '']]; + $expected = [['', '', '桃園', '市'], ['', '', '中壢', '區'], ['', '', '普義', '']]; $this->assertSame($expected, (array) (new Address('桃園縣中壢市普義'))->tokens()); - $expected = [['', '', '桃園', '縣'], ['', '', '中壢', '市'], ['', '', '普義', ''], ['10', '', '', '號']]; + $expected = [['', '', '桃園', '市'], ['', '', '中壢', '區'], ['', '', '普義', ''], ['10', '', '', '號']]; $this->assertSame($expected, (array) (new Address('桃園縣中壢市普義10號'))->tokens()); $expected = [['', '', '臺北', '市'], ['', '', '中山', '區'], ['', '', '敬業1', '路']]; diff --git a/tests/Moskytw/RuleTest.php b/tests/Moskytw/RuleTest.php index 935acb9..35e46fc 100644 --- a/tests/Moskytw/RuleTest.php +++ b/tests/Moskytw/RuleTest.php @@ -53,7 +53,7 @@ public function test_rule_init() $this->assertSame(['雙', '至', '含附號全'], (array) $rule->ruleTokens()); $rule = new Rule('桃園縣,中壢市,普義,連 49號含附號以下'); - $this->assertSame([['', '', '桃園', '縣'], ['', '', '中壢', '市'], ['', '', '普義', ''], ['49', '', '', '號']], (array) $rule->tokens()); + $this->assertSame([['', '', '桃園', '市'], ['', '', '中壢', '區'], ['', '', '普義', ''], ['49', '', '', '號']], (array) $rule->tokens()); $this->assertSame(['含附號以下'], (array) $rule->ruleTokens()); $rule = new Rule('臺中市,西屯區,西屯路3段西平南巷,  1之 3號及以上附號');