Skip to content

Commit

Permalink
refactor and support fullwidth
Browse files Browse the repository at this point in the history
  • Loading branch information
Recca Tsai committed Jun 17, 2016
1 parent 30df1be commit 44d8f3c
Show file tree
Hide file tree
Showing 7 changed files with 1,127 additions and 772 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ $twzipcode->getCounty(); // 新北市
$twzipcode->getDistrict(); // 萬里區
$twzipcode->getAddress(); // 新北市萬里區中正路100號
$twzipcode->getShortAddress(); // 中正路100號

# 取得全形字串
$twzipcode->getZipcode(true); // '207'
$twzipcode->getCounty(true); // '新北市'
$twzipcode->getDistrict(true); // '萬里區'
$twzipcode->getAddress(true); // '新北市萬里區龜港村中正路100號'
$twzipcode->getShortAddress(true); // '龜港村中正路100號'
```
165 changes: 165 additions & 0 deletions src/Converter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

namespace Recca0120\Twzipcode;

class Converter
{
/**
* $charMap.
*
* @var array
*/
private static $charMap = [
' ' => ' ',
'' => '!',
'' => '"',
'' => '#',
'' => '$',
'' => '%',
'' => '&',
'' => "'",
'' => '(',
'' => ')',
'' => '*',
'' => '+',
'' => ',',
'' => '-',
'' => '.',
'' => '/',
'' => '0',
'' => '1',
'' => '2',
'' => '3',
'' => '4',
'' => '5',
'' => '6',
'' => '7',
'' => '8',
'' => '9',
'' => ':',
'' => ';',
'' => '<',
'' => '=',
'' => '>',
'' => '?',
'' => '@',
'' => 'A',
'' => 'B',
'' => 'C',
'' => 'D',
'' => 'E',
'' => 'F',
'' => 'G',
'' => 'H',
'' => 'I',
'' => 'J',
'' => 'K',
'' => 'L',
'' => 'M',
'' => 'N',
'' => 'O',
'' => 'P',
'' => 'Q',
'' => 'R',
'' => 'S',
'' => 'T',
'' => 'U',
'' => 'V',
'' => 'W',
'' => 'X',
'' => 'Y',
'' => 'Z',
'' => '[',
'' => '\\',
'' => ']',
'' => '^',
'_' => '_',
'' => '`',
'' => 'a',
'' => 'b',
'' => 'c',
'' => 'd',
'' => 'e',
'' => 'f',
'' => 'g',
'' => 'h',
'' => 'i',
'' => 'j',
'' => 'k',
'' => 'l',
'' => 'm',
'' => 'n',
'' => 'o',
'' => 'p',
'' => 'q',
'' => 'r',
'' => 's',
'' => 't',
'' => 'u',
'' => 'v',
'' => 'w',
'' => 'x',
'' => 'y',
'' => 'z',
'' => '{',
'' => '|',
'' => '}',
'' => '~',
];

/**
* $str.
*
* @var string
*/
private $str;

public function __construct($str)
{
$this->str = $str;
}

/**
* full.
*
* @return string
*/
public function full()
{
return strtr($this->str, array_flip(self::$charMap));
}

/**
* half.
*
* @return string
*/
public function half()
{
return strtr($this->str, self::$charMap);
}

/**
* toFull.
*
* @param string $str
*
* @return string
*/
public static function toFull($str)
{
return (new static($str))->full();
}

/**
* toHalf.
*
* @param string $str
*
* @return string
*/
public static function toHalf($str)
{
return (new static($str))->half();
}
}
Loading

0 comments on commit 44d8f3c

Please sign in to comment.