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 handle LightnCandy token
15: *
16: * @package LightnCandy
17: * @author Zordius <zordius@gmail.com>
18: */
19:
20: namespace LightnCandy;
21:
22: /**
23: * LightnCandy Token handler
24: */
25: class Token
26: {
27: // RegExps
28: const VARNAME_SEARCH = '/(\\[[^\\]]+\\]|[^\\[\\]\\.]+)/';
29:
30: // Positions of matched token
31: const POS_LOTHER = 1;
32: const POS_LSPACE = 2;
33: const POS_BEGINTAG = 3;
34: const POS_LSPACECTL = 4;
35: const POS_BEGINRAW = 5;
36: const POS_OP = 6;
37: const POS_INNERTAG = 7;
38: const POS_ENDRAW = 8;
39: const POS_RSPACECTL = 9;
40: const POS_ENDTAG = 10;
41: const POS_RSPACE = 11;
42: const POS_ROTHER = 12;
43: const POS_BACKFILL = 13;
44:
45: /**
46: * Setup delimiter by default or provided string
47: *
48: * @param array<string,array|string|integer> $context Current context
49: * @param string|null $left left string of a token
50: * @param string|null $right right string of a token
51: */
52: public static function setDelimiter(&$context, $left = null, $right = null)
53: {
54: if ($left === null) {
55: $left = $context['delimiters'][0];
56: }
57: if ($right === null) {
58: $right = $context['delimiters'][1];
59: }
60: if (preg_match('/=/', "$left$right")) {
61: $context['error'][] = "Can not set delimiter contains '=' , you try to set delimiter as '$left' and '$right'.";
62: return;
63: }
64:
65: $context['tokens']['startchar'] = substr($left, 0, 1);
66: $context['tokens']['left'] = $left;
67: $context['tokens']['right'] = $right;
68: $rawcount = $context['rawblock'] ? '{2}' : ($context['flags']['rawblock'] ? '{0,2}' : '?');
69: $left = preg_quote($left);
70: $right = preg_quote($right);
71:
72: $context['tokens']['search'] = "/^(.*?)(\\s*)($left)(~?)(\\{{$rawcount})\\s*([\\^#\\/!&>\\*]{0,2})(.*?)\\s*(\\}{$rawcount})(~?)($right)(\\s*)(.*)\$/s";
73: }
74:
75: /**
76: * return token string
77: *
78: * @param string[] $token detected handlebars {{ }} token
79: * @param string[]|null $merge list of token strings to be merged
80: *
81: * @return string Return whole token
82: *
83: * @expect 'c' when input array(0, 'a', 'b', 'c', 'd', 'e')
84: * @expect 'cd' when input array(0, 'a', 'b', 'c', 'd', 'e', 'f')
85: * @expect 'qd' when input array(0, 'a', 'b', 'c', 'd', 'e', 'f'), array(3 => 'q')
86: */
87: public static function toString($token, $merge = null)
88: {
89: if (is_array($merge)) {
90: $token = array_replace($token, $merge);
91: }
92: return implode('', array_slice($token, 3, -2));
93: }
94: }
95: