Skip to content

Commit

Permalink
feat: support php8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Aug 19, 2022
1 parent 113f380 commit 9ffc671
Show file tree
Hide file tree
Showing 20 changed files with 657 additions and 515 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: tests

on: [ push ]

jobs:
build-test:
runs-on: ubuntu-latest

strategy:
fail-fast: true
matrix:
php: [ '7.1','7.2', '7.3', '7.4', '8.0', '8.1' ]
stability: [ prefer-stable ]

name: PHP ${{ matrix.php }} - ${{ matrix.stability }}

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, gd, redis, memcached
tools: composer:v2
coverage: none

- name: Install dependencies
uses: nick-invision/retry@v1
with:
timeout_minutes: 5
max_attempts: 5
command: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress ${{ matrix.flags }}

- name: Execute tests
continue-on-error: ${{ matrix.php <= 7.0 }}
run: vendor/bin/phpunit --verbose
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
./idea
/vendor
/resources/assets/vendor
/node_modules
/build
/src/data
composer.phar
composer.lock
.phpunit.result.cache
.php_cs.cache
.DS_Store
Thumbs.db
Expand Down
13 changes: 8 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
"require": {
"php": ">=5.5.9",
"ext-zlib": "*",
"recca0120/lodash": "^0.0.5"
"recca0120/lodash": "^1.1"
},
"require-dev": {
"mikey179/vfsStream": "^1.6",
"mikey179/vfsstream": "^1.6",
"mockery/mockery": "~0.9.4|~1.0",
"phpunit/phpunit": "~4.8|~5.4|~6.3|~7.0",
"nesbot/carbon": "~1.20"
"phpunit/phpunit": "~4.8|~5.4|~6.3|~7.0|~8.0|~9.0",
"nesbot/carbon": "~1.20|~2.0"
},
"autoload": {
"psr-4": {
Expand All @@ -28,7 +28,10 @@
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true
"optimize-autoloader": true,
"allow-plugins": {
"kylekatarnls/update-helper": true
}
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down
35 changes: 10 additions & 25 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
syntaxCheck="true"
verbose="true"
>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false"
bootstrap="tests/bootstrap.php" colors="true" convertErrorsToExceptions="true"
convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false"
stopOnError="false" stopOnFailure="false" verbose="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
</phpunit>
Binary file modified resources/data/zip3.rules
Binary file not shown.
Binary file modified resources/data/zip5.rules
Binary file not shown.
138 changes: 69 additions & 69 deletions src/Storages/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,51 @@

namespace Recca0120\Twzipcode\Storages;

use ZipArchive;
use Closure;
use Recca0120\Lodash\JArray;
use Recca0120\Twzipcode\Rule;
use Recca0120\Twzipcode\Address;
use Recca0120\Twzipcode\Contracts\Storage;
use Recca0120\Twzipcode\Rule;
use ZipArchive;

class File implements Storage
{
/**
* cached.
*
* @var array
*/
public static $cached = [
'zip3' => null,
'zip5' => null,
];
/**
* $path.
*
* @var string
*/
public $path;

/**
* $suffix.
*
* @var string
*/
public $suffix = '.rules';

/**
* cached.
*
* @var array
*/
public static $cached = [
'zip3' => null,
'zip5' => null,
];

/**
* __construct.
*
* @param string $path
*/
public function __construct($path = null)
{
$this->path = ($path ?: realpath(__DIR__.'/../../resources/data')).'/';
$this->path = ($path ?: dirname(dirname(__DIR__)).'/resources/data').'/';
}

/**
* zip3.
*
* @param \Recca0120\Twzipcode\Address $address
* @param Address $address
* @return string
*/
public function zip3(Address $address)
Expand All @@ -59,18 +58,35 @@ public function zip3(Address $address)
}

/**
* rules.
* restore.
*
* @param string $zip3
* @return \Recca0120\Lodash\JArray
* @param string $filename
* @return mixed
*/
public function rules($zip3)
protected function restore($filename)
{
$this->restore('zip5');
if (file_exists($this->path.$filename.$this->suffix) === false) {
return false;
}

return isset(self::$cached['zip5'][$zip3]) === true
? $this->decompress(self::$cached['zip5'][$zip3])
: new JArray([]);
if (is_null(self::$cached[$filename]) === false) {
return self::$cached[$filename];
}

return self::$cached[$filename] = $this->decompress(
file_get_contents($this->path.$filename.$this->suffix)
);
}

/**
* decompress.
*
* @param string $compressed
* @return mixed
*/
protected function decompress($compressed)
{
return unserialize(gzuncompress($compressed));
}

/**
Expand All @@ -84,11 +100,9 @@ public function load($source)
$zip5 = new JArray;
$zip3 = new JArray;
$this->each($this->prepareSource($source), function ($zipcode, $county, $district, $rules) use ($zip5, $zip3) {
$zip5[$zipcode] = $this->compress(
(new JArray($rules))->map(function ($rule) {
return new Rule($rule);
})
);
$zip5[$zipcode] = $this->compress((new JArray($rules))->map(function ($rule) {
return new Rule($rule);
}));

if (isset($zip3[$county]) === false) {
$zip3[$county] = substr($zipcode, 0, 1);
Expand All @@ -105,6 +119,21 @@ public function load($source)
return $this;
}

/**
* rules.
*
* @param string $zip3
* @return JArray
*/
public function rules($zip3)
{
$this->restore('zip5');

return isset(self::$cached['zip5'][$zip3]) === true
? $this->decompress(self::$cached['zip5'][$zip3])
: new JArray([]);
}

/**
* loadFile.
*
Expand Down Expand Up @@ -167,7 +196,7 @@ protected function getSource($file)
*/
protected function prepareSource($source)
{
$tickies = [
$tricks = [
'宜蘭縣壯圍鄉' => '263',
'新竹縣寶山鄉' => '308',
'臺南市新市區' => '744',
Expand All @@ -177,8 +206,8 @@ protected function prepareSource($source)
foreach ($rules as $rule) {
if (empty(trim($rule)) === false) {
list($zipcode, $county, $district) = explode(',', $rule);
$zip3 = isset($tickies[$county.$district]) === true
? $tickies[$county.$district]
$zip3 = isset($tricks[$county.$district]) === true
? $tricks[$county.$district]
: substr($zipcode, 0, 3);
$results[$county][$district][$zip3][] = $rule;
}
Expand All @@ -190,15 +219,15 @@ protected function prepareSource($source)
/**
* each.
*
* @param array $rules
* @param \Closure $callback
* @param array $ruleGroup
* @param Closure $callback
*/
protected function each($rules, $callback)
protected function each($ruleGroup, $callback)
{
foreach ($rules as $county => $temp) {
foreach ($temp as $district => $temp2) {
foreach ($temp2 as $zipcode => $rules) {
$callback($zipcode, $county, $district, $rules);
foreach ($ruleGroup as $county => $districts) {
foreach ($districts as $district => $addresses) {
foreach ($addresses as $zipcode => $rule) {
$callback($zipcode, $county, $district, $rule);
}
}
}
Expand All @@ -207,30 +236,20 @@ protected function each($rules, $callback)
/**
* compress.
*
* @param \Recca0120\Lodash\JArray $plainText
* @param JArray $plainText
* @return string
*/
protected function compress($plainText)
{
return gzcompress(serialize($plainText));
}

/**
* decompress.
*
* @param string $compressed
* @return mixed
*/
protected function decompress($compressed)
{
return unserialize(gzuncompress($compressed));
}

/**
* store.
*
* @param string $filename
* @param \Recca0120\Lodash\JArray $data
* @param JArray $data
* @return $this
*/
protected function store($filename, $data)
Expand All @@ -243,24 +262,5 @@ protected function store($filename, $data)
return $this;
}

/**
* restore.
*
* @param string $filename
* @return mixed
*/
protected function restore($filename)
{
if (file_exists($this->path.$filename.$this->suffix) === false) {
return false;
}

if (is_null(self::$cached[$filename]) === false) {
return self::$cached[$filename];
}

return self::$cached[$filename] = $this->decompress(
file_get_contents($this->path.$filename.$this->suffix)
);
}
}
6 changes: 2 additions & 4 deletions tests/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Recca0120\Twzipcode\Address;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;

class AddressTest extends TestCase
{
protected function tearDown()
{
m::close();
}
use MockeryPHPUnitIntegration;

public function testGetTokens()
{
Expand Down
Loading

0 comments on commit 9ffc671

Please sign in to comment.