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