Skip to content

Commit

Permalink
8.3
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Nov 10, 2024
1 parent 215c2ae commit fe46d4c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 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: [ '5.5.9', '5.6', '7.1','7.2', '7.3', '7.4', '8.0', '8.1', '8.2' ]
php: [ '5.5.9', '5.6', '7.1','7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3' ]
stability: [ prefer-stable ]

name: PHP ${{ matrix.php }} - ${{ matrix.stability }}
Expand Down
11 changes: 10 additions & 1 deletion resources/converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,31 @@

use Recca0120\Twzipcode\Storages\File;

set_error_handler(static function ($severity, $message, $file, $line) {
throw new ErrorException($message, $severity, $severity, $file, $line);
});

$start = microtime(true);
$file = __DIR__.'/Zip32_utf8_10501_1.zip';

// https://data.gov.tw/dataset/5948
$url = 'https://quality.data.gov.tw/dq_download_csv.php?nid=5948&md5_url=b7ce2082883f6b680810828799d4c32e';
$url = 'https://quality.data.gov.tw/dq_download_csv.php?nid=5948&md5_url=e1f6004ad33eb3ff3a824fb992a4b01a';

if (file_exists($file) === false) {
touch($file);
$contents = file_get_contents($url);

$encoding = mb_detect_encoding($contents, ['UCS-2LE', 'BIG5', 'UTF-8']);
if ($encoding !== 'UTF-8') {
$contents = mb_convert_encoding($contents, 'UTF-8', $encoding);
}
$contents = preg_replace("/^\xEF\xBB\xBF/", '', $contents);
$contents = trim(str_replace('Zip5,City,Area,Road,Scope', '', $contents));

if (strpos('Zip5,City,Area,Road,Scope', $contents) === false) {
throw new RuntimeException($contents);
}

$zip = new ZipArchive;
$zip->open($file, ZipArchive::OVERWRITE);
$zip->addFromString(pathinfo($file, PATHINFO_FILENAME).'.csv', $contents);
Expand Down
12 changes: 6 additions & 6 deletions src/Storages/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function zip3(Address $address)

foreach ([2, 1] as $value) {
$flat = $address->flat($value);
if (isset(self::$cached['zip3'][$flat])) {
if (! empty(self::$cached['zip3'][$flat])) {
return self::$cached['zip3'][$flat];
}
}
Expand All @@ -65,11 +65,11 @@ function ($zipcode, $county, $district, $rules) use (&$zip5, &$zip3) {
return new Rule($rule);
}, $rules));

if (isset($zip3[$county]) === false) {
if (empty($zip3[$county])) {
$zip3[$county] = substr($zipcode, 0, 1);
}

if (isset($zip3[$county.$district]) === false) {
if (empty($zip3[$county.$district])) {
$zip3[$county.$district] = substr($zipcode, 0, 3);
}
});
Expand All @@ -88,7 +88,7 @@ public function rules($zip3)
{
$this->restore('zip5');

return isset(self::$cached['zip5'][$zip3]) === true
return ! empty(self::$cached['zip5'][$zip3])
? new JArray($this->decompress(self::$cached['zip5'][$zip3]))
: new JArray([]);
}
Expand Down Expand Up @@ -168,9 +168,9 @@ private function prepareSource($source)
$results = [];
$rules = preg_split('/\n|\r\n$/', $source);
foreach ($rules as $rule) {
if (empty(trim($rule)) === false) {
if (! empty(trim($rule))) {
list($zipcode, $county, $district) = explode(',', $rule);
$zip3 = isset($tricks[$county.$district]) === true
$zip3 = ! empty($tricks[$county.$district])
? $tricks[$county.$district]
: substr($zipcode, 0, 3);
$results[$county][$district][$zip3][] = $rule;
Expand Down

0 comments on commit fe46d4c

Please sign in to comment.