-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
151 additions
and
26 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Recca0120\Twzipcode\Sources; | ||
|
||
class Json extends Csv | ||
{ | ||
/** | ||
* @return array{array{zipcode: string, county: string, district: string, text: string}} $rows | ||
*/ | ||
protected function rows() | ||
{ | ||
return array_map(static function ($data) { | ||
return [ | ||
'zipcode' => $data['郵遞區號'], | ||
'county' => $data['縣市名稱'], | ||
'district' => $data['鄉鎮市區'], | ||
'rule' => implode(',', $data), | ||
]; | ||
}, json_decode($this->contents(), true)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Recca0120\Twzipcode\Tests\Sources; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Recca0120\Twzipcode\Sources\Csv; | ||
|
||
class CsvTest extends TestCase | ||
{ | ||
public function test_csv_contents() | ||
{ | ||
$source = new StubCsv('test.csv'); | ||
$source->setContents('10058,臺北市,中正區,八德路1段,全 | ||
10079,臺北市,中正區,三元街,單全 | ||
'); | ||
|
||
$source->each(function ($zipcode, $county, $district, $rules) { | ||
self::assertEquals(100, $zipcode); | ||
self::assertEquals('臺北市', $county); | ||
self::assertEquals('中正區', $district); | ||
self::assertEquals([ | ||
'10058,臺北市,中正區,八德路1段,全', | ||
'10079,臺北市,中正區,三元街,單全', | ||
], $rules); | ||
}); | ||
} | ||
} | ||
|
||
class StubCsv extends Csv | ||
{ | ||
private $contents = ''; | ||
|
||
public function setContents($contents) | ||
{ | ||
$this->contents = $contents; | ||
} | ||
|
||
public function contents() | ||
{ | ||
return $this->contents; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Recca0120\Twzipcode\Tests\Sources; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Recca0120\Twzipcode\Sources\Json; | ||
|
||
class JsonTest extends TestCase | ||
{ | ||
public function test_csv_contents() | ||
{ | ||
$source = new StubJson('test.json'); | ||
$source->setContents('[{"郵遞區號":"10058","縣市名稱":"臺北市","鄉鎮市區":"中正區","原始路名":"八德路1段","投遞範圍":"全"},{"郵遞區號":"10079","縣市名稱":"臺北市","鄉鎮市區":"中正區","原始路名":"三元街","投遞範圍":"單全"}]'); | ||
|
||
$source->each(function ($zipcode, $county, $district, $rules) { | ||
self::assertEquals(100, $zipcode); | ||
self::assertEquals('臺北市', $county); | ||
self::assertEquals('中正區', $district); | ||
self::assertEquals([ | ||
'10058,臺北市,中正區,八德路1段,全', | ||
'10079,臺北市,中正區,三元街,單全', | ||
], $rules); | ||
}); | ||
} | ||
} | ||
|
||
class StubJson extends Json | ||
{ | ||
private $contents = ''; | ||
|
||
public function setContents($contents) | ||
{ | ||
$this->contents = $contents; | ||
} | ||
|
||
public function contents() | ||
{ | ||
return $this->contents; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters