      1: <?php
      2: /*
      3:
      4: MIT License
      5: Copyright 2013-2019 Zordius Chen. All Rights Reserved.
      6: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
      7: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
      8: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      9:
     10: Origin: https://github.com/zordius/lightncandy
     11: */
     12:
     13: /**
     14:  * file to keep LightnCandy Encoder
     15:  *
     16:  * @package    LightnCandy
     17:  * @author     Zordius <zordius@gmail.com>
     18:  */
     19:
     20: namespace LightnCandy;
     21:
     22: /**
     23:  * LightnCandy class to encode.
     24:  */
     25: class Encoder
     26: {
     27:     /**
     28:      * Get string value
     29:      *
     30:      * @param array<string,array|string|integer> $cx render time context
     31:      * @param array<array|string|integer>|string|integer|null $v value to be output
     32:      * @param integer $ex 1 to return untouched value, default is 0
     33:      *
     34:      * @return array<array|string|integer>|string|integer|null The raw value of the specified variable
     35:      *
     36:      * @expect true when input array('flags' => array('jstrue' => 0, 'mustlam' => 0, 'lambda' => 0)), true
     37:      * @expect 'true' when input array('flags' => array('jstrue' => 1)), true
     38:      * @expect '' when input array('flags' => array('jstrue' => 0, 'mustlam' => 0, 'lambda' => 0)), false
     39:      * @expect 'false' when input array('flags' => array('jstrue' => 1)), false
     40:      * @expect false when input array('flags' => array('jstrue' => 1)), false, true
     41:      * @expect 'Array' when input array('flags' => array('jstrue' => 1, 'jsobj' => 0)), array('a', 'b')
     42:      * @expect 'a,b' when input array('flags' => array('jstrue' => 1, 'jsobj' => 1, 'mustlam' => 0, 'lambda' => 0)), array('a', 'b')
     43:      * @expect '[object Object]' when input array('flags' => array('jstrue' => 1, 'jsobj' => 1)), array('a', 'c' => 'b')
     44:      * @expect '[object Object]' when input array('flags' => array('jstrue' => 1, 'jsobj' => 1)), array('c' => 'b')
     45:      * @expect 'a,true' when input array('flags' => array('jstrue' => 1, 'jsobj' => 1, 'mustlam' => 0, 'lambda' => 0)), array('a', true)
     46:      * @expect 'a,1' when input array('flags' => array('jstrue' => 0, 'jsobj' => 1, 'mustlam' => 0, 'lambda' => 0)), array('a',true)
     47:      * @expect 'a,' when input array('flags' => array('jstrue' => 0, 'jsobj' => 1, 'mustlam' => 0, 'lambda' => 0)), array('a',false)
     48:      * @expect 'a,false' when input array('flags' => array('jstrue' => 1, 'jsobj' => 1, 'mustlam' => 0, 'lambda' => 0)), array('a',false)
     49:      */
     50:     public static function raw($cx, $v, $ex = 0)
     51:     {
     52:         if ($ex) {
     53:             return $v;
     54:         }
     55:
     56:         if ($v === true) {
     57:             if ($cx['flags']['jstrue']) {
     58:                 return 'true';
     59:             }
     60:         }
     61:
     62:         if (($v === false)) {
     63:             if ($cx['flags']['jstrue']) {
     64:                 return 'false';
     65:             }
     66:         }
     67:
     68:         if (is_array($v)) {
     69:             if ($cx['flags']['jsobj']) {
     70:                 if (count(array_diff_key($v, array_keys(array_keys($v)))) > 0) {
     71:                     return '[object Object]';
     72:                 } else {
     73:                     $ret = array();
     74:                     foreach ($v as $k => $vv) {
     75:                         $ret[] = static::raw($cx, $vv);
     76:                     }
     77:                     return join(',', $ret);
     78:                 }
     79:             } else {
     80:                 return 'Array';
     81:             }
     82:         }
     83:
     84:         return "$v";
     85:     }
     86:
     87:     /**
     88:      * Get html encoded string
     89:      *
     90:      * @param array<string,array|string|integer> $cx render time context
     91:      * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded
     92:      *
     93:      * @return string The htmlencoded value of the specified variable
     94:      *
     95:      * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a'
     96:      * @expect 'a&amp;b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b'
     97:      * @expect 'a&#039;b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b'
     98:      */
     99:     public static function enc($cx, $var)
    100:     {
    101:         return htmlspecialchars(static::raw($cx, $var), ENT_QUOTES, 'UTF-8');
    102:     }
    103:
    104:     /**
    105:      * LightnCandy runtime method for {{var}} , and deal with single quote to same as handlebars.js .
    106:      *
    107:      * @param array<string,array|string|integer> $cx render time context
    108:      * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded
    109:      *
    110:      * @return string The htmlencoded value of the specified variable
    111:      *
    112:      * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a'
    113:      * @expect 'a&amp;b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b'
    114:      * @expect 'a&#x27;b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b'
    115:      * @expect '&#x60;a&#x27;b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), '`a\'b'
    116:      */
    117:     public static function encq($cx, $var)
    118:     {
    119:         return str_replace(array('=', '`', '&#039;'), array('&#x3D;', '&#x60;', '&#x27;'), htmlspecialchars(static::raw($cx, $var), ENT_QUOTES, 'UTF-8'));
    120:     }
    121: }
    122: