File SafeString.php

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