Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Nov 10, 2024
1 parent 23a3e8f commit fef33f3
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 35 deletions.
25 changes: 13 additions & 12 deletions src/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class Address
*/
public function __construct($address = '')
{
$this->tricky = new Tricky;
if (empty($address) === false) {
$this->set($address);
}
Expand Down Expand Up @@ -62,29 +61,22 @@ private function tokenize()
'(?:(?P<unit>([島縣市鄉鎮市區村里道鄰路街段巷弄號樓]|魚臺))|(?=\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;
}
}

return $tokens;
}

/**
* @return string
*/
public function __toString()
{
return $this->normalizer->value();
}

/**
* @return JArray
*/
Expand All @@ -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(
Expand All @@ -125,4 +118,12 @@ public function flat($length = null, $offset = 0)
return implode('', $token);
})->join('');
}

/**
* @return string
*/
public function __toString()
{
return $this->normalizer->value();
}
}
15 changes: 8 additions & 7 deletions src/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
6 changes: 3 additions & 3 deletions src/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion src/Sources/CSV.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Sources/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
Expand Down
2 changes: 1 addition & 1 deletion src/Sources/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct($text)
$this->text = $text;
}

protected function getContents()
protected function contents()
{
return $this->text;
}
Expand Down
23 changes: 14 additions & 9 deletions src/Tricky.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

class Tricky
{
/** @var Tricky */
private static $instance;
private static $cached = [];

/*
* 20742,新北市,萬里區,二坪,全
* 21042,連江縣,北竿鄉,坂里村,全
Expand Down Expand Up @@ -33,15 +37,6 @@ class Tricky
* 89442,金門縣,烈嶼鄉,二擔,全
*/

private static $cached = [];

public function __construct()
{
if (! array_key_exists('tricky', self::$cached)) {
$this->init();
}
}

/**
* @return void
*/
Expand Down Expand Up @@ -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;
}
}

0 comments on commit fef33f3

Please sign in to comment.