@@ -31,180 +31,180 @@ |
||
31 | 31 | */ |
32 | 32 | class PluginDump extends Plugin |
33 | 33 | { |
34 | - protected $outputObjects; |
|
35 | - protected $outputMethods; |
|
36 | - |
|
37 | - /** |
|
38 | - * @param string $var |
|
39 | - * @param bool $show_methods |
|
40 | - * |
|
41 | - * @return string |
|
42 | - */ |
|
43 | - public function process($var = '$', $show_methods = false) |
|
44 | - { |
|
45 | - $this->outputMethods = $show_methods; |
|
46 | - if ($var === '$') { |
|
47 | - $var = $this->core->getData(); |
|
48 | - $out = '<div style="background:#aaa; padding:5px; margin:5px; color:#000;">data'; |
|
49 | - } else { |
|
50 | - $out = '<div style="background:#aaa; padding:5px; margin:5px; color:#000;">dump'; |
|
51 | - } |
|
52 | - |
|
53 | - $this->outputObjects = array(); |
|
54 | - |
|
55 | - if (!is_array($var)) { |
|
56 | - if (is_object($var)) { |
|
57 | - return $this->exportObj('', $var); |
|
58 | - } else { |
|
59 | - return $this->exportVar('', $var); |
|
60 | - } |
|
61 | - } |
|
62 | - |
|
63 | - $scope = $this->core->getScope(); |
|
64 | - |
|
65 | - if ($var === $scope) { |
|
66 | - $out .= ' (current scope): <div style="background:#ccc;">'; |
|
67 | - } else { |
|
68 | - $out .= ':<div style="padding-left:20px;">'; |
|
69 | - } |
|
70 | - |
|
71 | - $out .= $this->export($var, $scope); |
|
72 | - |
|
73 | - return $out . '</div></div>'; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * @param $var |
|
78 | - * @param $scope |
|
79 | - * |
|
80 | - * @return string |
|
81 | - */ |
|
82 | - protected function export($var, $scope) |
|
83 | - { |
|
84 | - $out = ''; |
|
85 | - foreach ($var as $i => $v) { |
|
86 | - if (is_array($v) || (is_object($v) && $v instanceof Iterator)) { |
|
87 | - $out .= $i . ' (' . (is_array($v) ? 'array' : 'object: ' . get_class($v)) . ')'; |
|
88 | - if ($v === $scope) { |
|
89 | - $out .= ' (current scope):<div style="background:#ccc;padding-left:20px;">' . $this->export($v, $scope) . '</div>'; |
|
90 | - } else { |
|
91 | - $out .= ':<div style="padding-left:20px;">' . $this->export($v, $scope) . '</div>'; |
|
92 | - } |
|
93 | - } elseif (is_object($v)) { |
|
94 | - $out .= $this->exportObj($i . ' (object: ' . get_class($v) . '):', $v); |
|
95 | - } else { |
|
96 | - $out .= $this->exportVar($i . ' = ', $v); |
|
97 | - } |
|
98 | - } |
|
99 | - |
|
100 | - return $out; |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * @param $i |
|
105 | - * @param $v |
|
106 | - * |
|
107 | - * @return string |
|
108 | - */ |
|
109 | - protected function exportVar($i, $v) |
|
110 | - { |
|
111 | - if (is_string($v) || is_bool($v) || is_numeric($v)) { |
|
112 | - return $i . htmlentities(var_export($v, true)) . '<br />'; |
|
113 | - } elseif (is_null($v)) { |
|
114 | - return $i . 'null<br />'; |
|
115 | - } elseif (is_resource($v)) { |
|
116 | - return $i . 'resource(' . get_resource_type($v) . ')<br />'; |
|
117 | - } else { |
|
118 | - return $i . htmlentities(var_export($v, true)) . '<br />'; |
|
119 | - } |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * @param $i |
|
124 | - * @param $obj |
|
125 | - * |
|
126 | - * @return string |
|
127 | - */ |
|
128 | - protected function exportObj($i, $obj) |
|
129 | - { |
|
130 | - if (array_search($obj, $this->outputObjects, true) !== false) { |
|
131 | - return $i . ' [recursion, skipped]<br />'; |
|
132 | - } |
|
133 | - |
|
134 | - $this->outputObjects[] = $obj; |
|
135 | - |
|
136 | - $list = (array)$obj; |
|
137 | - |
|
138 | - $protectedLength = strlen(get_class($obj)) + 2; |
|
139 | - |
|
140 | - $out = array(); |
|
141 | - |
|
142 | - if ($this->outputMethods) { |
|
143 | - $ref = new ReflectionObject($obj); |
|
144 | - |
|
145 | - foreach ($ref->getMethods() as $method) { |
|
146 | - if (!$method->isPublic()) { |
|
147 | - continue; |
|
148 | - } |
|
149 | - |
|
150 | - if (empty($out['method'])) { |
|
151 | - $out['method'] = ''; |
|
152 | - } |
|
153 | - |
|
154 | - $params = array(); |
|
155 | - foreach ($method->getParameters() as $param) { |
|
156 | - $params[] = ($param->isPassedByReference() ? '&' : '') . '$' . $param->getName() . ($param->isOptional() ? ' = ' . var_export($param->getDefaultValue(), true) : ''); |
|
157 | - } |
|
158 | - |
|
159 | - $out['method'] .= '(method) ' . $method->getName() . '(' . implode(', ', $params) . ')<br />'; |
|
160 | - } |
|
161 | - } |
|
162 | - |
|
163 | - foreach ($list as $attributeName => $attributeValue) { |
|
164 | - if (property_exists($obj, $attributeName)) { |
|
165 | - $key = 'public'; |
|
166 | - } elseif (substr($attributeName, 0, 3) === "\0*\0") { |
|
167 | - $key = 'protected'; |
|
168 | - $attributeName = substr($attributeName, 3); |
|
169 | - } else { |
|
170 | - $key = 'private'; |
|
171 | - $attributeName = substr($attributeName, $protectedLength); |
|
172 | - } |
|
173 | - |
|
174 | - if (empty($out[$key])) { |
|
175 | - $out[$key] = ''; |
|
176 | - } |
|
177 | - |
|
178 | - $out[$key] .= '(' . $key . ') '; |
|
179 | - |
|
180 | - if (is_array($attributeValue)) { |
|
181 | - $out[$key] .= $attributeName . ' (array):<br /> |
|
34 | + protected $outputObjects; |
|
35 | + protected $outputMethods; |
|
36 | + |
|
37 | + /** |
|
38 | + * @param string $var |
|
39 | + * @param bool $show_methods |
|
40 | + * |
|
41 | + * @return string |
|
42 | + */ |
|
43 | + public function process($var = '$', $show_methods = false) |
|
44 | + { |
|
45 | + $this->outputMethods = $show_methods; |
|
46 | + if ($var === '$') { |
|
47 | + $var = $this->core->getData(); |
|
48 | + $out = '<div style="background:#aaa; padding:5px; margin:5px; color:#000;">data'; |
|
49 | + } else { |
|
50 | + $out = '<div style="background:#aaa; padding:5px; margin:5px; color:#000;">dump'; |
|
51 | + } |
|
52 | + |
|
53 | + $this->outputObjects = array(); |
|
54 | + |
|
55 | + if (!is_array($var)) { |
|
56 | + if (is_object($var)) { |
|
57 | + return $this->exportObj('', $var); |
|
58 | + } else { |
|
59 | + return $this->exportVar('', $var); |
|
60 | + } |
|
61 | + } |
|
62 | + |
|
63 | + $scope = $this->core->getScope(); |
|
64 | + |
|
65 | + if ($var === $scope) { |
|
66 | + $out .= ' (current scope): <div style="background:#ccc;">'; |
|
67 | + } else { |
|
68 | + $out .= ':<div style="padding-left:20px;">'; |
|
69 | + } |
|
70 | + |
|
71 | + $out .= $this->export($var, $scope); |
|
72 | + |
|
73 | + return $out . '</div></div>'; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * @param $var |
|
78 | + * @param $scope |
|
79 | + * |
|
80 | + * @return string |
|
81 | + */ |
|
82 | + protected function export($var, $scope) |
|
83 | + { |
|
84 | + $out = ''; |
|
85 | + foreach ($var as $i => $v) { |
|
86 | + if (is_array($v) || (is_object($v) && $v instanceof Iterator)) { |
|
87 | + $out .= $i . ' (' . (is_array($v) ? 'array' : 'object: ' . get_class($v)) . ')'; |
|
88 | + if ($v === $scope) { |
|
89 | + $out .= ' (current scope):<div style="background:#ccc;padding-left:20px;">' . $this->export($v, $scope) . '</div>'; |
|
90 | + } else { |
|
91 | + $out .= ':<div style="padding-left:20px;">' . $this->export($v, $scope) . '</div>'; |
|
92 | + } |
|
93 | + } elseif (is_object($v)) { |
|
94 | + $out .= $this->exportObj($i . ' (object: ' . get_class($v) . '):', $v); |
|
95 | + } else { |
|
96 | + $out .= $this->exportVar($i . ' = ', $v); |
|
97 | + } |
|
98 | + } |
|
99 | + |
|
100 | + return $out; |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * @param $i |
|
105 | + * @param $v |
|
106 | + * |
|
107 | + * @return string |
|
108 | + */ |
|
109 | + protected function exportVar($i, $v) |
|
110 | + { |
|
111 | + if (is_string($v) || is_bool($v) || is_numeric($v)) { |
|
112 | + return $i . htmlentities(var_export($v, true)) . '<br />'; |
|
113 | + } elseif (is_null($v)) { |
|
114 | + return $i . 'null<br />'; |
|
115 | + } elseif (is_resource($v)) { |
|
116 | + return $i . 'resource(' . get_resource_type($v) . ')<br />'; |
|
117 | + } else { |
|
118 | + return $i . htmlentities(var_export($v, true)) . '<br />'; |
|
119 | + } |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * @param $i |
|
124 | + * @param $obj |
|
125 | + * |
|
126 | + * @return string |
|
127 | + */ |
|
128 | + protected function exportObj($i, $obj) |
|
129 | + { |
|
130 | + if (array_search($obj, $this->outputObjects, true) !== false) { |
|
131 | + return $i . ' [recursion, skipped]<br />'; |
|
132 | + } |
|
133 | + |
|
134 | + $this->outputObjects[] = $obj; |
|
135 | + |
|
136 | + $list = (array)$obj; |
|
137 | + |
|
138 | + $protectedLength = strlen(get_class($obj)) + 2; |
|
139 | + |
|
140 | + $out = array(); |
|
141 | + |
|
142 | + if ($this->outputMethods) { |
|
143 | + $ref = new ReflectionObject($obj); |
|
144 | + |
|
145 | + foreach ($ref->getMethods() as $method) { |
|
146 | + if (!$method->isPublic()) { |
|
147 | + continue; |
|
148 | + } |
|
149 | + |
|
150 | + if (empty($out['method'])) { |
|
151 | + $out['method'] = ''; |
|
152 | + } |
|
153 | + |
|
154 | + $params = array(); |
|
155 | + foreach ($method->getParameters() as $param) { |
|
156 | + $params[] = ($param->isPassedByReference() ? '&' : '') . '$' . $param->getName() . ($param->isOptional() ? ' = ' . var_export($param->getDefaultValue(), true) : ''); |
|
157 | + } |
|
158 | + |
|
159 | + $out['method'] .= '(method) ' . $method->getName() . '(' . implode(', ', $params) . ')<br />'; |
|
160 | + } |
|
161 | + } |
|
162 | + |
|
163 | + foreach ($list as $attributeName => $attributeValue) { |
|
164 | + if (property_exists($obj, $attributeName)) { |
|
165 | + $key = 'public'; |
|
166 | + } elseif (substr($attributeName, 0, 3) === "\0*\0") { |
|
167 | + $key = 'protected'; |
|
168 | + $attributeName = substr($attributeName, 3); |
|
169 | + } else { |
|
170 | + $key = 'private'; |
|
171 | + $attributeName = substr($attributeName, $protectedLength); |
|
172 | + } |
|
173 | + |
|
174 | + if (empty($out[$key])) { |
|
175 | + $out[$key] = ''; |
|
176 | + } |
|
177 | + |
|
178 | + $out[$key] .= '(' . $key . ') '; |
|
179 | + |
|
180 | + if (is_array($attributeValue)) { |
|
181 | + $out[$key] .= $attributeName . ' (array):<br /> |
|
182 | 182 | <div style="padding-left:20px;">' . $this->export($attributeValue, false) . '</div>'; |
183 | - } elseif (is_object($attributeValue)) { |
|
184 | - $out[$key] .= $this->exportObj($attributeName . ' (object: ' . get_class($attributeValue) . '):', $attributeValue); |
|
185 | - } else { |
|
186 | - $out[$key] .= $this->exportVar($attributeName . ' = ', $attributeValue); |
|
187 | - } |
|
188 | - } |
|
183 | + } elseif (is_object($attributeValue)) { |
|
184 | + $out[$key] .= $this->exportObj($attributeName . ' (object: ' . get_class($attributeValue) . '):', $attributeValue); |
|
185 | + } else { |
|
186 | + $out[$key] .= $this->exportVar($attributeName . ' = ', $attributeValue); |
|
187 | + } |
|
188 | + } |
|
189 | 189 | |
190 | - $return = $i . '<br /><div style="padding-left:20px;">'; |
|
190 | + $return = $i . '<br /><div style="padding-left:20px;">'; |
|
191 | 191 | |
192 | - if (!empty($out['method'])) { |
|
193 | - $return .= $out['method']; |
|
194 | - } |
|
192 | + if (!empty($out['method'])) { |
|
193 | + $return .= $out['method']; |
|
194 | + } |
|
195 | 195 | |
196 | - if (!empty($out['public'])) { |
|
197 | - $return .= $out['public']; |
|
198 | - } |
|
196 | + if (!empty($out['public'])) { |
|
197 | + $return .= $out['public']; |
|
198 | + } |
|
199 | 199 | |
200 | - if (!empty($out['protected'])) { |
|
201 | - $return .= $out['protected']; |
|
202 | - } |
|
200 | + if (!empty($out['protected'])) { |
|
201 | + $return .= $out['protected']; |
|
202 | + } |
|
203 | 203 | |
204 | - if (!empty($out['private'])) { |
|
205 | - $return .= $out['private']; |
|
206 | - } |
|
204 | + if (!empty($out['private'])) { |
|
205 | + $return .= $out['private']; |
|
206 | + } |
|
207 | 207 | |
208 | - return $return . '</div>'; |
|
209 | - } |
|
208 | + return $return . '</div>'; |
|
209 | + } |
|
210 | 210 | } |
@@ -30,14 +30,14 @@ |
||
30 | 30 | */ |
31 | 31 | class PluginCountSentences extends Plugin implements ICompilable |
32 | 32 | { |
33 | - /** |
|
34 | - * @param Compiler $compiler |
|
35 | - * @param string $value |
|
36 | - * |
|
37 | - * @return string |
|
38 | - */ |
|
39 | - public static function compile(Compiler $compiler, $value) |
|
40 | - { |
|
41 | - return "preg_match_all('#[\\w\\pL]\\.(?![\\w\\pL])#u', $value, \$tmp)"; |
|
42 | - } |
|
33 | + /** |
|
34 | + * @param Compiler $compiler |
|
35 | + * @param string $value |
|
36 | + * |
|
37 | + * @return string |
|
38 | + */ |
|
39 | + public static function compile(Compiler $compiler, $value) |
|
40 | + { |
|
41 | + return "preg_match_all('#[\\w\\pL]\\.(?![\\w\\pL])#u', $value, \$tmp)"; |
|
42 | + } |
|
43 | 43 | } |
44 | 44 | \ No newline at end of file |
@@ -32,16 +32,16 @@ |
||
32 | 32 | */ |
33 | 33 | class PluginIndent extends Plugin implements ICompilable |
34 | 34 | { |
35 | - /** |
|
36 | - * @param Compiler $compiler |
|
37 | - * @param string $value |
|
38 | - * @param int $by |
|
39 | - * @param string $char |
|
40 | - * |
|
41 | - * @return string |
|
42 | - */ |
|
43 | - public static function compile(Compiler $compiler, $value, $by = 4, $char = ' ') |
|
44 | - { |
|
45 | - return "preg_replace('#^#m', '" . str_repeat(substr($char, 1, - 1), trim($by, '"\'')) . "', $value)"; |
|
46 | - } |
|
35 | + /** |
|
36 | + * @param Compiler $compiler |
|
37 | + * @param string $value |
|
38 | + * @param int $by |
|
39 | + * @param string $char |
|
40 | + * |
|
41 | + * @return string |
|
42 | + */ |
|
43 | + public static function compile(Compiler $compiler, $value, $by = 4, $char = ' ') |
|
44 | + { |
|
45 | + return "preg_replace('#^#m', '" . str_repeat(substr($char, 1, - 1), trim($by, '"\'')) . "', $value)"; |
|
46 | + } |
|
47 | 47 | } |
48 | 48 | \ No newline at end of file |
@@ -33,37 +33,37 @@ |
||
33 | 33 | */ |
34 | 34 | class PluginTruncate extends Plugin |
35 | 35 | { |
36 | - /** |
|
37 | - * @param string $value |
|
38 | - * @param int $length |
|
39 | - * @param string $etc |
|
40 | - * @param bool $break |
|
41 | - * @param bool $middle |
|
42 | - * |
|
43 | - * @return mixed|string |
|
44 | - */ |
|
45 | - public function process($value, $length = 80, $etc = '...', $break = false, $middle = false) |
|
46 | - { |
|
47 | - if ($length == 0) { |
|
48 | - return ''; |
|
49 | - } |
|
36 | + /** |
|
37 | + * @param string $value |
|
38 | + * @param int $length |
|
39 | + * @param string $etc |
|
40 | + * @param bool $break |
|
41 | + * @param bool $middle |
|
42 | + * |
|
43 | + * @return mixed|string |
|
44 | + */ |
|
45 | + public function process($value, $length = 80, $etc = '...', $break = false, $middle = false) |
|
46 | + { |
|
47 | + if ($length == 0) { |
|
48 | + return ''; |
|
49 | + } |
|
50 | 50 | |
51 | - $value = (string)$value; |
|
52 | - $etc = (string)$etc; |
|
53 | - $length = (int)$length; |
|
51 | + $value = (string)$value; |
|
52 | + $etc = (string)$etc; |
|
53 | + $length = (int)$length; |
|
54 | 54 | |
55 | - if (strlen($value) < $length) { |
|
56 | - return $value; |
|
57 | - } |
|
55 | + if (strlen($value) < $length) { |
|
56 | + return $value; |
|
57 | + } |
|
58 | 58 | |
59 | - $length = max($length - strlen($etc), 0); |
|
60 | - if ($break === false && $middle === false) { |
|
61 | - $value = preg_replace('#\s+(\S*)?$#', '', substr($value, 0, $length + 1)); |
|
62 | - } |
|
63 | - if ($middle === false) { |
|
64 | - return substr($value, 0, $length) . $etc; |
|
65 | - } |
|
59 | + $length = max($length - strlen($etc), 0); |
|
60 | + if ($break === false && $middle === false) { |
|
61 | + $value = preg_replace('#\s+(\S*)?$#', '', substr($value, 0, $length + 1)); |
|
62 | + } |
|
63 | + if ($middle === false) { |
|
64 | + return substr($value, 0, $length) . $etc; |
|
65 | + } |
|
66 | 66 | |
67 | - return substr($value, 0, ceil($length / 2)) . $etc . substr($value, - floor($length / 2)); |
|
68 | - } |
|
67 | + return substr($value, 0, ceil($length / 2)) . $etc . substr($value, - floor($length / 2)); |
|
68 | + } |
|
69 | 69 | } |
70 | 70 | \ No newline at end of file |
@@ -31,15 +31,15 @@ |
||
31 | 31 | */ |
32 | 32 | class PluginDefault extends Plugin implements ICompilable |
33 | 33 | { |
34 | - /** |
|
35 | - * @param Compiler $compiler |
|
36 | - * @param mixed $value |
|
37 | - * @param string $default |
|
38 | - * |
|
39 | - * @return string |
|
40 | - */ |
|
41 | - public static function compile(Compiler $compiler, $value, $default = '') |
|
42 | - { |
|
43 | - return '(($tmp = ' . $value . ')===null||$tmp===\'\' ? ' . $default . ' : $tmp)'; |
|
44 | - } |
|
34 | + /** |
|
35 | + * @param Compiler $compiler |
|
36 | + * @param mixed $value |
|
37 | + * @param string $default |
|
38 | + * |
|
39 | + * @return string |
|
40 | + */ |
|
41 | + public static function compile(Compiler $compiler, $value, $default = '') |
|
42 | + { |
|
43 | + return '(($tmp = ' . $value . ')===null||$tmp===\'\' ? ' . $default . ' : $tmp)'; |
|
44 | + } |
|
45 | 45 | } |
46 | 46 | \ No newline at end of file |
@@ -30,15 +30,15 @@ |
||
30 | 30 | */ |
31 | 31 | class PluginCat extends Plugin implements ICompilable |
32 | 32 | { |
33 | - /** |
|
34 | - * @param Compiler $compiler |
|
35 | - * @param string $value |
|
36 | - * @param array $rest |
|
37 | - * |
|
38 | - * @return string |
|
39 | - */ |
|
40 | - public static function compile(Compiler $compiler, $value, array $rest) |
|
41 | - { |
|
42 | - return '(' . $value . ').(' . implode(').(', $rest) . ')'; |
|
43 | - } |
|
33 | + /** |
|
34 | + * @param Compiler $compiler |
|
35 | + * @param string $value |
|
36 | + * @param array $rest |
|
37 | + * |
|
38 | + * @return string |
|
39 | + */ |
|
40 | + public static function compile(Compiler $compiler, $value, array $rest) |
|
41 | + { |
|
42 | + return '(' . $value . ').(' . implode(').(', $rest) . ')'; |
|
43 | + } |
|
44 | 44 | } |
@@ -31,19 +31,19 @@ |
||
31 | 31 | */ |
32 | 32 | class PluginCountCharacters extends Plugin implements ICompilable |
33 | 33 | { |
34 | - /** |
|
35 | - * @param Compiler $compiler |
|
36 | - * @param string $value |
|
37 | - * @param bool $count_spaces |
|
38 | - * |
|
39 | - * @return string |
|
40 | - */ |
|
41 | - public static function compile(Compiler $compiler, $value, $count_spaces = false) |
|
42 | - { |
|
43 | - if ($count_spaces === 'false') { |
|
44 | - return 'preg_match_all(\'#[^\s\pZ]#u\', ' . $value . ', $tmp)'; |
|
45 | - } |
|
34 | + /** |
|
35 | + * @param Compiler $compiler |
|
36 | + * @param string $value |
|
37 | + * @param bool $count_spaces |
|
38 | + * |
|
39 | + * @return string |
|
40 | + */ |
|
41 | + public static function compile(Compiler $compiler, $value, $count_spaces = false) |
|
42 | + { |
|
43 | + if ($count_spaces === 'false') { |
|
44 | + return 'preg_match_all(\'#[^\s\pZ]#u\', ' . $value . ', $tmp)'; |
|
45 | + } |
|
46 | 46 | |
47 | - return 'mb_strlen(' . $value . ', $this->charset)'; |
|
48 | - } |
|
47 | + return 'mb_strlen(' . $value . ', $this->charset)'; |
|
48 | + } |
|
49 | 49 | } |
50 | 50 | \ No newline at end of file |
@@ -30,14 +30,14 @@ |
||
30 | 30 | */ |
31 | 31 | class PluginNl2br extends Plugin implements ICompilable |
32 | 32 | { |
33 | - /** |
|
34 | - * @param Compiler $compiler |
|
35 | - * @param string $value |
|
36 | - * |
|
37 | - * @return string |
|
38 | - */ |
|
39 | - public static function compile(Compiler $compiler, $value) |
|
40 | - { |
|
41 | - return 'nl2br((string) ' . $value . ')'; |
|
42 | - } |
|
33 | + /** |
|
34 | + * @param Compiler $compiler |
|
35 | + * @param string $value |
|
36 | + * |
|
37 | + * @return string |
|
38 | + */ |
|
39 | + public static function compile(Compiler $compiler, $value) |
|
40 | + { |
|
41 | + return 'nl2br((string) ' . $value . ')'; |
|
42 | + } |
|
43 | 43 | } |
44 | 44 | \ No newline at end of file |
@@ -41,159 +41,159 @@ |
||
41 | 41 | */ |
42 | 42 | class PluginMath extends Plugin implements ICompilable |
43 | 43 | { |
44 | - /** |
|
45 | - * @param Compiler $compiler |
|
46 | - * @param string $equation |
|
47 | - * @param string $format |
|
48 | - * @param string $assign |
|
49 | - * @param array $rest |
|
50 | - * |
|
51 | - * @return string |
|
52 | - * @throws CompilationException |
|
53 | - */ |
|
54 | - public static function compile(Compiler $compiler, $equation, $format = '', $assign = '', array $rest = array()) |
|
55 | - { |
|
56 | - /* |
|
44 | + /** |
|
45 | + * @param Compiler $compiler |
|
46 | + * @param string $equation |
|
47 | + * @param string $format |
|
48 | + * @param string $assign |
|
49 | + * @param array $rest |
|
50 | + * |
|
51 | + * @return string |
|
52 | + * @throws CompilationException |
|
53 | + */ |
|
54 | + public static function compile(Compiler $compiler, $equation, $format = '', $assign = '', array $rest = array()) |
|
55 | + { |
|
56 | + /* |
|
57 | 57 | * Holds the allowed function, characters, operators and constants |
58 | 58 | */ |
59 | - $allowed = array( |
|
60 | - '0', |
|
61 | - '1', |
|
62 | - '2', |
|
63 | - '3', |
|
64 | - '4', |
|
65 | - '5', |
|
66 | - '6', |
|
67 | - '7', |
|
68 | - '8', |
|
69 | - '9', |
|
70 | - '+', |
|
71 | - '-', |
|
72 | - '/', |
|
73 | - '*', |
|
74 | - '.', |
|
75 | - ' ', |
|
76 | - '<<', |
|
77 | - '>>', |
|
78 | - '%', |
|
79 | - '&', |
|
80 | - '^', |
|
81 | - '|', |
|
82 | - '~', |
|
83 | - 'abs(', |
|
84 | - 'ceil(', |
|
85 | - 'floor(', |
|
86 | - 'exp(', |
|
87 | - 'log10(', |
|
88 | - 'cos(', |
|
89 | - 'sin(', |
|
90 | - 'sqrt(', |
|
91 | - 'tan(', |
|
92 | - 'M_PI', |
|
93 | - 'INF', |
|
94 | - 'M_E', |
|
95 | - ); |
|
59 | + $allowed = array( |
|
60 | + '0', |
|
61 | + '1', |
|
62 | + '2', |
|
63 | + '3', |
|
64 | + '4', |
|
65 | + '5', |
|
66 | + '6', |
|
67 | + '7', |
|
68 | + '8', |
|
69 | + '9', |
|
70 | + '+', |
|
71 | + '-', |
|
72 | + '/', |
|
73 | + '*', |
|
74 | + '.', |
|
75 | + ' ', |
|
76 | + '<<', |
|
77 | + '>>', |
|
78 | + '%', |
|
79 | + '&', |
|
80 | + '^', |
|
81 | + '|', |
|
82 | + '~', |
|
83 | + 'abs(', |
|
84 | + 'ceil(', |
|
85 | + 'floor(', |
|
86 | + 'exp(', |
|
87 | + 'log10(', |
|
88 | + 'cos(', |
|
89 | + 'sin(', |
|
90 | + 'sqrt(', |
|
91 | + 'tan(', |
|
92 | + 'M_PI', |
|
93 | + 'INF', |
|
94 | + 'M_E', |
|
95 | + ); |
|
96 | 96 | |
97 | - /* |
|
97 | + /* |
|
98 | 98 | * Holds the functions that can accept multiple arguments |
99 | 99 | */ |
100 | - $funcs = array( |
|
101 | - 'round(', |
|
102 | - 'log(', |
|
103 | - 'pow(', |
|
104 | - 'max(', |
|
105 | - 'min(', |
|
106 | - 'rand(', |
|
107 | - ); |
|
100 | + $funcs = array( |
|
101 | + 'round(', |
|
102 | + 'log(', |
|
103 | + 'pow(', |
|
104 | + 'max(', |
|
105 | + 'min(', |
|
106 | + 'rand(', |
|
107 | + ); |
|
108 | 108 | |
109 | - $equation = $equationSrc = str_ireplace(array( |
|
110 | - 'pi', |
|
111 | - 'M_PI()', |
|
112 | - 'inf', |
|
113 | - ' e ' |
|
114 | - ), |
|
115 | - array( |
|
116 | - 'M_PI', |
|
117 | - 'M_PI', |
|
118 | - 'INF', |
|
119 | - ' M_E ' |
|
120 | - ), |
|
121 | - $equation); |
|
109 | + $equation = $equationSrc = str_ireplace(array( |
|
110 | + 'pi', |
|
111 | + 'M_PI()', |
|
112 | + 'inf', |
|
113 | + ' e ' |
|
114 | + ), |
|
115 | + array( |
|
116 | + 'M_PI', |
|
117 | + 'M_PI', |
|
118 | + 'INF', |
|
119 | + ' M_E ' |
|
120 | + ), |
|
121 | + $equation); |
|
122 | 122 | |
123 | - $delim = $equation[0]; |
|
124 | - $open = $delim . '.'; |
|
125 | - $close = '.' . $delim; |
|
126 | - $equation = substr($equation, 1, - 1); |
|
127 | - $out = ''; |
|
128 | - $ptr = 1; |
|
129 | - $allowcomma = 0; |
|
130 | - while (strlen($equation) > 0) { |
|
131 | - $substr = substr($equation, 0, $ptr); |
|
132 | - if (array_search($substr, $allowed) !== false) { |
|
133 | - // allowed string |
|
134 | - $out .= $substr; |
|
135 | - $equation = substr($equation, $ptr); |
|
136 | - $ptr = 0; |
|
137 | - } elseif (array_search($substr, $funcs) !== false) { |
|
138 | - // allowed func |
|
139 | - $out .= $substr; |
|
140 | - $equation = substr($equation, $ptr); |
|
141 | - $ptr = 0; |
|
142 | - ++ $allowcomma; |
|
143 | - if ($allowcomma === 1) { |
|
144 | - $allowed[] = ','; |
|
145 | - } |
|
146 | - } elseif (isset($rest[$substr])) { |
|
147 | - // variable |
|
148 | - $out .= $rest[$substr]; |
|
149 | - $equation = substr($equation, $ptr); |
|
150 | - $ptr = 0; |
|
151 | - } elseif ($substr === $open) { |
|
152 | - // pre-replaced variable |
|
153 | - preg_match('#.*\((?:[^()]*?|(?R))\)' . str_replace('.', '\\.', $close) . '#', substr($equation, 2), $m); |
|
154 | - if (empty($m)) { |
|
155 | - preg_match('#.*?' . str_replace('.', '\\.', $close) . '#', substr($equation, 2), $m); |
|
156 | - } |
|
157 | - $out .= substr($m[0], 0, - 2); |
|
158 | - $equation = substr($equation, strlen($m[0]) + 2); |
|
159 | - $ptr = 0; |
|
160 | - } elseif ($substr === '(') { |
|
161 | - // opening parenthesis |
|
162 | - if ($allowcomma > 0) { |
|
163 | - ++ $allowcomma; |
|
164 | - } |
|
123 | + $delim = $equation[0]; |
|
124 | + $open = $delim . '.'; |
|
125 | + $close = '.' . $delim; |
|
126 | + $equation = substr($equation, 1, - 1); |
|
127 | + $out = ''; |
|
128 | + $ptr = 1; |
|
129 | + $allowcomma = 0; |
|
130 | + while (strlen($equation) > 0) { |
|
131 | + $substr = substr($equation, 0, $ptr); |
|
132 | + if (array_search($substr, $allowed) !== false) { |
|
133 | + // allowed string |
|
134 | + $out .= $substr; |
|
135 | + $equation = substr($equation, $ptr); |
|
136 | + $ptr = 0; |
|
137 | + } elseif (array_search($substr, $funcs) !== false) { |
|
138 | + // allowed func |
|
139 | + $out .= $substr; |
|
140 | + $equation = substr($equation, $ptr); |
|
141 | + $ptr = 0; |
|
142 | + ++ $allowcomma; |
|
143 | + if ($allowcomma === 1) { |
|
144 | + $allowed[] = ','; |
|
145 | + } |
|
146 | + } elseif (isset($rest[$substr])) { |
|
147 | + // variable |
|
148 | + $out .= $rest[$substr]; |
|
149 | + $equation = substr($equation, $ptr); |
|
150 | + $ptr = 0; |
|
151 | + } elseif ($substr === $open) { |
|
152 | + // pre-replaced variable |
|
153 | + preg_match('#.*\((?:[^()]*?|(?R))\)' . str_replace('.', '\\.', $close) . '#', substr($equation, 2), $m); |
|
154 | + if (empty($m)) { |
|
155 | + preg_match('#.*?' . str_replace('.', '\\.', $close) . '#', substr($equation, 2), $m); |
|
156 | + } |
|
157 | + $out .= substr($m[0], 0, - 2); |
|
158 | + $equation = substr($equation, strlen($m[0]) + 2); |
|
159 | + $ptr = 0; |
|
160 | + } elseif ($substr === '(') { |
|
161 | + // opening parenthesis |
|
162 | + if ($allowcomma > 0) { |
|
163 | + ++ $allowcomma; |
|
164 | + } |
|
165 | 165 | |
166 | - $out .= $substr; |
|
167 | - $equation = substr($equation, $ptr); |
|
168 | - $ptr = 0; |
|
169 | - } elseif ($substr === ')') { |
|
170 | - // closing parenthesis |
|
171 | - if ($allowcomma > 0) { |
|
172 | - -- $allowcomma; |
|
173 | - if ($allowcomma === 0) { |
|
174 | - array_pop($allowed); |
|
175 | - } |
|
176 | - } |
|
166 | + $out .= $substr; |
|
167 | + $equation = substr($equation, $ptr); |
|
168 | + $ptr = 0; |
|
169 | + } elseif ($substr === ')') { |
|
170 | + // closing parenthesis |
|
171 | + if ($allowcomma > 0) { |
|
172 | + -- $allowcomma; |
|
173 | + if ($allowcomma === 0) { |
|
174 | + array_pop($allowed); |
|
175 | + } |
|
176 | + } |
|
177 | 177 | |
178 | - $out .= $substr; |
|
179 | - $equation = substr($equation, $ptr); |
|
180 | - $ptr = 0; |
|
181 | - } elseif ($ptr >= strlen($equation)) { |
|
182 | - // parse error if we've consumed the entire equation without finding anything valid |
|
183 | - throw new CompilationException($compiler, |
|
184 | - 'Math : Syntax error or variable undefined in equation ' . $equationSrc . ' at ' . $substr); |
|
185 | - } else { |
|
186 | - // nothing special, advance |
|
187 | - ++ $ptr; |
|
188 | - } |
|
189 | - } |
|
190 | - if ($format !== '\'\'') { |
|
191 | - $out = 'sprintf(' . $format . ', ' . $out . ')'; |
|
192 | - } |
|
193 | - if ($assign !== '\'\'') { |
|
194 | - return '($this->assignInScope(' . $out . ', ' . $assign . '))'; |
|
195 | - } |
|
178 | + $out .= $substr; |
|
179 | + $equation = substr($equation, $ptr); |
|
180 | + $ptr = 0; |
|
181 | + } elseif ($ptr >= strlen($equation)) { |
|
182 | + // parse error if we've consumed the entire equation without finding anything valid |
|
183 | + throw new CompilationException($compiler, |
|
184 | + 'Math : Syntax error or variable undefined in equation ' . $equationSrc . ' at ' . $substr); |
|
185 | + } else { |
|
186 | + // nothing special, advance |
|
187 | + ++ $ptr; |
|
188 | + } |
|
189 | + } |
|
190 | + if ($format !== '\'\'') { |
|
191 | + $out = 'sprintf(' . $format . ', ' . $out . ')'; |
|
192 | + } |
|
193 | + if ($assign !== '\'\'') { |
|
194 | + return '($this->assignInScope(' . $out . ', ' . $assign . '))'; |
|
195 | + } |
|
196 | 196 | |
197 | - return '(' . $out . ')'; |
|
198 | - } |
|
197 | + return '(' . $out . ')'; |
|
198 | + } |
|
199 | 199 | } |
200 | 200 | \ No newline at end of file |