| 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 string utilities | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @package    LightnCandy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @author     Zordius <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | namespace LightnCandy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use \LightnCandy\Encoder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * LightnCandy SafeString class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | class SafeString extends Encoder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     const EXTENDED_COMMENT_SEARCH = '/{{!--.*?--}}/s'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     const IS_SUBEXP_SEARCH = '/^\(.+\)$/s'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     const IS_BLOCKPARAM_SEARCH = '/^ +\|(.+)\|$/s'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private $string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public static $jsContext = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         'flags' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             'jstrue' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             'jsobj' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * Constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param string $str input string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param bool|string $escape false to not escape, true to escape, 'encq' to escape as handlebars.js | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @return string Stripped template | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @expect 'abc' when input 'abc' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @expect 'abc{{!}}cde' when input 'abc{{!}}cde' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @expect 'abc{{! }}cde' when input 'abc{{!----}}cde' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 4 |  |     function __construct($str, $escape = false) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 4 |  |         $this->string = $escape ? (($escape === 'encq') ? static::encq(static::$jsContext, $str) : static::enc(static::$jsContext, $str)) : $str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 4 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 4 |  |     function __toString() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 4 |  |         return $this->string; | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * Strip extended comments {{!-- .... --}} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param string $template handlebars template string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @return string Stripped template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @expect 'abc' when input 'abc' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @expect 'abc{{!}}cde' when input 'abc{{!}}cde' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @expect 'abc{{! }}cde' when input 'abc{{!----}}cde' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 718 |  |     public static function stripExtendedComments($template) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 718 |  |         return preg_replace(static::EXTENDED_COMMENT_SEARCH, '{{! }}', $template); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * Escape template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @param string $template handlebars template string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @return string Escaped template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @expect 'abc' when input 'abc' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @expect 'a\\\\bc' when input 'a\bc' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @expect 'a\\\'bc' when input 'a\'bc' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 718 |  |     public static function escapeTemplate($template) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 718 |  |         return addcslashes(addcslashes($template, '\\'), "'"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |  | 
            
                        
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.