Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP向けミニマム実装 #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: 2

jobs:
build:
working_directory: ~/zengin-php
docker:
- image: circleci/php:latest
steps:
- checkout
- run: sudo composer self-update
- run: composer install -n --prefer-dist
- run:
name: phpunit
command: ./vendor/bin/phpunit
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/*
/composer.lock
/vendor/
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "source-data"]
path = source-data
url = git@github.com:zengin-code/source-data.git
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,37 @@
# zengin-php
The ruby implementation of ZenginCode.
The PHP implementation of ZenginCode.

## Installation
Install via [composer](https://getcomposer.org/).

```
$ composer require zengin-code/zengin-php
```

## Usage
```php
// get source data updated at
echo \ZenginCode\ZenginCode::getLastUpdatedAt(); // 20190501

// get all banks as Bank instance array
$banks = \ZenginCode\ZenginCode::findBank();

// find bank from code as Bank instance
$bank = \ZenginCode\ZenginCode::findBank('0005');
echo $bank->code . PHP_EOL; // 0005
echo $bank->name . PHP_EOL; // 三菱UFJ
echo $bank->kana . PHP_EOL; // ミツビシユ-エフジエイ
echo $bank->hira . PHP_EOL; // みつびしゆ-えふじえい
echo $bank->roma . PHP_EOL; // mitsubishiyu-efujiei

// get all branches of bank as Branch instance array
$branches = \ZenginCode\ZenginCode::findBranch();

// find branch from code as Branch instance
$branch = \ZenginCode\ZenginCode::findBranch('0005', '002');
echo $branch->code . PHP_EOL; // 002
echo $branch->name . PHP_EOL; // 丸の内
echo $branch->kana . PHP_EOL; // マルノウチ
echo $branch->hira . PHP_EOL; // まるのうち
echo $branch->roma . PHP_EOL; // marunouchi
```
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "zengin-code/zengin-php",
"description": "bank codes and branch codes for Japanese",
"keywords": ["bank code"],
"homepage": "https://github.com/thatblue/zengin-php",
"license": "MIT",
"require": {
"php": ">= 5.5",
"guzzlehttp/guzzle": "^6.3"
},
"require-dev": {
"phpunit/phpunit": "^7.5 || ^4.8",
"php-coveralls/php-coveralls": "^2.1 || ^1.1"
},
"autoload": {
"psr-4": {
"ZenginCode\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"ZenginCode\\": "tests/"
}
}
}
21 changes: 21 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="tests/bootstrap.php"
colors="true"
verbose="true">
<testsuites>
<testsuite name="zengin-php">
<directory>tests/ZenginCode/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>

<php>
<const name="PHPUNIT_TESTSUITE" value="true"/>
</php>
</phpunit>
1 change: 1 addition & 0 deletions source-data
Submodule source-data added at c398f1
25 changes: 25 additions & 0 deletions src/Model/Bank.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace ZenginCode\Model;

/**
* Class Bank
*
* @package ZenginCode\Model
*/
class Bank
{
public $code = null;
public $name = null;
public $kana = null;
public $hira = null;
public $roma = null;

public function __construct($sourceJson)
{
$this->code = $sourceJson['code'];
$this->name = $sourceJson['name'];
$this->kana = $sourceJson['kana'];
$this->hira = $sourceJson['hira'];
$this->roma = $sourceJson['roma'];
}
}
25 changes: 25 additions & 0 deletions src/Model/Branch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace ZenginCode\Model;

/**
* Class Branch
*
* @package ZenginCode\Model
*/
class Branch
{
public $code = null;
public $name = null;
public $kana = null;
public $hira = null;
public $roma = null;

public function __construct($sourceJson)
{
$this->code = $sourceJson['code'];
$this->name = $sourceJson['name'];
$this->kana = $sourceJson['kana'];
$this->hira = $sourceJson['hira'];
$this->roma = $sourceJson['roma'];
}
}
103 changes: 103 additions & 0 deletions src/ZenginCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
namespace ZenginCode;

use ZenginCode\Model\Bank;
use ZenginCode\Model\Branch;

