-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from lichunqiang/patch-1
formatted code style base on psr-2
- Loading branch information
Showing
1 changed file
with
52 additions
and
42 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
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; | ||
} | ||
} | ||
|