Skip to content

Commit 418f543

Browse files
authored
Return raw source strings for functions (#10)
* wip: Return raw strings for functions * Improve code style, add readme * New version nr
1 parent b84b0c2 commit 418f543

5 files changed

Lines changed: 38 additions & 16 deletions

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This small JS utility reads PHP strings containing arrays and returns a JavaScri
77
It uses [glayzzle/php-parser](https://github.com/glayzzle/php-parser) to parse PHP into AST and uses that
88
info to extract arrays.
99
It supports both indexed and associative arrays (i.e. lists and dictionaries/maps) and array, string, numeric and null values.
10+
Inline function calls are not evaluated but returned as raw strings. See the example below.
1011

1112
## Installation
1213

@@ -35,6 +36,7 @@ const phpString = `[
3536
'and_numeric' => 42,
3637
'what_about' => true,
3738
'or' => false,
39+
'func' => strtoupper('abc'),
3840
]`;
3941
const data = fromString(phpString);
4042
```
@@ -50,7 +52,8 @@ const data = fromString(phpString);
5052
also_supports: null,
5153
and_numeric: 42,
5254
what_about: true,
53-
or: false
55+
or: false,
56+
func: "strtoupper('abc')"
5457
}
5558
```
5659

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ function fromString (phpString) {
1313
extractDoc: false,
1414
suppressErrors: true
1515
},
16-
ast: { withPositions: false }
16+
ast: {
17+
withPositions: true,
18+
withSource: true
19+
}
1720
});
1821

1922
phpString = phpString.trim();
@@ -72,6 +75,7 @@ function parseValue (expr) {
7275
if (expr.kind === 'boolean') return expr.value;
7376
if (expr.kind === 'nullkeyword') return null;
7477
if (expr.kind === 'identifier' && expr.name.name === 'null') return null;
78+
if (expr.kind === 'call') return expr.loc?.source;
7579
return undefined;
7680
}
7781

index.test.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,19 @@ test('php file', () => {
5757
expect(data).toEqual(['foo', 'bar']);
5858
});
5959

60-
test('all value types match (arrays strings numbers decimals nulls booleans)', () => {
60+
test('function calls', () => {
61+
const data = fromString(`[
62+
'func' => env('SOME_VAR', true),
63+
'another' => substr($var, 0, 5),
64+
'arrow' => (fn() => 123),
65+
]`);
66+
expect(data).toEqual({
67+
func : "env('SOME_VAR', true)",
68+
another: "substr($var, 0, 5)",
69+
});
70+
})
71+
72+
test('all value types match (arrays strings numbers decimals nulls booleans functions)', () => {
6173
const data = fromString(`[
6274
'arrayvalue' => [
6375
'hello', 'world'
@@ -68,6 +80,7 @@ test('all value types match (arrays strings numbers decimals nulls booleans)', (
6880
'nullvalue' => null,
6981
'truevalue' => true,
7082
'falsevalue' => false,
83+
'functionvalue' => strtoupper('abc'),
7184
]`);
7285
expect(data).toEqual({
7386
arrayvalue: ['hello', 'world'],
@@ -76,6 +89,8 @@ test('all value types match (arrays strings numbers decimals nulls booleans)', (
7689
decimalvalue: 4.2,
7790
nullvalue: null,
7891
truevalue: true,
79-
falsevalue: false
92+
falsevalue: false,
93+
functionvalue: 'strtoupper(\'abc\')'
8094
});
8195
});
96+

package-lock.json

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "php-array-reader",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "Reads PHP arrays from a string into a JavaScript object",
55
"main": "index.js",
66
"types": "index.d.ts",

0 commit comments

Comments
 (0)