Skip to content

Commit

Permalink
fix: 第六都桃園被忽略了
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Aug 19, 2022
1 parent 9ffc671 commit d7c4bea
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
./idea
.idea
/vendor
/resources/assets/vendor
/node_modules
Expand Down
2 changes: 1 addition & 1 deletion src/Normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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].'' : '');
Expand Down
33 changes: 14 additions & 19 deletions src/Zipcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
*
Expand Down Expand Up @@ -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);
}
}
7 changes: 3 additions & 4 deletions tests/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -52,8 +51,8 @@ public function testGetTokensWithTricky()
$address = new Address('桃園縣中壢市普義10號');

$this->assertSame([
['', '', '桃園', ''],
['', '', '中壢', ''],
['', '', '桃園', ''],
['', '', '中壢', ''],
['', '', '普義', ''],
['10', '', '', ''],
], (array) $address->tokens());
Expand Down
11 changes: 4 additions & 7 deletions tests/Moskytw/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,30 @@

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());
}

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', '']];
Expand Down
2 changes: 1 addition & 1 deletion tests/Moskytw/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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號及以上附號');
Expand Down

0 comments on commit d7c4bea

Please sign in to comment.