<?php
namespace Qii\Library;

/**
 * 中文数字和阿拉伯数字相互转换
 * Chinese2digits::SectionToChinese(1111);
 * Chinese2digits::NumberToChinese(1111)
 * Chinese2digits::ChineseToNumber("一千一百一十一")
 */
class Chinese2digits {
    /**
     * 最多4位数
     *
     * @param int $section
     * @return string
     */
    public static function SectionToChinese($section) {
        $chnNumChar = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
        $chnUnitChar = ["", "十", "百", "千"];
        $chnStr = "";
        $unitPos = 0;
        $zero = true;
        while(true) {
            if ($section <= 0) {
                break;
            }
            $v = intval($section) % 10;
            if ($v == 0) {
                if (!$zero) {
                    $zero = true;
                    $chnStr = $chnNumChar[v] . $chnStr;
                }
            } else {
                $zero = false;
                $strIns = $chnNumChar[$v];
                $strIns .= $chnUnitChar[$unitPos];
                $chnStr = $strIns . $chnStr;
            }
            $unitPos++;
            $section = intval($section / 10);
        }
        return $chnStr;
    }

    /**
     * 数字转中文
     *
     * @param int $num 数字
     * @return string
     */
    public static function NumberToChinese($num) {
        $chnNumChar = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
        $chnUnitSection = ["", "万", "亿", "万亿", "亿亿"];

        $unitPos = 0;
        $chnStr = "";
        $needZero = false;

        if ($num == 0) {
            return $chnNumChar[0];
        }
        while (true) {
            if ($num <= 0) {
                break;
            }
            $section = $num % 10000;
            if ($needZero) {
                $chnStr = $chnNumChar[0] . $chnStr;
            }
            $strIns = self::SectionToChinese($section);
            if ($section != 0) {
                $strIns .= $chnUnitSection[$unitPos];
            } else {
                $strIns .= $chnUnitSection[0];
            }

            $chnStr = $strIns . $chnStr;
            $needZero = ($section < 1000) && ($section > 0);
            $num = intval($num / 10000);
            $unitPos++;
        }
        return $chnStr;
    }

    /**
     * 中文字符串数字
     *
     * @param string $chnStr 中文字符
     * @return float|int
     */
    public static function ChineseToNumber($chnStr) {
        if(preg_match("/^十/", $chnStr)) {
            $chnStr = "一". $chnStr;
        }
        $chnNumCharDict = ["零" => 0, "一" => 1, "二" => 2, "三" => 3, "四" => 4, "五" => 5, "六" => 6, "七" => 7, "八" => 8, "九" => 9];
        $chnNameValue = ["十" => ["value" => 10, "secUnit" => false]
            , "百" => ["value" => 100, "secUnit" => false]
            , "千" => ["value" => 1000, "secUnit" => false]
            , "万" => ["value" => 10000, "secUnit" => true]
            , "亿" => ["value" => 100000000, "secUnit" => true]
        ];
        $rtn = 0;
        $section = 0;
        $number = 0;
        $str = preg_split("/(?<!^)(?!$)/u", $chnStr);
        for ($i = 0; $i < count($str); $i++) {
            $num = $chnNumCharDict[$str[$i]];
            if ($num != 0) {
                $number = $num;
                if ($i == count($str)-1) {
                    $section += $number;
                }
            } else {
                $units = $chnNameValue[$str[$i]]['value'];
                $secUnit = $chnNameValue[$str[$i]]['secUnit'];
                if ($secUnit) {
                    $section = ($section + $number) * $units;
                    $rtn += $section;
                    $section = 0;
                } else {
                    $section += ($number * $units);
                }
                $number = 0;
            }
        }
        return $rtn + $section;
    }
}