Rumah php教程 PHP源码 重写YII的CJSON为WJSON输出model支持自定义变量

重写YII的CJSON为WJSON输出model支持自定义变量

May 23, 2016 am 08:39 AM

重写YII的CJSON为WJSON输出model支持自定义变量
因YII的CJSON输出model时只能输出attributes变量,不能输出自定义变量,重新此类使之可以输出自定义变量

<?php
 
/**
 * @author PHP中文网
 * 重写YII的CJSON为WJSON输出model支持自定义变量
 * Class WJSON
 */
class WJSON extends CJSON {
    protected static function nameValue($name, $value) {
        return self::encode(strval($name)) . &#39;:&#39; . self::encode($value);
    }
 
    public static function encode($var) {
        switch (gettype($var)) {
            case &#39;boolean&#39;:
                return $var ? &#39;true&#39; : &#39;false&#39;;
 
            case &#39;NULL&#39;:
                return &#39;null&#39;;
 
            case &#39;integer&#39;:
                return (int)$var;
 
            case &#39;double&#39;:
            case &#39;float&#39;:
                return str_replace(&#39;,&#39;, &#39;.&#39;, (float)$var); // locale-independent representation
 
            case &#39;string&#39;:
                if (($enc = strtoupper(Yii::app()->charset)) !== &#39;UTF-8&#39;)
                    $var = iconv($enc, &#39;UTF-8&#39;, $var);
 
                if (function_exists(&#39;json_encode&#39;))
                    return json_encode($var);
 
                // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT
                $ascii = &#39;&#39;;
                $strlen_var = strlen($var);
 
                /*
                 * Iterate over every character in the string,
                 * escaping with a slash or encoding to UTF-8 where necessary
                 */
                for ($c = 0; $c < $strlen_var; ++$c) {
 
                    $ord_var_c = ord($var{$c});
 
                    switch (true) {
                        case $ord_var_c == 0x08:
                            $ascii .= &#39;\b&#39;;
                            break;
                        case $ord_var_c == 0x09:
                            $ascii .= &#39;\t&#39;;
                            break;
                        case $ord_var_c == 0x0A:
                            $ascii .= &#39;\n&#39;;
                            break;
                        case $ord_var_c == 0x0C:
                            $ascii .= &#39;\f&#39;;
                            break;
                        case $ord_var_c == 0x0D:
                            $ascii .= &#39;\r&#39;;
                            break;
 
                        case $ord_var_c == 0x22:
                        case $ord_var_c == 0x2F:
                        case $ord_var_c == 0x5C:
                            // double quote, slash, slosh
                            $ascii .= &#39;\\&#39; . $var{$c};
                            break;
 
                        case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)):
                            // characters U-00000000 - U-0000007F (same as ASCII)
                            $ascii .= $var{$c};
                            break;
 
                        case (($ord_var_c & 0xE0) == 0xC0):
                            // characters U-00000080 - U-000007FF, mask 110XXXXX
                            // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
                            $char = pack(&#39;C*&#39;, $ord_var_c, ord($var{$c + 1}));
                            $c += 1;
                            $utf16 = self::utf8ToUTF16BE($char);
                            $ascii .= sprintf(&#39;\u%04s&#39;, bin2hex($utf16));
                            break;
 
                        case (($ord_var_c & 0xF0) == 0xE0):
                            // characters U-00000800 - U-0000FFFF, mask 1110XXXX
                            // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
                            $char = pack(&#39;C*&#39;, $ord_var_c,
                                ord($var{$c + 1}),
                                ord($var{$c + 2}));
                            $c += 2;
                            $utf16 = self::utf8ToUTF16BE($char);
                            $ascii .= sprintf(&#39;\u%04s&#39;, bin2hex($utf16));
                            break;
 
                        case (($ord_var_c & 0xF8) == 0xF0):
                            // characters U-00010000 - U-001FFFFF, mask 11110XXX
                            // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
                            $char = pack(&#39;C*&#39;, $ord_var_c,
                                ord($var{$c + 1}),
                                ord($var{$c + 2}),
                                ord($var{$c + 3}));
                            $c += 3;
                            $utf16 = self::utf8ToUTF16BE($char);
                            $ascii .= sprintf(&#39;\u%04s&#39;, bin2hex($utf16));
                            break;
 
                        case (($ord_var_c & 0xFC) == 0xF8):
                            // characters U-00200000 - U-03FFFFFF, mask 111110XX
                            // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
                            $char = pack(&#39;C*&#39;, $ord_var_c,
                                ord($var{$c + 1}),
                                ord($var{$c + 2}),
                                ord($var{$c + 3}),
                                ord($var{$c + 4}));
                            $c += 4;
                            $utf16 = self::utf8ToUTF16BE($char);
                            $ascii .= sprintf(&#39;\u%04s&#39;, bin2hex($utf16));
                            break;
 
                        case (($ord_var_c & 0xFE) == 0xFC):
                            // characters U-04000000 - U-7FFFFFFF, mask 1111110X
                            // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
                            $char = pack(&#39;C*&#39;, $ord_var_c,
                                ord($var{$c + 1}),
                                ord($var{$c + 2}),
                                ord($var{$c + 3}),
                                ord($var{$c + 4}),
                                ord($var{$c + 5}));
                            $c += 5;
                            $utf16 = self::utf8ToUTF16BE($char);
                            $ascii .= sprintf(&#39;\u%04s&#39;, bin2hex($utf16));
                            break;
                    }
                }
 
                return &#39;"&#39; . $ascii . &#39;"&#39;;
 
            case &#39;array&#39;:
 
                // treat as a JSON object
                if (is_array($var) && count($var) && (array_keys($var) !== range(0, sizeof($var) - 1))) {
                    return &#39;{&#39; .
                    join(&#39;,&#39;, array_map(array(&#39;WJSON&#39;, &#39;nameValue&#39;),
                        array_keys($var),
                        array_values($var)))
                    . &#39;}&#39;;
                }
 
                // treat it like a regular array
                return &#39;[&#39; . join(&#39;,&#39;, array_map(array(&#39;WJSON&#39;, &#39;encode&#39;), $var)) . &#39;]&#39;;
 
            case &#39;object&#39;:
                if ($var instanceof Traversable) {
                    $vars = array();
                    foreach ($var as $k => $v)
                        $vars[$k] = $v;
                } else
                    $vars = get_object_vars($var);
 
                // related
                foreach ($var->relations() as $key => $related) {
                    if ($var->hasRelated($key)) {
                        $vars[$key] = $var->$key;
                    }
                }
 
                return &#39;{&#39; .
                join(&#39;,&#39;, array_map(array(&#39;WJSON&#39;, &#39;nameValue&#39;),
                    array_keys($vars),
                    array_values($vars)))
                . &#39;}&#39;;
 
            default:
                return &#39;&#39;;
        }
    }
 
}
Salin selepas log masuk

                       


Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn

Alat AI Hot

Undresser.AI Undress

Undresser.AI Undress

Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover

AI Clothes Remover

Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool

Undress AI Tool

Gambar buka pakaian secara percuma

Clothoff.io

Clothoff.io

Penyingkiran pakaian AI

AI Hentai Generator

AI Hentai Generator

Menjana ai hentai secara percuma.

Alat panas

Notepad++7.3.1

Notepad++7.3.1

Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina

SublimeText3 versi Cina

Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1

Hantar Studio 13.0.1

Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6

Dreamweaver CS6

Alat pembangunan web visual

SublimeText3 versi Mac

SublimeText3 versi Mac

Perisian penyuntingan kod peringkat Tuhan (SublimeText3)