From fef33f3bcc161814e75e370fe95d0e33a4a427db Mon Sep 17 00:00:00 2001 From: recca0120 Date: Mon, 11 Nov 2024 04:07:36 +0800 Subject: [PATCH] refactor --- src/Address.php | 25 +++++++++++++------------ src/Point.php | 15 ++++++++------- src/Rule.php | 6 +++--- src/Sources/CSV.php | 2 +- src/Sources/Source.php | 4 ++-- src/Sources/Text.php | 2 +- src/Tricky.php | 23 ++++++++++++++--------- 7 files changed, 42 insertions(+), 35 deletions(-) diff --git a/src/Address.php b/src/Address.php index abb6ba0..13da642 100644 --- a/src/Address.php +++ b/src/Address.php @@ -32,7 +32,6 @@ class Address */ public function __construct($address = '') { - $this->tricky = new Tricky; if (empty($address) === false) { $this->set($address); } @@ -62,14 +61,15 @@ private function tokenize() '(?:(?P([島縣市鄉鎮市區村里道鄰路街段巷弄號樓]|魚臺))|(?=\d+(?:之\d+)?[巷弄號樓]|$))', ]); + $tricky = Tricky::instance(); + $address = $tricky->hash($this->normalizer)->value(); $tokens = []; - $address = $this->tricky->hash($this->normalizer)->value(); if (preg_match_all('/'.$patterns.'/u', $address, $matches, PREG_SET_ORDER) !== false) { foreach ($matches as $values) { $token = array_map(static function ($unit) use ($values) { return isset($values[$unit]) === true ? $values[$unit] : ''; }, $units); - $token[static::NAME] = $this->tricky->flip($token[static::NAME]); + $token[static::NAME] = $tricky->flip($token[static::NAME]); $tokens[] = $token; } } @@ -77,14 +77,6 @@ private function tokenize() return $tokens; } - /** - * @return string - */ - public function __toString() - { - return $this->normalizer->value(); - } - /** * @return JArray */ @@ -97,11 +89,12 @@ public function tokens() * @param string $index * @return Point */ - public function getPoint($index) + public function point($index) { if (isset($this->tokens[$index]) === false) { return new Point(0, 0); } + $token = $this->tokens[$index]; return new Point( @@ -125,4 +118,12 @@ public function flat($length = null, $offset = 0) return implode('', $token); })->join(''); } + + /** + * @return string + */ + public function __toString() + { + return $this->normalizer->value(); + } } diff --git a/src/Point.php b/src/Point.php index 12028d9..3704915 100644 --- a/src/Point.php +++ b/src/Point.php @@ -34,19 +34,20 @@ public function isEmpty() */ public function compare(self $point, $operator = '=') { - $sum = $this->x * 10 + $this->y; - $sum2 = $point->x * 10 + $point->y; + $x = $this->x * 10 + $this->y; + $y = $point->x * 10 + $point->y; + switch ($operator) { case '>': - return $sum > $sum2; + return $x > $y; case '>=': - return $sum >= $sum2; + return $x >= $y; case '<': - return $sum < $sum2; + return $x < $y; case '<=': - return $sum <= $sum2; + return $x <= $y; } - return $sum === $sum2; + return $x === $y; } } diff --git a/src/Rule.php b/src/Rule.php index a8d421e..dabf7c7 100644 --- a/src/Rule.php +++ b/src/Rule.php @@ -81,14 +81,14 @@ public function match($address) return false; } - $addressPoint = $address->getPoint($cur + 1); + $addressPoint = $address->point($cur + 1); if ($currentTokens->length() > 0 && $addressPoint->isEmpty() === true) { return false; } - $left = $this->address->getPoint($ruleAddressTokens->length() - 1); - $right = $this->address->getPoint($ruleAddressTokens->length() - 2); + $left = $this->address->point($ruleAddressTokens->length() - 1); + $right = $this->address->point($ruleAddressTokens->length() - 2); foreach ($this->tokens as $token) { if ( diff --git a/src/Sources/CSV.php b/src/Sources/CSV.php index 70b703c..e00710b 100644 --- a/src/Sources/CSV.php +++ b/src/Sources/CSV.php @@ -16,7 +16,7 @@ public function __construct($file) $this->extension = pathinfo($this->file, PATHINFO_EXTENSION); } - protected function getContents() + protected function contents() { return $this->extension === 'zip' ? static::unzip($this->file) : file_get_contents($this->file); } diff --git a/src/Sources/Source.php b/src/Sources/Source.php index 4fcb5bc..a611bec 100644 --- a/src/Sources/Source.php +++ b/src/Sources/Source.php @@ -21,14 +21,14 @@ public function each(callable $callback) /** * @return string */ - abstract protected function getContents(); + abstract protected function contents(); /** * @return array{array{zipcode: string, county: string, district: string, text: string}} $rows */ protected function rows() { - $lines = preg_split('/\n|\r\n$/', $this->getContents()); + $lines = preg_split('/\n|\r\n$/', $this->contents()); $lines = array_filter($lines, static function ($line) { return ! empty(trim($line)); }); diff --git a/src/Sources/Text.php b/src/Sources/Text.php index 28f054f..2f12b44 100644 --- a/src/Sources/Text.php +++ b/src/Sources/Text.php @@ -17,7 +17,7 @@ public function __construct($text) $this->text = $text; } - protected function getContents() + protected function contents() { return $this->text; } diff --git a/src/Tricky.php b/src/Tricky.php index 59418eb..55034c5 100644 --- a/src/Tricky.php +++ b/src/Tricky.php @@ -4,6 +4,10 @@ class Tricky { + /** @var Tricky */ + private static $instance; + private static $cached = []; + /* * 20742,新北市,萬里區,二坪,全 * 21042,連江縣,北竿鄉,坂里村,全 @@ -33,15 +37,6 @@ class Tricky * 89442,金門縣,烈嶼鄉,二擔,全 */ - private static $cached = []; - - public function __construct() - { - if (! array_key_exists('tricky', self::$cached)) { - $this->init(); - } - } - /** * @return void */ @@ -88,4 +83,14 @@ public function flip($token) { return strtr($token, self::$cached['flip']); } + + public static function instance() + { + if (! self::$instance) { + self::$instance = new self(); + self::$instance->init(); + } + + return self::$instance; + } }