Skip to content

Commit 857dcc9

Browse files
committed
Allow to get a char in a string with hooks
1 parent b74e3b7 commit 857dcc9

File tree

6 files changed

+15
-2
lines changed

6 files changed

+15
-2
lines changed

examples/string-char-at.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var str = 'abc';
2+
3+
return str[1];

examples/string-char-at.return

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
b

src/JsPhpize/Compiler/Compiler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,9 @@ protected function visitFunctionCall(FunctionCall $functionCall, $indent)
250250
return $staticCall;
251251
}
252252

253-
return 'function_exists(' . var_export($name, true) . ') ? ' .
253+
return '(function_exists(' . var_export($name, true) . ') ? ' .
254254
$staticCall . ' : ' .
255-
$dynamicCall;
255+
$dynamicCall . ')';
256256
}
257257

258258
if (count($functionCall->children)) {

src/JsPhpize/Compiler/Helpers/Dot.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ function ($base) {
1717
};
1818
$fallbackDot = function ($base, $key) use ($getCallable, $getRegExp) {
1919
if (is_string($base)) {
20+
if (preg_match('/^[-+]?\d+$/', strval($key))) {
21+
return substr($base, intval($key), 1);
22+
}
2023
if ($key === 'substr' || $key === 'slice') {
2124
return function ($start, $length = null) use ($base) {
2225
return func_num_args() === 1 ? substr($base, $start) : substr($base, $start, $length);

src/JsPhpize/Compiler/Helpers/DotObject.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ function ($base) {
1717
};
1818
$fallbackDot = function ($base, $key) use ($getCallable, $getRegExp) {
1919
if (is_string($base)) {
20+
if (preg_match('/^[-+]?\d+$/', strval($key))) {
21+
return substr($base, intval($key), 1);
22+
}
2023
if ($key === 'substr' || $key === 'slice') {
2124
return function ($start, $length = null) use ($base) {
2225
return func_num_args() === 1 ? substr($base, $start) : substr($base, $start, $length);

src/JsPhpize/Compiler/Helpers/DotWithArrayPrototype.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ function ($base) {
9393
};
9494
$fallbackDot = function ($base, $key) use ($getCallable, $getRegExp) {
9595
if (is_string($base)) {
96+
if (preg_match('/^[-+]?\d+$/', strval($key))) {
97+
return substr($base, intval($key), 1);
98+
}
9699
if ($key === 'substr' || $key === 'slice') {
97100
return function ($start, $length = null) use ($base) {
98101
return func_num_args() === 1 ? substr($base, $start) : substr($base, $start, $length);

0 commit comments

Comments
 (0)