Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This small JS utility reads PHP strings containing arrays and returns a JavaScri
It uses [glayzzle/php-parser](https://github.com/glayzzle/php-parser) to parse PHP into AST and uses that
info to extract arrays.
It supports both indexed and associative arrays (i.e. lists and dictionaries/maps) and array, string, numeric and null values.
Inline function calls are not evaluated but returned as raw strings. See the example below.

## Installation

Expand Down Expand Up @@ -35,6 +36,7 @@ const phpString = `[
'and_numeric' => 42,
'what_about' => true,
'or' => false,
'func' => strtoupper('abc'),
]`;
const data = fromString(phpString);
```
Expand All @@ -50,7 +52,8 @@ const data = fromString(phpString);
also_supports: null,
and_numeric: 42,
what_about: true,
or: false
or: false,
func: "strtoupper('abc')"
}
```

Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ function fromString (phpString) {
extractDoc: false,
suppressErrors: true
},
ast: { withPositions: false }
ast: {
withPositions: true,
withSource: true
}
});

phpString = phpString.trim();
Expand Down Expand Up @@ -72,6 +75,7 @@ function parseValue (expr) {
if (expr.kind === 'boolean') return expr.value;
if (expr.kind === 'nullkeyword') return null;
if (expr.kind === 'identifier' && expr.name.name === 'null') return null;
if (expr.kind === 'call') return expr.loc?.source;
return undefined;
}

Expand Down
19 changes: 17 additions & 2 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,19 @@ test('php file', () => {
expect(data).toEqual(['foo', 'bar']);
});

test('all value types match (arrays strings numbers decimals nulls booleans)', () => {
test('function calls', () => {
const data = fromString(`[
'func' => env('SOME_VAR', true),
'another' => substr($var, 0, 5),
'arrow' => (fn() => 123),
]`);
expect(data).toEqual({
func : "env('SOME_VAR', true)",
another: "substr($var, 0, 5)",
});
})

test('all value types match (arrays strings numbers decimals nulls booleans functions)', () => {
const data = fromString(`[
'arrayvalue' => [
'hello', 'world'
Expand All @@ -68,6 +80,7 @@ test('all value types match (arrays strings numbers decimals nulls booleans)', (
'nullvalue' => null,
'truevalue' => true,
'falsevalue' => false,
'functionvalue' => strtoupper('abc'),
]`);
expect(data).toEqual({
arrayvalue: ['hello', 'world'],
Expand All @@ -76,6 +89,8 @@ test('all value types match (arrays strings numbers decimals nulls booleans)', (
decimalvalue: 4.2,
nullvalue: null,
truevalue: true,
falsevalue: false
falsevalue: false,
functionvalue: 'strtoupper(\'abc\')'
});
});

22 changes: 11 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "php-array-reader",
"version": "2.0.0",
"version": "2.1.0",
"description": "Reads PHP arrays from a string into a JavaScript object",
"main": "index.js",
"types": "index.d.ts",
Expand Down