      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 of LightnCandy Expression handler
     15:  *
     16:  * @package    LightnCandy
     17:  * @author     Zordius <zordius@gmail.com>
     18:  */
     19:
     20: namespace LightnCandy;
     21:
     22: /**
     23:  * LightnCandy Expression handler
     24:  */
     25: class Expression
     26: {
     27:     /**
     28:      * return 'true' or 'false' string.
     29:      *
     30:      * @param integer $v value
     31:      *
     32:      * @return string 'true' when the value larger then 0
     33:      *
     34:      * @expect 'true' when input 1
     35:      * @expect 'true' when input 999
     36:      * @expect 'false' when input 0
     37:      * @expect 'false' when input -1
     38:      */
     39:     public static function boolString($v)
     40:     {
     41:         return ($v > 0) ? 'true' : 'false';
     42:     }
     43:
     44:     /**
     45:      * Get string presentation for a string list
     46:      *
     47:      * @param array<string> $list an array of strings.
     48:      *
     49:      * @return string PHP list string
     50:      *
     51:      * @expect '' when input array()
     52:      * @expect "'a'" when input array('a')
     53:      * @expect "'a','b','c'" when input array('a', 'b', 'c')
     54:      */
     55:     public static function listString($list)
     56:     {
     57:         return implode(',', (array_map(function ($v) {
     58:             return "'$v'";
     59:         }, $list)));
     60:     }
     61:
     62:     /**
     63:      * Get string presentation for an array
     64:      *
     65:      * @param array<string> $list an array of variable names.
     66:      *
     67:      * @return string PHP array names string
     68:      *
     69:      * @expect '' when input array()
     70:      * @expect "['a']" when input array('a')
     71:      * @expect "['a']['b']['c']" when input array('a', 'b', 'c')
     72:      */
     73:     public static function arrayString($list)
     74:     {
     75:         return implode('', (array_map(function ($v) {
     76:             return "['$v']";
     77:         }, $list)));
     78:     }
     79:
     80:     /**
     81:      * Analyze an expression
     82:      *
     83:      * @param array<string,array|string|integer> $context Current context
     84:      * @param array<array|string|integer> $var variable parsed path
     85:      *
     86:      * @return array<integer|boolean|array> analyzed result
     87:      *
     88:      * @expect array(0, false, array('foo')) when input array('flags' => array('spvar' => 0)), array(0, 'foo')
     89:      * @expect array(1, false, array('foo')) when input array('flags' => array('spvar' => 0)), array(1, 'foo')
     90:      */
     91:     public static function analyze($context, $var)
     92:     {
     93:         $levels = 0;
     94:         $spvar = false;
     95:
     96:         if (isset($var[0])) {
     97:             // trace to parent
     98:             if (!is_string($var[0]) && is_int($var[0])) {
     99:                 $levels = array_shift($var);
    100:             }
    101:         }
    102:
    103:         if (isset($var[0])) {
    104:             // handle @root, @index, @key, @last, etc
    105:             if ($context['flags']['spvar']) {
    106:                 if (substr($var[0], 0, 1) === '@') {
    107:                     $spvar = true;
    108:                     $var[0] = substr($var[0], 1);
    109:                 }
    110:             }
    111:         }
    112:
    113:         return array($levels, $spvar, $var);
    114:     }
    115:
    116:     /**
    117:      * get normalized handlebars expression for a variable
    118:      *
    119:      * @param integer $levels trace N levels top parent scope
    120:      * @param boolean $spvar is the path start with @ or not
    121:      * @param array<string|integer> $var variable parsed path
    122:      *
    123:      * @return string normalized expression for debug display
    124:      *
    125:      * @expect '[a].[b]' when input 0, false, array('a', 'b')
    126:      * @expect '@[root]' when input 0, true, array('root')
    127:      * @expect 'this' when input 0, false, null
    128:      * @expect 'this.[id]' when input 0, false, array(null, 'id')
    129:      * @expect '@[root].[a].[b]' when input 0, true, array('root', 'a', 'b')
    130:      * @expect '../../[a].[b]' when input 2, false, array('a', 'b')
    131:      * @expect '../[a\'b]' when input 1, false, array('a\'b')
    132:      */
    133:     public static function toString($levels, $spvar, $var)
    134:     {
    135:         return ($spvar ? '@' : '') . str_repeat('../', $levels) . ((is_array($var) && count($var)) ? implode('.', array_map(function ($v) {
    136:             return ($v === null) ? 'this' : "[$v]";
    137:         }, $var)) : 'this');
    138:     }
    139: }
    140: