This repository was archived by the owner on Jan 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathArguments.php
More file actions
201 lines (179 loc) · 5.62 KB
/
Copy pathArguments.php
File metadata and controls
201 lines (179 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* This file is part of Handlebars-php
*
* PHP version 5.3
*
* @category Xamin
* @package Handlebars
* @author Dmitriy Simushev <simushevds@gmail.com>
* @copyright 2014 Authors
* @license MIT <http://opensource.org/licenses/MIT>
* @version GIT: $Id$
* @link http://xamin.ir
*/
namespace Handlebars;
/**
* Encapsulates helpers arguments.
*
* @category Xamin
* @package Handlebars
* @author Dmitriy Simushev <simushevds@gmail.com>
* @copyright 2014 Authors
* @license MIT <http://opensource.org/licenses/MIT>
* @version Release: @package_version@
* @link http://xamin.ir
*/
class Arguments
{
/**
* List of named arguments.
*
* @var array
*/
protected $namedArgs = array();
/**
* List of positional arguments.
*
* @var array
*/
protected $positionalArgs = array();
/**
* The original arguments string that was used to fill in arguments.
*
* @var string
*/
protected $originalString = '';
/**
* Class constructor.
*
* @param string $args_string Arguments string as passed to a helper.
*/
public function __construct($args_string = false)
{
$this->originalString = (string)$args_string;
if ($this->originalString !== '') {
$this->parse($args_string);
}
}
/**
* Returns string representation of the arguments list.
*
* This method is here mostly for backward compatibility reasons.
*
* @return string
*/
public function __toString()
{
return $this->originalString;
}
/**
* Returns a list of named arguments.
*
* @return array
*/
public function getNamedArguments()
{
return $this->namedArgs;
}
/**
* Returns a list of positional arguments.
*
* @return array
*/
public function getPositionalArguments()
{
return $this->positionalArgs;
}
/**
* Breaks an argument string into arguments and stores them inside the
* object.
*
* @param string $args_string Arguments string as passed to a helper.
*
* @return void
* @throws \InvalidArgumentException
*/
protected function parse($args_string)
{
$bad_chars = preg_quote(Context::NOT_VALID_NAME_CHARS, '#');
$bad_seg_chars = preg_quote(Context::NOT_VALID_SEGMENT_NAME_CHARS, '#');
$name_chunk = '(?:@?[^' . $bad_chars . '\s]+)|(?:\[[^' . $bad_seg_chars . ']+\])';
$variable_name = '(?:\.\.\/)*(?:(?:' . $name_chunk . ')[\.\/])*(?:' . $name_chunk . ')\.?';
$special_variable_name = '@[a-z]+';
$escaped_value = '(?:(?<!\\\\)".*?(?<!\\\\)"|(?<!\\\\)\'.*?(?<!\\\\)\')';
$argument_name = $name_chunk;
$argument_value = $variable_name . '|' . $escaped_value . '|' . $special_variable_name;
$positional_argument = '#^(' . $argument_value . ')#';
$named_argument = '#^(' . $argument_name . ')\s*=\s*(' . $argument_value . ')#';
$current_str = trim($args_string);
// Split arguments string
while (strlen($current_str) !== 0) {
if (preg_match($named_argument, $current_str, $matches)) {
// Named argument found
$name = $this->prepareArgumentName($matches[1]);
$value = $this->prepareArgumentValue($matches[2]);
$this->namedArgs[$name] = $value;
// Remove found argument from arguments string.
$current_str = ltrim(substr($current_str, strlen($matches[0])));
} elseif (preg_match($positional_argument, $current_str, $matches)) {
// A positional argument found. It cannot follow named arguments
if (count($this->namedArgs) !== 0) {
throw new \InvalidArgumentException('Positional arguments cannot follow named arguments');
}
$this->positionalArgs[] = $this->prepareArgumentValue($matches[1]);
// Remove found argument from arguments string.
$current_str = ltrim(substr($current_str, strlen($matches[0])));
} else {
throw new \InvalidArgumentException(
sprintf(
'Malformed arguments string: "%s"',
$args_string
)
);
}
}
}
/**
* Prepares argument's value to add to arguments list.
*
* The method unescapes value and wrap it into \Handlebars\String class if
* needed.
*
* @param string $value Argument's value
*
* @return string|\Handlebars\String
*/
protected function prepareArgumentValue($value)
{
// Check if argument's value is a quoted string literal
if ($value[0] == "'" || $value[0] == '"') {
// Remove enclosing quotes and unescape
return new String(stripcslashes(substr($value, 1, -1)));
}
// Check if the value is an integer literal
if (preg_match("/^-?\d+$/", $value)) {
// Wrap the value into the String class to tell the Context that
// it's a value and not a variable name.
return new String($value);
}
return $value;
}
/**
* Prepares argument's name.
*
* Remove sections braces if needed.
*
* @param strign $name Argument's name
*
* @return string
*/
protected function prepareArgumentName($name)
{
// Check if argument's name is a segment
if ($name[0] == '[') {
$name = substr($name, 1, -1);
}
return $name;
}
}