Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Nov 10, 2024
1 parent 0145667 commit cf38d19
Show file tree
Hide file tree
Showing 21 changed files with 584 additions and 899 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.1','7.2', '7.3', '7.4', '8.0', '8.1', '8.2' ]
stability: [ prefer-stable ]

name: PHP ${{ matrix.php }} - ${{ matrix.stability }}
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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"
},
Expand Down
4 changes: 2 additions & 2 deletions resources/converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
97 changes: 18 additions & 79 deletions src/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,86 +6,46 @@

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

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'];

Expand All @@ -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);
Expand All @@ -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('');
}
}
38 changes: 5 additions & 33 deletions src/Contracts/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
28 changes: 4 additions & 24 deletions src/Normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@

class Normalizer extends JString
{
/**
* normalizeAddress.
*
* @return static
*/
public function normalizeAddress()
public function normalizeAddress(): self
{
return $this
->replace([
Expand Down Expand Up @@ -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()
Expand Down
33 changes: 3 additions & 30 deletions src/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit cf38d19

Please sign in to comment.