class ZenginCode
{
protected static $_version = null;
protected static $_banks = [];
protected static $_branches = [];

const DATA_PATH = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'source-data' . DIRECTORY_SEPARATOR . 'data';
const DIGIT_BANK_CODE = 4;
const DIGIT_BRANCH_CODE = 3;

/**
* get Last Updated date.
*
* @return string updated_at (YYYYMMDD style)
*/
public static function getLastUpdatedAt()
{
if(!static::$_version) {
static::$_version = file_get_contents(ZenginCode::DATA_PATH . DIRECTORY_SEPARATOR . 'updated_at');
}

return static::$_version;
}

/**
* load source file
* NOTICE: USE ONLY INTERNAL CALL (this method not verify path)
*
* @param $path
* @return array
*/
protected static function loadSourceFile($path)
{
if(!is_file(static::DATA_PATH . DIRECTORY_SEPARATOR . $path)) {
return null;
}

$fileContent = file_get_contents(static::DATA_PATH . DIRECTORY_SEPARATOR . $path);
return $fileContent !== false ? \GuzzleHttp\json_decode($fileContent, true) : null;
}

protected static function normalizeCode($rawCode, $digit)
{
return is_int($rawCode) ? sprintf('%0' . $digit . 'd', $rawCode) : (string)$rawCode;
}

/**
* find bank instance of specified code.
*
* if you not specified bank code, return all bank instances
*
* @param null $bankCode
* @return null|Bank|Bank[]
*/
public static function findBank($bankCode = null)
{
if(!static::$_banks) {
$jsonBanks = static::loadSourceFile('banks.json');
foreach ($jsonBanks as $jsonBank) {
static::$_banks[$jsonBank['code']] = new Bank($jsonBank);
}
}

if($bankCode === null) {
return static::$_banks;
}

$bankCode = static::normalizeCode($bankCode, static::DIGIT_BANK_CODE);

return isset(static::$_banks[$bankCode]) ? static::$_banks[$bankCode] : null;
}

public static function findBranch($bankCode, $branchCode = null)
{
$bankCode = static::normalizeCode($bankCode, static::DIGIT_BANK_CODE);

if(!isset(static::$_branches[$bankCode])) {
$jsonBranches = static::loadSourceFile('branches' . DIRECTORY_SEPARATOR . $bankCode . '.json');
if($jsonBranches) {
$branches = [];
foreach ($jsonBranches as $jsonBranch) {
$branches[$jsonBranch['code']] = new Branch($jsonBranch);
}

static::$_branches[$bankCode] = $branches;
}
}

if($branchCode === null) {
return static::$_branches[$bankCode];
}

$branchCode = static::normalizeCode($branchCode, static::DIGIT_BRANCH_CODE);

return isset(static::$_branches[$bankCode][$branchCode]) ? static::$_branches[$bankCode][$branchCode] : null;
}
}
113 changes: 113 additions & 0 deletions tests/ZenginCode/ZenginCodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
namespace ZenginCode;

use PHPUnit\Framework\TestCase;

class ZenginCodeTest extends TestCase
{
public function test_getLastUpdatedAt()
{
$this->assertSame(
file_get_contents(ZenginCode::DATA_PATH . DIRECTORY_SEPARATOR . 'updated_at'),
ZenginCode::getLastUpdatedAt()
);
}

public function test_findBank_existsCode()
{
$actualBank = ZenginCode::findBank('0001');

$this->assertSame('0001', $actualBank->code);
$this->assertSame('みずほ', $actualBank->name);
$this->assertSame('ミズホ', $actualBank->kana);
$this->assertSame('みずほ', $actualBank->hira);
$this->assertSame('mizuho', $actualBank->roma);
}

public function test_findBank_notExistsCode()
{
$this->assertNull(ZenginCode::findBank('9999'));
}

public function test_findBank_intCode()
{
$actualBank = ZenginCode::findBank(125);

$this->assertSame('0125', $actualBank->code);
$this->assertSame('七十七', $actualBank->name);
$this->assertSame('シチジユウシチ', $actualBank->kana);
$this->assertSame('しちじゆうしち', $actualBank->hira);
$this->assertSame('shichijiyuushichi', $actualBank->roma);
}

public function test_findBank_all()
{
$banks = ZenginCode::findBank();

$this->assertIsArray($banks);
foreach ($banks as $bank) {
$this->assertInstanceOf('ZenginCode\Model\Bank', $bank);
}
}

public function test_findBranch_existsCode()
{
$actualBranch = ZenginCode::findBranch('0001', '001');
$this->assertSame('001', $actualBranch->code);
$this->assertSame('東京営業部', $actualBranch->name);
$this->assertSame('トウキヨウ', $actualBranch->kana);
$this->assertSame('とうきよう', $actualBranch->hira);
$this->assertSame('toukiyou', $actualBranch->roma);
}

/**
* @depends test_findBranch_existsCode
*/
public function test_findBranch_anotherExistsCode()
{
$actualBranch = ZenginCode::findBranch('0005', '001');
$this->assertSame('001', $actualBranch->code);
$this->assertSame('本店', $actualBranch->name);
$this->assertSame('ホンテン', $actualBranch->kana);
$this->assertSame('ほんてん', $actualBranch->hira);
$this->assertSame('honten', $actualBranch->roma);
}

/**
* @depends test_findBranch_anotherExistsCode
*/
public function test_findBranch_recallFirstExitsCode()
{
$actualBranch = ZenginCode::findBranch('0001', '001');
$this->assertSame('001', $actualBranch->code);
$this->assertSame('東京営業部', $actualBranch->name);
$this->assertSame('トウキヨウ', $actualBranch->kana);
$this->assertSame('とうきよう', $actualBranch->hira);
$this->assertSame('toukiyou', $actualBranch->roma);
}

public function test_findBranch_notExistsCode()
{
$this->assertNull(ZenginCode::findBranch('0001', '999'));
}

public function test_findBranch_notExistsBank()
{
try {
$this->assertNull(ZenginCode::findBranch('9999', '001'));
} catch (\Throwable $t) {
$this->fail();
}
}

public function test_findBranch_all()
{
$branches = ZenginCode::findBranch('0001');

$this->assertIsArray($branches);
foreach ($branches as $branch) {
$this->assertInstanceOf('ZenginCode\Model\Branch', $branch);
}
}

}
4 changes: 4 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
error_reporting(E_ALL);

require_once 'vendor/autoload.php';