Skip to content

Commit

Permalink
Merge pull request #3 from lichunqiang/patch-1
Browse files Browse the repository at this point in the history
formatted code style base on psr-2
  • Loading branch information
shen2 committed Mar 13, 2015
2 parents cbb567e + b0c5544 commit 4b999fa
Showing 1 changed file with 52 additions and 42 deletions.
94 changes: 52 additions & 42 deletions src/GB2260.php
Original file line number Diff line number Diff line change
@@ -1,43 +1,53 @@
<?php
namespace cn;

class GB2260{
protected static $_data;

public static function getData(){
if (empty(self::$_data))
self::$_data = require 'data.php';
return self::$_data;
}

public static function parse($code){
if (empty(self::$_data))
self::$_data = require 'data.php';

$code = preg_replace('/0+$/', '', $code);
$codeLength = strlen($code);
if ($codeLength < 2 || $codeLength > 6 || $codeLength % 2 !== 0) {
throw new \Exception('Invalid code');
}

$province = self::$_data[substr($code, 0, 2) . '0000'];
if (!$province) return null;

if ($codeLength === 2) {
return $province;
}

$area = self::$_data[substr($code, 0, 4) . '00'];
if (!$area) return null;

if ($codeLength === 4) {
return $province . ' ' . $area;
}

$name = self::$_data[$code];
if (!$name) return null;

return $province . ' ' . $area . ' ' . $name;
}
<?php namespace cn;

class GB2260
{
protected static $_data;

public static function getData()
{
if (empty(self::$_data)) {
self::$_data = require 'data.php';
}

return self::$_data;
}

public static function parse($code)
{
if (empty(self::$_data)) {
self::$_data = require 'data.php';
}

$code = preg_replace('/0+$/', '', $code);
$codeLength = strlen($code);
if ($codeLength < 2 || $codeLength > 6 || $codeLength % 2 !== 0) {
throw new \Exception('Invalid code');
}

$province = self::$_data[substr($code, 0, 2) . '0000'];
if (!$province) {
return null;
}

if ($codeLength === 2) {
return $province;
}

$area = self::$_data[substr($code, 0, 4) . '00'];
if (!$area) {
return null;
}

if ($codeLength === 4) {
return $province . ' ' . $area;
}

$name = self::$_data[$code];
if (!$name) {
return null;
}

return $province . ' ' . $area . ' ' . $name;
}
}

0 comments on commit 4b999fa

Please sign in to comment.