diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 8567aab..37f93ed 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -9,7 +9,7 @@ jobs: strategy: fail-fast: true matrix: - php: [ '7.1','7.2', '7.3', '7.4', '8.0', '8.1', '8.2'] + php: [ '7.0', '7.1','7.2', '7.3', '7.4', '8.0', '8.1', '8.2' ] stability: [ prefer-stable ] name: PHP ${{ matrix.php }} - ${{ matrix.stability }} diff --git a/composer.json b/composer.json index 8a27241..bf6420d 100644 --- a/composer.json +++ b/composer.json @@ -1,11 +1,13 @@ { "name": "recca0120/twzipcode", "description": "twzipcode", - "keywords": ["twzipcode"], + "keywords": [ + "twzipcode" + ], "license": "MIT", "type": "library", "require": { - "php": ">=5.5.9", + "php": ">=7.1", "ext-zlib": "*", "recca0120/lodash": "^1.1" }, diff --git a/resources/converter.php b/resources/converter.php index 542523b..c4fb615 100644 --- a/resources/converter.php +++ b/resources/converter.php @@ -20,11 +20,11 @@ $contents = preg_replace("/^\xEF\xBB\xBF/", '', $contents); $contents = trim(str_replace('Zip5,City,Area,Road,Scope', '', $contents)); - $zip = new ZipArchive(); + $zip = new ZipArchive; $zip->open($file, ZipArchive::OVERWRITE); $zip->addFromString(pathinfo($file, PATHINFO_FILENAME).'.csv', $contents); $zip->close(); } -(new File())->loadFile($file); +(new File)->loadFile($file); echo 'benchmark: '.(microtime(true) - $start)."\n"; diff --git a/src/Address.php b/src/Address.php index b4934dd..7720179 100644 --- a/src/Address.php +++ b/src/Address.php @@ -6,73 +6,38 @@ class Address { - /** - * NO. - * - * @var int - */ - const NO = 0; + public const NO = 0; - /** - * SUBNO. - * - * @var int - */ - const SUBNO = 1; + public const SUBNO = 1; - /** - * NAME. - * - * @var int - */ - const NAME = 2; + public const NAME = 2; - /** - * UNIT. - * - * @var int - */ - const UNIT = 3; + public const UNIT = 3; - /** - * $normalizer. - * - * @var Normalizer - */ + /** @var Normalizer */ public $normalizer; - /** - * @var Tricky - */ + /** @var Tricky */ public $tricky; - /** - * $tokens. - * - * @var JArray - */ + /** @var JArray */ public $tokens = []; /** - * __construct. - * * @param static|array $address */ public function __construct($address = '') { - $this->tricky = new Tricky(); + $this->tricky = new Tricky; if (empty($address) === false) { $this->set($address); } } /** - * set. - * * @param static|string $address - * @return $this */ - public function set($address) + public function set($address): self { $this->normalizer = (new Normalizer($address))->normalize()->normalizeAddress(); $this->tokens = $this->tokenize(); @@ -80,12 +45,7 @@ public function set($address) return $this; } - /** - * tokenize. - * - * @return array - */ - private function tokenize() + private function tokenize(): array { $units = [static::NO => 'no', static::SUBNO => 'subno', static::NAME => 'name', static::UNIT => 'unit']; @@ -109,33 +69,17 @@ private function tokenize() return $tokens; } - /** - * __toString. - * - * @return string - */ public function __toString() { return $this->normalizer->value(); } - /** - * tokens. - * - * @return JArray - */ - public function tokens() + public function tokens(): JArray { return new JArray($this->tokens); } - /** - * getPoint. - * - * @param string $index - * @return Point - */ - public function getPoint($index) + public function getPoint(string $index): Point { if (isset($this->tokens[$index]) === false) { return new Point(0, 0); @@ -148,21 +92,16 @@ public function getPoint($index) ); } - /** - * flat. - * - * @param int $length - * @param int $offset - * @return string - */ - public function flat($length = null, $offset = 0) + public function flat(?int $length = null, int $offset = 0): string { $tokens = $this->tokens(); $length = $length ?: $tokens->length(); $end = $offset + $length; - return (string) $tokens->slice($offset, $end)->map(function ($token) { - return implode('', $token); - })->join(''); + return (string) $tokens->slice($offset, $end) + ->map(function ($token) { + return implode('', $token); + }) + ->join(''); } } diff --git a/src/Contracts/Storage.php b/src/Contracts/Storage.php index a2e3c4d..cab2208 100644 --- a/src/Contracts/Storage.php +++ b/src/Contracts/Storage.php @@ -6,41 +6,13 @@ interface Storage { - /** - * zip3. - * - * @return string - */ - public function zip3(Address $address); + public function zip3(Address $address): ?string; - /** - * rules. - * - * @param string $zip3 - * @return JString - */ - public function rules($zip3); + public function rules(string $zip3): array; - /** - * load. - * - * @param string $source - * @return $this - */ - public function load($source); + public function load(string $source): self; - /** - * loadFile. - * - * @param string $file - * @return $this - */ - public function loadFile($file = null); + public function loadFile(?string $file = null): self; - /** - * flush. - * - * @return $this - */ - public function flush(); + public function flush(): self; } diff --git a/src/Normalizer.php b/src/Normalizer.php index 9fdcd19..5b03931 100644 --- a/src/Normalizer.php +++ b/src/Normalizer.php @@ -6,12 +6,7 @@ class Normalizer extends JString { - /** - * normalizeAddress. - * - * @return static - */ - public function normalizeAddress() + public function normalizeAddress(): self { return $this ->replace([ @@ -48,34 +43,19 @@ public function normalizeAddress() ]); } - /** - * normalize. - * - * @return static - */ - public function normalize() + public function normalize(): self { return $this->trim()->regularize()->digitize(); } - /** - * digitize. - * - * @return static - */ - public function digitize() + public function digitize(): self { return $this->replace('/[一二三四五六七八九十百千]+(?=[段路街巷弄號樓])/u', function ($m) { return (new static($m[0]))->chineseToNumber(); }); } - /** - * regularize. - * - * @return static - */ - public function regularize() + public function regularize(): self { return $this ->toHalfCase() diff --git a/src/Point.php b/src/Point.php index 349e68a..c6c3573 100644 --- a/src/Point.php +++ b/src/Point.php @@ -4,49 +4,22 @@ class Point { - /** - * $x. - * - * @var int - */ public $x = 0; - /** - * $y. - * - * @var int - */ public $y = 0; - /** - * __construct. - * - * @param int $x - * @param int $y - */ - public function __construct($x = 0, $y = 0) + public function __construct(int $x = 0, int $y = 0) { $this->x = $x; $this->y = $y; } - /** - * empty. - * - * @return bool - */ - public function isEmpty() + public function isEmpty(): bool { return $this->x === 0 && $this->y === 0; } - /** - * compare. - * - * @param string $operator - * @return bool - */ - public function compare(self $point, $operator = '=') + public function compare(self $point, string $operator = '='): bool { $sum = $this->x * 10 + $this->y; $sum2 = $point->x * 10 + $point->y; diff --git a/src/Rule.php b/src/Rule.php index 39b42f5..9582147 100644 --- a/src/Rule.php +++ b/src/Rule.php @@ -7,40 +7,19 @@ class Rule { - /** - * $zip3. - * - * @var string - */ - public $zip3; + /** @var string */ + private $zip3; - /** - * $zip5. - * - * @var string - */ - public $zip5; + /** @var string */ + private $zip5; - /** - * $address. - * - * @var Address - */ - public $address; + /** @var Address */ + private $address; - /** - * $tokens. - * - * @var array - */ - public $tokens; + /** @var array */ + private $tokens; - /** - * __construct. - * - * @param string $rule - */ - public function __construct($rule) + public function __construct(string $rule) { if (preg_match('/^(\d+),?(.*)/', $rule, $m)) { $this->zip5 = $m[1]; @@ -53,43 +32,25 @@ public function __construct($rule) }); } - /** - * zip5. - * - * @return string - */ - public function zip5() + public function zip5(): string { return $this->zip5; } - /** - * zip. - * - * @return string - */ - public function zip() + public function zip(): string { return $this->zip3(); } - /** - * zip3. - * - * @return string - */ - public function zip3() + public function zip3(): string { return $this->zip3; } /** - * match. - * * @param Address|string $address - * @return bool */ - public function match($address) + public function match($address): bool { $ruleAddressTokens = $this->address->tokens(); $address = $this->normalizeAddress( @@ -123,16 +84,16 @@ public function match($address) ($token === '以上' && $addressPoint->compare($left, '>=') === false) || ($token === '以下' && $addressPoint->compare($left, '<=') === false) || ($token === '至' && ( - ($right->compare($addressPoint, '<=') && $addressPoint->compare($left, '<=')) || - ($currentTokens->includes('含附號全') === true && ($addressPoint->x === $left->x)) - ) === false) || + ($right->compare($addressPoint, '<=') && $addressPoint->compare($left, '<=')) || + ($currentTokens->includes('含附號全') === true && ($addressPoint->x === $left->x)) + ) === false) || ($token === '含附號' && ($addressPoint->x === $left->x) === false) || ($token === '附號全' && ($addressPoint->x === $left->x && $addressPoint->y > 0) === false) || ($token === '及以上附號' && $addressPoint->compare($left, '>=') === false) || ($token === '含附號以下' && ( - $addressPoint->compare($left, '<=') || - $addressPoint->x === $left->x - ) === false) + $addressPoint->compare($left, '<=') || + $addressPoint->x === $left->x + ) === false) ) { return false; } @@ -141,23 +102,17 @@ public function match($address) return true; } - /** - * tokens. - * - * @return JArray - */ - public function tokens() + public function tokens(): JArray { return new JArray($this->tokens); } - /** - * tokenize. - * - * @param string $rule - * @return array - */ - private function tokenize($rule, Closure $addressResolver) + public function address(): Address + { + return $this->address; + } + + private function tokenize(string $rule, Closure $addressResolver): array { $tokens = []; @@ -168,27 +123,22 @@ private function tokenize($rule, Closure $addressResolver) '[連至單雙全](?=[\d全]|$)', ]; - $addressResolver($this->normalize($rule)->replace('/'.implode('|', $pattern).'/u', function ($m) use (&$tokens) { - $token = &$m[0]; - if ($token === '連') { - return ''; - } + $addressResolver($this->normalize($rule)->replace('/'.implode('|', $pattern).'/u', + function ($m) use (&$tokens) { + $token = &$m[0]; + if ($token === '連') { + return ''; + } - $tokens[] = $token; + $tokens[] = $token; - return $token === '附號全' ? '號' : ''; - })); + return $token === '附號全' ? '號' : ''; + })); return $tokens; } - /** - * normalize. - * - * @param string $rule - * @return Normalizer - */ - private function normalize($rule) + private function normalize(string $rule): Normalizer { $pattern = '((?P\d+)之)?\s*(?P\d+)至之?\s*(?P\d+)(?P\w)'; @@ -207,12 +157,7 @@ private function normalize($rule) }); } - /** - * normalizeAddress. - * - * @return Address - */ - private function normalizeAddress(Address $address, JArray $ruleAddressTokens) + private function normalizeAddress(Address $address, JArray $ruleAddressTokens): Address { $removeUnits = array_diff(['里', '鄰', '巷', '弄'], (array) $ruleAddressTokens->map(function ($token) { return isset($token[Address::UNIT]) === true ? $token[Address::UNIT] : ''; @@ -220,22 +165,15 @@ private function normalizeAddress(Address $address, JArray $ruleAddressTokens) return new Address( new JArray($address->tokens()->filter(function ($token) use ($removeUnits) { - return isset($token[Address::UNIT]) === true && in_array($token[Address::UNIT], $removeUnits, true) === false; + return isset($token[Address::UNIT]) === true && + in_array($token[Address::UNIT], $removeUnits, true) === false; })->map(function ($token) { return implode('', $token); })) ); } - /** - * equalsToken. - * - * @param JArray $ruleAddressTokens - * @param JArray $addressTokens - * @param int $cur - * @return bool - */ - private function equalsToken($ruleAddressTokens, $addressTokens, $cur) + private function equalsToken(JArray $ruleAddressTokens, JArray $addressTokens, int $cur): bool { if ($cur >= $addressTokens->length()) { return false; diff --git a/src/Rules.php b/src/Rules.php index c388146..480c56c 100644 --- a/src/Rules.php +++ b/src/Rules.php @@ -2,26 +2,18 @@ namespace Recca0120\Twzipcode; +use Recca0120\Lodash\JArray; use Recca0120\Twzipcode\Contracts\Storage; use Recca0120\Twzipcode\Storages\File; class Rules { - /** - * $storage. - * - * @var Storage - */ + /** @var Storage */ private $storage; - /** - * __construct. - * - * @param Storage $storage - */ - public function __construct(Storage $storage = null) + public function __construct(?Storage $storage = null) { - $this->storage = $storage ?: new File(); + $this->storage = $storage ?: new File; } /** @@ -39,7 +31,7 @@ public function match($address) return; } - $rule = $this->storage->rules($zip3)->find(function ($rule) use ($address) { + $rule = (new JArray($this->storage->rules($zip3)))->find(function ($rule) use ($address) { return $rule->match($address); }); diff --git a/src/Storages/File.php b/src/Storages/File.php index 63e3c31..4270b91 100644 --- a/src/Storages/File.php +++ b/src/Storages/File.php @@ -11,84 +11,52 @@ class File implements Storage { - /** - * cached. - * - * @var array - */ - public static $cached = [ - 'zip3' => null, - 'zip5' => null, - ]; + public static $cached = ['zip3' => null, 'zip5' => null]; - /** - * $path. - * - * @var string - */ + /** @var string */ public $path; - /** - * $suffix. - * - * @var string - */ public $suffix = '.rules'; - /** - * __construct. - * - * @param string $path - */ - public function __construct($path = null) + public function __construct(?string $path = null) { - $this->path = ($path ?: dirname(dirname(__DIR__)).'/resources/data').'/'; + $this->path = ($path ?: dirname(__DIR__, 2).'/resources/data').'/'; } - /** - * zip3. - * - * @return JString - */ - public function zip3(Address $address) + public function zip3(Address $address): ?string { $this->restore('zip3'); foreach ([2, 1] as $value) { $flat = $address->flat($value); if (isset(self::$cached['zip3'][$flat])) { - $zip3 = self::$cached['zip3'][$flat]; - - return $zip3; + return self::$cached['zip3'][$flat]; } } return null; } - /** - * load. - * - * @param string $source - * @return $this - */ - public function load($source) + public function load(string $source): Storage { $zip5 = []; $zip3 = []; - $this->each($this->prepareSource($source), function ($zipcode, $county, $district, $rules) use (&$zip5, &$zip3) { - $zip5[$zipcode] = $this->compress(array_map(function ($rule) { - return new Rule($rule); - }, $rules)); - - if (isset($zip3[$county]) === false) { - $zip3[$county] = substr($zipcode, 0, 1); - } + $this->each($this->prepareSource($source), + function ($zipcode, $county, $district, $rules) use (&$zip5, &$zip3) { + $zip5[$zipcode] = $this->compress( + array_map(static function ($rule) { + return new Rule($rule); + }, $rules) + ); + + if (isset($zip3[$county]) === false) { + $zip3[$county] = substr($zipcode, 0, 1); + } - if (isset($zip3[$county.$district]) === false) { - $zip3[$county.$district] = substr($zipcode, 0, 3); - } - }); + if (isset($zip3[$county.$district]) === false) { + $zip3[$county.$district] = substr($zipcode, 0, 3); + } + }); $this->store('zip3', $zip3); $this->store('zip5', $zip5); @@ -96,28 +64,16 @@ public function load($source) return $this; } - /** - * rules. - * - * @param string $zip3 - * @return JArray - */ - public function rules($zip3) + public function rules(string $zip3): array { $this->restore('zip5'); return isset(self::$cached['zip5'][$zip3]) === true - ? new JArray($this->decompress(self::$cached['zip5'][$zip3])) - : new JArray([]); + ? $this->decompress(self::$cached['zip5'][$zip3]) + : []; } - /** - * loadFile. - * - * @param string $file - * @return $this - */ - public function loadFile($file = null) + public function loadFile(?string $file = null): Storage { $file = $file ?: $this->path.'../Zip32_utf8_10501_1.csv'; $this->load($this->getSource($file)); @@ -125,49 +81,29 @@ public function loadFile($file = null) return $this; } - /** - * flush. - * - * @return $this - */ - public function flush() + public function flush(): Storage { - static::$cached = [ - 'zip3' => null, - 'zip5' => null, - ]; + static::$cached = ['zip3' => null, 'zip5' => null]; return $this; } - /** - * restore. - * - * @param string $filename - * @return mixed - */ - private function restore($filename) + private function restore(string $filename): void { if (self::$cached[$filename] !== null) { - return self::$cached[$filename]; + return; } if (file_exists($this->path.$filename.$this->suffix) === false) { - return false; + return; } - return self::$cached[$filename] = new JArray($this->decompress( + self::$cached[$filename] = new JArray($this->decompress( file_get_contents($this->path.$filename.$this->suffix) )); } - /** - * getSource. - * - * @param string $file - * @return string - */ - private function getSource($file) + private function getSource(string $file): string { $extension = pathinfo($file, PATHINFO_EXTENSION); @@ -211,13 +147,7 @@ private function prepareSource($source) return $results; } - /** - * each. - * - * @param array $ruleGroup - * @param Closure $callback - */ - private function each($ruleGroup, $callback) + private function each(array $ruleGroup, Closure $callback): void { foreach ($ruleGroup as $county => $districts) { foreach ($districts as $district => $addresses) { @@ -230,40 +160,22 @@ private function each($ruleGroup, $callback) /** * compress. - * - * @param array $array - * @return string */ - private function compress($array) + private function compress(array $array): string { return gzcompress(serialize($array)); } /** * decompress. - * - * @param string $compressed - * @return array */ - private function decompress($compressed) + private function decompress(string $compressed): array { - return unserialize(gzuncompress($compressed)); + return unserialize(gzuncompress($compressed), ['max_depth' => 0]); } - /** - * store. - * - * @param string $filename - * @param JArray $data - * @return $this - */ - private function store($filename, $data) + private function store(string $filename, array $data): void { - file_put_contents( - $this->path.$filename.$this->suffix, - $this->compress($data) - ); - - return $this; + file_put_contents($this->path.$filename.$this->suffix, $this->compress($data)); } } diff --git a/src/Tricky.php b/src/Tricky.php index 59418eb..a817794 100644 --- a/src/Tricky.php +++ b/src/Tricky.php @@ -42,10 +42,7 @@ public function __construct() } } - /** - * @return void - */ - private function init() + private function init(): void { $tricky = ['島', '嶼', '鄉', '市', '鎮', '區', '村', '里', '路', '新市', '阿里山', '鎮興里平']; self::$cached['hash'] = array_reduce($tricky, static function ($acc, $unit) { @@ -71,20 +68,12 @@ private function init() ]; } - /** - * @param Normalizer $normalizer - * @return Normalizer - */ - public function hash($normalizer) + public function hash(Normalizer $normalizer): Normalizer { return $normalizer->replace(self::$cached['replace']); } - /** - * @param string $token - * @return string - */ - public function flip($token) + public function flip(string $token): string { return strtr($token, self::$cached['flip']); } diff --git a/src/Zipcode.php b/src/Zipcode.php index 40090c4..29a7ccc 100644 --- a/src/Zipcode.php +++ b/src/Zipcode.php @@ -18,22 +18,15 @@ class Zipcode 'shortAddress' => null, ]; - /** - * $address. - * - * @var Address - */ + /** @var Address */ private $address; /** * __construct. - * - * @param string $address - * @param Rules $rules */ - public function __construct($address, Rules $rules = null) + public function __construct(string $address, ?Rules $rules = null) { - $rules = $rules ?: new Rules(); + $rules = $rules ?: new Rules; $this->address = new Address($address); $zip = (string) $rules->match($this->address); @@ -55,72 +48,37 @@ public function __construct($address, Rules $rules = null) $this->attributes['address'] = $this->address->flat(); } - /** - * parse. - * - * @return static - */ - public static function parse($address, Rules $rules = null) + public static function parse($address, ?Rules $rules = null): self { return new static($address, $rules); } - /** - * zip3. - * - * @return string - */ - public function zip3() + public function zip3(): ?string { return $this->attributes['zip3']; } - /** - * zip5. - * - * @return string - */ - public function zip5() + public function zip5(): ?string { return $this->attributes['zip5']; } - /** - * county. - * - * @return string - */ - public function county() + public function county(): ?string { return $this->attributes['county']; } - /** - * district. - * - * @return string - */ - public function district() + public function district(): ?string { return $this->attributes['district']; } - /** - * address. - * - * @return string - */ - public function address() + public function address(): ?string { return $this->attributes['address']; } - /** - * shortAddress. - * - * @return string - */ - public function shortAddress() + public function shortAddress(): ?string { return $this->attributes['shortAddress']; } diff --git a/tests/Moskytw/AddressTest.php b/tests/Moskytw/AddressTest.php index a5941aa..79dad0e 100644 --- a/tests/Moskytw/AddressTest.php +++ b/tests/Moskytw/AddressTest.php @@ -9,19 +9,19 @@ class AddressTest extends TestCase { - public function test_address_init() + public function test_address_init(): void { $expected = [['', '', '臺北', '市'], ['', '', '大安', '區'], ['', '', '市府', '路'], ['1', '', '', '號']]; $this->assertSame($expected, (array) (new Address('臺北市大安區市府路1號'))->tokens()); } - public function test_address_init_sub_no() + public function test_address_init_sub_no(): void { $expected = [['', '', '臺北', '市'], ['', '', '大安', '區'], ['', '', '市府', '路'], ['1', '之1', '', '號']]; $this->assertSame($expected, (array) (new Address('臺北市大安區市府路1之1號'))->tokens()); } - public function test_address_init_tricky_input() + public function test_address_init_tricky_input(): void { $expected = [['', '', '桃園', '市'], ['', '', '中壢', '區'], ['', '', '普義', '']]; $this->assertSame($expected, (array) (new Address('桃園縣中壢市普義'))->tokens()); @@ -36,7 +36,7 @@ public function test_address_init_tricky_input() $this->assertSame($expected, (array) (new Address('臺北市中山區敬業1路10號'))->tokens()); } - public function test_address_init_normalization() + public function test_address_init_normalization(): void { $expected = [['', '', '臺北', '市'], ['', '', '大安', '區'], ['', '', '市府', '路'], ['1', '之1', '', '號']]; @@ -49,7 +49,7 @@ public function test_address_init_normalization() $this->assertSame($expected, (array) (new Address('臺北市, 大安區, 市府路 1 - 1 號'))->tokens()); } - public function test_address_init_normalization_chinese_number() + public function test_address_init_normalization_chinese_number(): void { $this->assertSame('八德路', (new Address('八德路'))->normalize()); $this->assertSame('三元街', (new Address('三元街'))->normalize()); @@ -67,7 +67,7 @@ public function test_address_init_normalization_chinese_number() $this->assertSame('愛富3街', (new Address('愛富三街'))->normalize()); } - public function test_address_flat() + public function test_address_flat(): void { $address = new Address('臺北市大安區市府路1之1號'); $this->assertSame('臺北市', $address->flat(1)); diff --git a/tests/Moskytw/DirectoryTest.php b/tests/Moskytw/DirectoryTest.php index 6f2280f..be5afcf 100644 --- a/tests/Moskytw/DirectoryTest.php +++ b/tests/Moskytw/DirectoryTest.php @@ -85,7 +85,7 @@ protected function setUp(): void '); } - public function test_find() + public function test_find(): void { // 10043,臺北市,中正區,中華路1段,單 25之 3號以下 $this->assertSame('10043', $this->directory->find('臺北市中正區中華路1段25號')); @@ -119,7 +119,7 @@ public function test_find() $this->assertSame('10042', $this->directory->find('臺北市中正區中華路1段53號')); } - public function test_find_gradually() + public function test_find_gradually(): void { $this->assertSame('1', $this->directory->find('臺北市')); $this->assertSame('100', $this->directory->find('臺北市中正區')); diff --git a/tests/Moskytw/RuleTest.php b/tests/Moskytw/RuleTest.php index 35e46fc..98082c3 100644 --- a/tests/Moskytw/RuleTest.php +++ b/tests/Moskytw/RuleTest.php @@ -13,62 +13,92 @@ class RuleTest extends TestCase public function test_rule_init() { $rule = new Rule('臺北市,中正區,八德路1段,全'); - $this->assertSame([['', '', '臺北', '市'], ['', '', '中正', '區'], ['', '', '八德', '路'], ['', '', '1', '段']], (array) $rule->tokens()); + $this->assertSame([['', '', '臺北', '市'], ['', '', '中正', '區'], ['', '', '八德', '路'], ['', '', '1', '段']], + (array) $rule->tokens()); $this->assertSame(['全'], (array) $rule->ruleTokens()); $rule = new Rule('臺北市,中正區,三元街,單全'); - $this->assertSame([['', '', '臺北', '市'], ['', '', '中正', '區'], ['', '', '三元', '街']], (array) $rule->tokens()); + $this->assertSame([['', '', '臺北', '市'], ['', '', '中正', '區'], ['', '', '三元', '街']], + (array) $rule->tokens()); $this->assertSame(['單', '全'], (array) $rule->ruleTokens()); $rule = new Rule('臺北市,中正區,三元街,雙 48號以下'); - $this->assertSame([['', '', '臺北', '市'], ['', '', '中正', '區'], ['', '', '三元', '街'], ['48', '', '', '號']], (array) $rule->tokens()); + $this->assertSame([ + ['', '', '臺北', '市'], ['', '', '中正', '區'], ['', '', '三元', '街'], ['48', '', '', '號'], + ], (array) $rule->tokens()); $this->assertSame(['雙', '以下'], (array) $rule->ruleTokens()); $rule = new Rule('臺北市,中正區,大埔街,單 15號以上'); - $this->assertSame([['', '', '臺北', '市'], ['', '', '中正', '區'], ['', '', '大埔', '街'], ['15', '', '', '號']], (array) $rule->tokens()); + $this->assertSame([ + ['', '', '臺北', '市'], ['', '', '中正', '區'], ['', '', '大埔', '街'], ['15', '', '', '號'], + ], (array) $rule->tokens()); $this->assertSame(['單', '以上'], (array) $rule->ruleTokens()); $rule = new Rule('臺北市,中正區,中華路1段,單 25之 3號以下'); - $this->assertSame([['', '', '臺北', '市'], ['', '', '中正', '區'], ['', '', '中華', '路'], ['', '', '1', '段'], ['25', '之3', '', '號']], (array) $rule->tokens()); + $this->assertSame([ + ['', '', '臺北', '市'], ['', '', '中正', '區'], ['', '', '中華', '路'], ['', '', '1', '段'], + ['25', '之3', '', '號'], + ], (array) $rule->tokens()); $this->assertSame(['單', '以下'], (array) $rule->ruleTokens()); $rule = new Rule('臺北市,中正區,中華路1段,單 27號至 47號'); - $this->assertSame([['', '', '臺北', '市'], ['', '', '中正', '區'], ['', '', '中華', '路'], ['', '', '1', '段'], ['27', '', '', '號'], ['47', '', '', '號']], (array) $rule->tokens()); + $this->assertSame([ + ['', '', '臺北', '市'], ['', '', '中正', '區'], ['', '', '中華', '路'], ['', '', '1', '段'], + ['27', '', '', '號'], ['47', '', '', '號'], + ], (array) $rule->tokens()); $this->assertSame(['單', '至'], (array) $rule->ruleTokens()); $rule = new Rule('臺北市,中正區,仁愛路1段,連 2之 4號以上'); - $this->assertSame([['', '', '臺北', '市'], ['', '', '中正', '區'], ['', '', '仁愛', '路'], ['', '', '1', '段'], ['2', '之4', '', '號']], (array) $rule->tokens()); + $this->assertSame([ + ['', '', '臺北', '市'], ['', '', '中正', '區'], ['', '', '仁愛', '路'], ['', '', '1', '段'], + ['2', '之4', '', '號'], + ], (array) $rule->tokens()); $this->assertSame(['以上'], (array) $rule->ruleTokens()); $rule = new Rule('臺北市,中正區,杭州南路1段,  14號含附號'); - $this->assertSame([['', '', '臺北', '市'], ['', '', '中正', '區'], ['', '', '杭州南', '路'], ['', '', '1', '段'], ['14', '', '', '號']], (array) $rule->tokens()); + $this->assertSame([ + ['', '', '臺北', '市'], ['', '', '中正', '區'], ['', '', '杭州南', '路'], ['', '', '1', '段'], + ['14', '', '', '號'], + ], (array) $rule->tokens()); $this->assertSame(['含附號'], (array) $rule->ruleTokens()); $rule = new Rule('臺北市,大同區,哈密街,  47附號全'); - $this->assertSame([['', '', '臺北', '市'], ['', '', '大同', '區'], ['', '', '哈密', '街'], ['47', '', '', '號']], (array) $rule->tokens()); + $this->assertSame([ + ['', '', '臺北', '市'], ['', '', '大同', '區'], ['', '', '哈密', '街'], ['47', '', '', '號'], + ], (array) $rule->tokens()); $this->assertSame(['附號全'], (array) $rule->ruleTokens()); $rule = new Rule('臺北市,大同區,哈密街,雙 68巷至 70號含附號全'); - $this->assertSame([['', '', '臺北', '市'], ['', '', '大同', '區'], ['', '', '哈密', '街'], ['68', '', '', '巷'], ['70', '', '', '號']], (array) $rule->tokens()); + $this->assertSame([ + ['', '', '臺北', '市'], ['', '', '大同', '區'], ['', '', '哈密', '街'], ['68', '', '', '巷'], + ['70', '', '', '號'], + ], (array) $rule->tokens()); $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號及以上附號'); - $this->assertSame([['', '', '臺中', '市'], ['', '', '西屯', '區'], ['', '', '西屯', '路'], ['', '', '3', '段'], ['', '', '西平南', '巷'], ['1', '之3', '', '號']], (array) $rule->tokens()); + $this->assertSame([ + ['', '', '臺中', '市'], ['', '', '西屯', '區'], ['', '', '西屯', '路'], ['', '', '3', '段'], + ['', '', '西平南', '巷'], ['1', '之3', '', '號'], + ], (array) $rule->tokens()); $this->assertSame(['及以上附號'], (array) $rule->ruleTokens()); } - public function test_rule_init_tricky_input() + public function test_rule_init_tricky_input(): void { $rule = new Rule('新北市,中和區,連城路,雙 268之 1號以下'); - $this->assertSame([['', '', '新北', '市'], ['', '', '中和', '區'], ['', '', '連城', '路'], ['268', '之1', '', '號']], (array) $rule->tokens()); + $this->assertSame([ + ['', '', '新北', '市'], ['', '', '中和', '區'], ['', '', '連城', '路'], ['268', '之1', '', '號'], + ], (array) $rule->tokens()); $this->assertSame(['雙', '以下'], (array) $rule->ruleTokens()); $rule = new Rule('新北市,泰山區,全興路,全'); - $this->assertSame([['', '', '新北', '市'], ['', '', '泰山', '區'], ['', '', '全興', '路']], (array) $rule->tokens()); + $this->assertSame([['', '', '新北', '市'], ['', '', '泰山', '區'], ['', '', '全興', '路']], + (array) $rule->tokens()); $this->assertSame(['全'], (array) $rule->ruleTokens()); } diff --git a/tests/Moskytw/stubs/Address.php b/tests/Moskytw/stubs/Address.php index 2dc55e8..654d36f 100644 --- a/tests/Moskytw/stubs/Address.php +++ b/tests/Moskytw/stubs/Address.php @@ -34,7 +34,7 @@ public function flat($length = null) $length = count($this->tokens) + $length; } - return implode('', array_map(function ($token) { + return implode('', array_map(static function ($token) { return implode('', $token); }, array_slice((array) $this->tokens, 0, $length))); } @@ -44,7 +44,7 @@ public function tokens() return $this->tokens; } - protected function getTokenPoint($index) + protected function getTokenPoint($index): array { $point = $this->address->getPoint($index); diff --git a/tests/Moskytw/stubs/Directory.php b/tests/Moskytw/stubs/Directory.php index 176f7de..a406a1a 100644 --- a/tests/Moskytw/stubs/Directory.php +++ b/tests/Moskytw/stubs/Directory.php @@ -8,6 +8,7 @@ class Directory { private $storage; + private $rules; public function __construct($root) @@ -16,19 +17,19 @@ public function __construct($root) $this->rules = new Rules($this->storage); } - public function __call($method, $argments) + public function __call($method, $arguments) { - return call_user_func_array([$this->rules, $method], $argments); + return call_user_func_array([$this->rules, $method], $arguments); } - public function load($source) + public function load($source): Directory { $this->storage->flush()->load($source); return $this; } - public function find($address) + public function find($address): ?string { return $this->rules->match($address); } diff --git a/tests/Moskytw/stubs/Rule.php b/tests/Moskytw/stubs/Rule.php index 48237c3..42e08a3 100644 --- a/tests/Moskytw/stubs/Rule.php +++ b/tests/Moskytw/stubs/Rule.php @@ -2,6 +2,7 @@ namespace Moskytw; +use Recca0120\Lodash\JArray; use Recca0120\Twzipcode\Rule as BaseRule; class Rule @@ -13,17 +14,17 @@ public function __construct($rule) $this->rule = new BaseRule($rule); } - public function ruleTokens() + public function ruleTokens(): JArray { return $this->rule->tokens(); } - public function tokens() + public function tokens(): JArray { - return $this->rule->address->tokens(); + return $this->rule->address()->tokens(); } - public function match(Address $address) + public function match(Address $address): bool { return $this->rule->match((string) $address); } diff --git a/tests/RuleTest.php b/tests/RuleTest.php index 86120f5..3ca0c38 100644 --- a/tests/RuleTest.php +++ b/tests/RuleTest.php @@ -18,7 +18,7 @@ public function testGetTokensWithAll(): void ['', '', '中正', '區'], ['', '', '八德', '路'], ['', '', '1', '段'], - ], (array) $rule->address->tokens()); + ], (array) $rule->address()->tokens()); } public function testGetTokensWithSingleAll(): void @@ -31,7 +31,7 @@ public function testGetTokensWithSingleAll(): void ['', '', '臺北', '市'], ['', '', '中正', '區'], ['', '', '三元', '街'], - ], (array) $rule->address->tokens()); + ], (array) $rule->address()->tokens()); } public function testGetTokensWithDouble48Below(): void @@ -45,7 +45,7 @@ public function testGetTokensWithDouble48Below(): void ['', '', '中正', '區'], ['', '', '三元', '街'], ['48', '', '', '號'], - ], (array) $rule->address->tokens()); + ], (array) $rule->address()->tokens()); } public function testGetTokensWithSingle15Above(): void @@ -59,7 +59,7 @@ public function testGetTokensWithSingle15Above(): void ['', '', '中正', '區'], ['', '', '大埔', '街'], ['15', '', '', '號'], - ], (array) $rule->address->tokens()); + ], (array) $rule->address()->tokens()); } public function testGetTokensWithSingle253Below(): void @@ -74,7 +74,7 @@ public function testGetTokensWithSingle253Below(): void ['', '', '中華', '路'], ['', '', '1', '段'], ['25', '之3', '', '號'], - ], (array) $rule->address->tokens()); + ], (array) $rule->address()->tokens()); } public function testGetTokensWithSingle27To47(): void @@ -90,7 +90,7 @@ public function testGetTokensWithSingle27To47(): void ['', '', '1', '段'], ['27', '', '', '號'], ['47', '', '', '號'], - ], (array) $rule->address->tokens()); + ], (array) $rule->address()->tokens()); } public function testGetTokensWithAnd24Above(): void @@ -105,7 +105,7 @@ public function testGetTokensWithAnd24Above(): void ['', '', '仁愛', '路'], ['', '', '1', '段'], ['2', '之4', '', '號'], - ], (array) $rule->address->tokens()); + ], (array) $rule->address()->tokens()); } public function testGetTokensWith14SubNo(): void @@ -120,7 +120,7 @@ public function testGetTokensWith14SubNo(): void ['', '', '杭州南', '路'], ['', '', '1', '段'], ['14', '', '', '號'], - ], (array) $rule->address->tokens()); + ], (array) $rule->address()->tokens()); } public function testGetTokensWith47SubNoAll(): void @@ -134,7 +134,7 @@ public function testGetTokensWith47SubNoAll(): void ['', '', '大同', '區'], ['', '', '哈密', '街'], ['47', '', '', '號'], - ], (array) $rule->address->tokens()); + ], (array) $rule->address()->tokens()); } public function testGetTokensWithDouble68Alley70SubNoAll(): void @@ -149,7 +149,7 @@ public function testGetTokensWithDouble68Alley70SubNoAll(): void ['', '', '哈密', '街'], ['68', '', '', '巷'], ['70', '', '', '號'], - ], (array) $rule->address->tokens()); + ], (array) $rule->address()->tokens()); } public function testGetTokensWith49AndSubnoBelow() @@ -163,7 +163,7 @@ public function testGetTokensWith49AndSubnoBelow() ['', '', '中壢', '區'], ['', '', '普義', ''], ['49', '', '', '號'], - ], (array) $rule->address->tokens()); + ], (array) $rule->address()->tokens()); } public function testGetTokensWith13AndSubNoAbove(): void @@ -179,7 +179,7 @@ public function testGetTokensWith13AndSubNoAbove(): void ['', '', '3', '段'], ['', '', '西平南', '巷'], ['1', '之3', '', '號'], - ], (array) $rule->address->tokens()); + ], (array) $rule->address()->tokens()); } public function testGetTokensWithTrickyDouble2681Below(): void @@ -193,7 +193,7 @@ public function testGetTokensWithTrickyDouble2681Below(): void ['', '', '中和', '區'], ['', '', '連城', '路'], ['268', '之1', '', '號'], - ], (array) $rule->address->tokens()); + ], (array) $rule->address()->tokens()); } public function testGetTokensWithTrickyFull(): void @@ -206,7 +206,7 @@ public function testGetTokensWithTrickyFull(): void ['', '', '新北', '市'], ['', '', '泰山', '區'], ['', '', '全興', '路'], - ], (array) $rule->address->tokens()); + ], (array) $rule->address()->tokens()); } public function testGetTokensToWithoutUnit(): void @@ -222,7 +222,7 @@ public function testGetTokensToWithoutUnit(): void ['5', '', '', '號'], ['5', '', '', '樓'], ['9', '', '', '樓'], - ], (array) $rule->address->tokens()); + ], (array) $rule->address()->tokens()); } public function testGetTokensToSubnoWithoutUnit(): void @@ -237,7 +237,7 @@ public function testGetTokensToSubnoWithoutUnit(): void ['', '', '學進', '路'], ['82', '之1', '', '號'], ['82', '之20', '', '號'], - ], (array) $rule->address->tokens()); + ], (array) $rule->address()->tokens()); } public function testMatchTheSame(): void diff --git a/tests/RulesTest.php b/tests/RulesTest.php index 1647f7f..f336240 100644 --- a/tests/RulesTest.php +++ b/tests/RulesTest.php @@ -128,7 +128,7 @@ public function testMatchGradually() public function testZip3North() { File::$cached = ['zip3' => null, 'zip5' => null]; - $rules = new Rules(); + $rules = new Rules; $this->assertSame('100', $rules->match('台北市中正區'.uniqid())); $this->assertSame('103', $rules->match('台北市大同區'.uniqid())); $this->assertSame('104', $rules->match('台北市中山區'.uniqid())); diff --git a/tests/Storages/FileTest.php b/tests/Storages/FileTest.php index f30fade..aaa2ec3 100644 --- a/tests/Storages/FileTest.php +++ b/tests/Storages/FileTest.php @@ -2,23 +2,24 @@ namespace Recca0120\Twzipcode\Tests\Storages; -use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; use Mockery as m; +use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; use org\bovigo\vfs\vfsStream; use PHPUnit\Framework\TestCase; -use Recca0120\Twzipcode\Storages\File; use Recca0120\Twzipcode\Address; +use Recca0120\Twzipcode\Storages\File as Storage; class FileTest extends TestCase { use MockeryPHPUnitIntegration; + private $root; private $storage; protected function setUp(): void { - $root = vfsStream::setup(); - $this->storage = new File($root->url()); + $this->root = vfsStream::setup(); + $this->storage = new Storage($this->root->url()); $this->storage->flush()->load(' 10058,臺北市,中正區,八德路1段,全 10079,臺北市,中正區,三元街,單全 @@ -87,7 +88,7 @@ protected function setUp(): void public function testDefaultPath(): void { - $storage = new File(); + $storage = new Storage; $this->assertEquals(realpath(__DIR__.'/../../resources/data').'/', $storage->path); } @@ -116,14 +117,9 @@ public function testRules(): void public function testLoadResources(): void { - File::$cached = [ - 'zip3' => null, - 'zip5' => null, - ]; - $root = vfsStream::setup(); - $storage = new File($root->url()); - $storage->flush() - ->loadFile(__DIR__.'/../../resources/Zip32_utf8_10501_1.zip'); + Storage::$cached = ['zip3' => null, 'zip5' => null]; + $storage = new Storage($this->root->url()); + $storage->flush()->loadFile(__DIR__.'/../../resources/Zip32_utf8_10501_1.zip'); $address = m::mock(Address::class); diff --git a/tests/ZipcodeTest.php b/tests/ZipcodeTest.php index 7e7def0..995cd4d 100644 --- a/tests/ZipcodeTest.php +++ b/tests/ZipcodeTest.php @@ -47,7 +47,7 @@ protected function setUp(): void public function testZipcode(): void { $address = '台中市大里區金城里14鄰塗城路9478巷9478弄9478號'; - $zipcode = Zipcode::parse($address, $this->rules ?: new Rules()); + $zipcode = Zipcode::parse($address, $this->rules ?: new Rules); $this->assertSame('412', $zipcode->zip3()); $this->assertSame('41275', $zipcode->zip5()); @@ -60,7 +60,7 @@ public function testZipcode(): void public function testZipcode2(): void { $address = '宜蘭縣宜蘭市金城里14鄰慶和街同興巷9478弄9478號'; - $zipcode = Zipcode::parse($address, $this->rules ?: new Rules()); + $zipcode = Zipcode::parse($address, $this->rules ?: new Rules); $this->assertSame('260', $zipcode->zip3()); $this->assertSame('26044', $zipcode->zip5()); @@ -73,7 +73,7 @@ public function testZipcode2(): void public function testZipcode3(): void { $address = '台北市中正區中華路1段25號'; - $zipcode = Zipcode::parse($address, $this->rules ?: new Rules()); + $zipcode = Zipcode::parse($address, $this->rules ?: new Rules); $this->assertSame('100', $zipcode->zip3()); $this->assertSame('10043', $zipcode->zip5()); @@ -86,7 +86,7 @@ public function testZipcode3(): void public function testZipcode4(): void { $address = '宜蘭縣壯圍鄉環市東路1段374號'; - $zipcode = Zipcode::parse($address, $this->rules ?: new Rules()); + $zipcode = Zipcode::parse($address, $this->rules ?: new Rules); $this->assertSame('260', $zipcode->zip3()); $this->assertSame('26060', $zipcode->zip5());