<?php
/**
 * Link         :   http://www.phpcorner.net
 * User         :   qingbing<780042175@qq.com>
 * Date         :   2018-10-27
 * Version      :   1.0
 */

namespace Helper;

/**
 * Class Coding
 * @package Helper
 */
class Coding
{
    /**
     * base64 码的替换字符
     * @var array
     */
    private static $_base64Pattern = [
        '0' => '00',
        '+' => '01',
        '/' => '02',
        '=' => '03',
    ];

    /**
     * base64 编码
     * @param string $var
     * @return string
     */
    public static function base64_encode($var)
    {
        return strtr(base64_encode($var), self::$_base64Pattern);
    }

    /**
     * base64 解码
     * @param string $var
     * @return string
     */
    public static function base64_decode($var)
    {
        return base64_decode(strtr($var, array_flip(self::$_base64Pattern)));
    }

    /**
     * 安全编码
     * @param mixed $var
     * @return string
     */
    public static function secure_encode($var)
    {
        return self::base64_encode(serialize($var));
    }

    /**
     * 安全解码
     * @param string $var
     * @return mixed
     */
    public static function secure_decode($var)
    {
        return unserialize(self::base64_decode($var));
    }

    /**
     * 把变量json串化
     * @param mixed $var
     * @param bool $isCustom
     * @return string
     */
    public static function json_encode($var, $isCustom = false)
    {
        if ($isCustom) {
            return self::_json_encode($var, $isCustom);
        }
        return json_encode($var);
    }

    private static function _json_encode($var, $isCustom)
    {
        if (null === $var)
            return 'null';
        if (is_bool($var))
            return $var ? "true" : "false";
        if (is_integer($var))
            return "{$var}";
        if (is_float($var))
            return "{$var}";
        if (is_string($var)) {
            return '"' . addslashes($var) . '"';
        }
        if (is_array($var)) {
            if (0 === count($var))
                return '[]';
            if (array_keys($var) === range(0, sizeof($var) - 1)) {
                $_t = [];
                foreach ($var as $v) {
                    array_push($_t, self::_json_encode($v, $isCustom));
                }
                return '[' . implode(',', $_t) . ']';
            } else {
                $_t = [];
                foreach ($var as $k => $v) {
                    $_t[] = '"' . $k . '"' . ':' . self::_json_encode($v, $isCustom);
                }
                return '{' . implode(',', $_t) . '}';
            }
        }
        if (is_object($var)) {
            $var = get_object_vars($var);
            if (0 === count($var))
                return '{}';
            $_t = [];
            foreach ($var as $k => $v) {
                $_t[] = '"' . $k . '"' . ':' . self::_json_encode($v, $isCustom);
            }
            return '{' . implode(',', $_t) . '}';
        }
        return '* UNKNOWN *';
    }

    /**
     * json 解码
     * @param string $var
     * @param bool $useArray
     * @return mixed
     */
    public static function json_decode($var, $useArray = true)
    {
        return json_decode($var, $useArray);
    }
}