Skip to content

Commit fef33f3

Browse files
committed
refactor
1 parent 23a3e8f commit fef33f3

File tree

7 files changed

+42
-35
lines changed

7 files changed

+42
-35
lines changed

src/Address.php

+13-12
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class Address
3232
*/
3333
public function __construct($address = '')
3434
{
35-
$this->tricky = new Tricky;
3635
if (empty($address) === false) {
3736
$this->set($address);
3837
}
@@ -62,29 +61,22 @@ private function tokenize()
6261
'(?:(?P<unit>([島縣市鄉鎮市區村里道鄰路街段巷弄號樓]|魚臺))|(?=\d+(?:之\d+)?[巷弄號樓]|$))',
6362
]);
6463

64+
$tricky = Tricky::instance();
65+
$address = $tricky->hash($this->normalizer)->value();
6566
$tokens = [];
66-
$address = $this->tricky->hash($this->normalizer)->value();
6767
if (preg_match_all('/'.$patterns.'/u', $address, $matches, PREG_SET_ORDER) !== false) {
6868
foreach ($matches as $values) {
6969
$token = array_map(static function ($unit) use ($values) {
7070
return isset($values[$unit]) === true ? $values[$unit] : '';
7171
}, $units);
72-
$token[static::NAME] = $this->tricky->flip($token[static::NAME]);
72+
$token[static::NAME] = $tricky->flip($token[static::NAME]);
7373
$tokens[] = $token;
7474
}
7575
}
7676

7777
return $tokens;
7878
}
7979

80-
/**
81-
* @return string
82-
*/
83-
public function __toString()
84-
{
85-
return $this->normalizer->value();
86-
}
87-
8880
/**
8981
* @return JArray
9082
*/
@@ -97,11 +89,12 @@ public function tokens()
9789
* @param string $index
9890
* @return Point
9991
*/
100-
public function getPoint($index)
92+
public function point($index)
10193
{
10294
if (isset($this->tokens[$index]) === false) {
10395
return new Point(0, 0);
10496
}
97+
10598
$token = $this->tokens[$index];
10699

107100
return new Point(
@@ -125,4 +118,12 @@ public function flat($length = null, $offset = 0)
125118
return implode('', $token);
126119
})->join('');
127120
}
121+
122+
/**
123+
* @return string
124+
*/
125+
public function __toString()
126+
{
127+
return $this->normalizer->value();
128+
}
128129
}

src/Point.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,20 @@ public function isEmpty()
3434
*/
3535
public function compare(self $point, $operator = '=')
3636
{
37-
$sum = $this->x * 10 + $this->y;
38-
$sum2 = $point->x * 10 + $point->y;
37+
$x = $this->x * 10 + $this->y;
38+
$y = $point->x * 10 + $point->y;
39+
3940
switch ($operator) {
4041
case '>':
41-
return $sum > $sum2;
42+
return $x > $y;
4243
case '>=':
43-
return $sum >= $sum2;
44+
return $x >= $y;
4445
case '<':
45-
return $sum < $sum2;
46+
return $x < $y;
4647
case '<=':
47-
return $sum <= $sum2;
48+
return $x <= $y;
4849
}
4950

50-
return $sum === $sum2;
51+
return $x === $y;
5152
}
5253
}

src/Rule.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ public function match($address)
8181
return false;
8282
}
8383

84-
$addressPoint = $address->getPoint($cur + 1);
84+
$addressPoint = $address->point($cur + 1);
8585

8686
if ($currentTokens->length() > 0 && $addressPoint->isEmpty() === true) {
8787
return false;
8888
}
8989

90-
$left = $this->address->getPoint($ruleAddressTokens->length() - 1);
91-
$right = $this->address->getPoint($ruleAddressTokens->length() - 2);
90+
$left = $this->address->point($ruleAddressTokens->length() - 1);
91+
$right = $this->address->point($ruleAddressTokens->length() - 2);
9292

9393
foreach ($this->tokens as $token) {
9494
if (

src/Sources/CSV.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function __construct($file)
1616
$this->extension = pathinfo($this->file, PATHINFO_EXTENSION);
1717
}
1818

19-
protected function getContents()
19+
protected function contents()
2020
{
2121
return $this->extension === 'zip' ? static::unzip($this->file) : file_get_contents($this->file);
2222
}

src/Sources/Source.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ public function each(callable $callback)
2121
/**
2222
* @return string
2323
*/
24-
abstract protected function getContents();
24+
abstract protected function contents();
2525

2626
/**
2727
* @return array{array{zipcode: string, county: string, district: string, text: string}} $rows
2828
*/
2929
protected function rows()
3030
{
31-
$lines = preg_split('/\n|\r\n$/', $this->getContents());
31+
$lines = preg_split('/\n|\r\n$/', $this->contents());
3232
$lines = array_filter($lines, static function ($line) {
3333
return ! empty(trim($line));
3434
});

src/Sources/Text.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function __construct($text)
1717
$this->text = $text;
1818
}
1919

20-
protected function getContents()
20+
protected function contents()
2121
{
2222
return $this->text;
2323
}

src/Tricky.php

+14-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
class Tricky
66
{
7+
/** @var Tricky */
8+
private static $instance;
9+
private static $cached = [];
10+
711
/*
812
* 20742,新北市,萬里區,二坪,全
913
* 21042,連江縣,北竿鄉,坂里村,全
@@ -33,15 +37,6 @@ class Tricky
3337
* 89442,金門縣,烈嶼鄉,二擔,全
3438
*/
3539

36-
private static $cached = [];
37-
38-
public function __construct()
39-
{
40-
if (! array_key_exists('tricky', self::$cached)) {
41-
$this->init();
42-
}
43-
}
44-
4540
/**
4641
* @return void
4742
*/
@@ -88,4 +83,14 @@ public function flip($token)
8883
{
8984
return strtr($token, self::$cached['flip']);
9085
}
86+
87+
public static function instance()
88+
{
89+
if (! self::$instance) {
90+
self::$instance = new self();
91+
self::$instance->init();
92+
}
93+
94+
return self::$instance;
95+
}
9196
}

0 commit comments

Comments
 (0)