diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index fdb2697..8dfb311 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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 }} diff --git a/resources/converter.php b/resources/converter.php index c4fb615..7181b3c 100644 --- a/resources/converter.php +++ b/resources/converter.php @@ -4,15 +4,20 @@ 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); @@ -20,6 +25,10 @@ $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); diff --git a/src/Storages/File.php b/src/Storages/File.php index e392e34..433f111 100644 --- a/src/Storages/File.php +++ b/src/Storages/File.php @@ -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]; } } @@ -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); } }); @@ -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([]); } @@ -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;