| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | Copyrights for code authored by Yahoo! Inc. is licensed under the following terms: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | MIT License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | Copyright (c) 2013-2015 Yahoo! Inc. All Rights Reserved. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | 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: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | 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. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | Origin: https://github.com/zordius/lightncandy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * file to keep LightnCandy Encoder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @package    LightnCandy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @author     Zordius <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | namespace LightnCandy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * LightnCandy class to encode. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | class Encoder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * Get string value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param array<string,array|string|integer> $cx render time context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param array<array|string|integer>|string|integer|null $v value to be output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @return string 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 | 395 |  |     public static function raw($cx, $v) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 395 |  |         if ($v === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 2 |  |             if ($cx['flags']['jstrue']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  |                 return 'true'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 395 |  |         if (($v === false)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 6 |  |             if ($cx['flags']['jstrue']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 6 |  |                 return 'false'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 390 |  |         if (is_array($v)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 6 |  |             if ($cx['flags']['jsobj']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 6 |  |                 if (count(array_diff_key($v, array_keys(array_keys($v)))) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 2 |  |                     return '[object Object]'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 5 |  |                     $ret = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 5 |  |                     foreach ($v as $k => $vv) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 5 |  |                         $ret[] = static::raw($cx, $vv); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 5 |  |                     return join(',', $ret); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |                 return 'Array'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 390 |  |         return "$v"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * Get html encoded string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param array<string,array|string|integer> $cx render time context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @return string The htmlencoded value of the specified variable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @expect 'a&b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @expect 'a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b' | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 94 | 1 |  |     public static function enc($cx, $var) { | 
            
                                                                        
                            
            
                                    
            
            
                | 95 | 1 |  |         return htmlentities(static::raw($cx, $var), ENT_QUOTES, 'UTF-8'); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * LightnCandy runtime method for {{var}} , and deal with single quote to same as handlebars.js . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param array<string,array|string|integer> $cx render time context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @return string The htmlencoded value of the specified variable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @expect 'a&b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @expect 'a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @expect '`a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), '`a\'b' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 1 |  |     public static function encq($cx, $var) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 1 |  |         return str_replace(array('=', '`', '''), array('=', '`', '''), htmlentities(static::raw($cx, $var), ENT_QUOTES, 'UTF-8')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |  |