-
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
12 changed files
with
196 additions
and
127 deletions.
There are no files selected for viewing
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,8 @@ | ||
<?php | ||
|
||
namespace Recca0120\Twzipcode\Contracts; | ||
|
||
interface Source | ||
{ | ||
public function each(callable $callback); | ||
} |
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,23 @@ | ||
<?php | ||
|
||
namespace Recca0120\Twzipcode\Sources; | ||
|
||
class CSV extends Source | ||
{ | ||
/** @var string */ | ||
protected $file; | ||
|
||
/** @var string */ | ||
private $extension; | ||
|
||
public function __construct($file) | ||
{ | ||
$this->file = $file; | ||
$this->extension = pathinfo($this->file, PATHINFO_EXTENSION); | ||
} | ||
|
||
protected function getContents() | ||
{ | ||
return $this->extension === 'zip' ? static::unzip($this->file) : file_get_contents($this->file); | ||
} | ||
} |
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,84 @@ | ||
<?php | ||
|
||
namespace Recca0120\Twzipcode\Sources; | ||
|
||
use Recca0120\Twzipcode\Contracts\Source as SourceContract; | ||
use ZipArchive; | ||
|
||
abstract class Source implements SourceContract | ||
{ | ||
protected static $tricks = [ | ||
'宜蘭縣壯圍鄉' => '263', | ||
'新竹縣寶山鄉' => '308', | ||
'臺南市新市區' => '744', | ||
]; | ||
|
||
public function each(callable $callback) | ||
{ | ||
static::eachGroup(static::prepare($this->rows()), $callback); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
abstract protected function getContents(); | ||
|
||
/** | ||
* @return array{array{zipcode: string, county: string, district: string, text: string}} $rows | ||
*/ | ||
protected function rows() | ||
{ | ||
$lines = preg_split('/\n|\r\n$/', $this->getContents()); | ||
$lines = array_filter($lines, static function ($line) { | ||
return ! empty(trim($line)); | ||
}); | ||
|
||
return array_map(static function ($line) { | ||
$data = explode(',', $line); | ||
|
||
return ['zipcode' => $data[0], 'county' => $data[1], 'district' => $data[2], 'text' => $line]; | ||
}, $lines); | ||
} | ||
|
||
/** | ||
* @param array{array{zipcode: string, county: string, district: string, text: string}} $rows | ||
* @return array | ||
*/ | ||
protected static function prepare($rows) | ||
{ | ||
return array_reduce($rows, static function ($results, $row) { | ||
$zip3 = ! empty(self::$tricks[$row['county'].$row['district']]) | ||
? self::$tricks[$row['county'].$row['district']] | ||
: substr($row['zipcode'], 0, 3); | ||
|
||
$results[$row['county']][$row['district']][$zip3][] = $row['text']; | ||
|
||
return $results; | ||
}, []); | ||
} | ||
|
||
protected static function eachGroup($ruleGroup, $callback) | ||
{ | ||
foreach ($ruleGroup as $county => $districts) { | ||
foreach ($districts as $district => $addresses) { | ||
foreach ($addresses as $zipcode => $rule) { | ||
$callback($zipcode, $county, $district, $rule); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param string $file | ||
* @return string | ||
*/ | ||
protected static function unzip($file) | ||
{ | ||
$zip = new ZipArchive; | ||
$zip->open($file); | ||
$contents = $zip->getFromIndex(0); | ||
$zip->close(); | ||
|
||
return $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,24 @@ | ||
<?php | ||
|
||
namespace Recca0120\Twzipcode\Sources; | ||
|
||
class Text extends Source | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $text; | ||
|
||
/** | ||
* @param string $text | ||
*/ | ||
public function __construct($text) | ||
{ | ||
$this->text = $text; | ||
} | ||
|
||
protected function getContents() | ||
{ | ||
return $this->text; | ||
} | ||
} |
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
Oops, something went wrong.