Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for trailing comma in arrays. #16

Merged
merged 2 commits into from
Feb 7, 2021
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
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,31 @@ VarExporter::export([

Types considered scalar here are `int`, `bool`, `float`, `string` and `null`.

### `VarExporter::TRAILING_COMMA_IN_ARRAY`

Add trailing comma for last item of an array:

```php
VarExporter::export([
'one' => ['hello', 'world', 123, true, false, null, 7.5],
'two' => ['hello', 'world', ['one', 'two', 'three']]
], VarExporter::TRAILING_COMMA_IN_ARRAY | VarExporter::INLINE_NUMERIC_SCALAR_ARRAY);
```

```php
[
'one' => ['hello', 'world', 123, true, false, null, 7.5],
'two' => [
'hello',
'world',
['one', 'two', 'three'],
],
]
```

Trailing comma won't be added to inlined arrays if this flag is used along with
`VarExporter::INLINE_NUMERIC_SCALAR_ARRAY`.

### `VarExporter::CLOSURE_SNAPSHOT_USES`

Export the current value of each `use()` variable as expression inside the exported closure.
Expand Down
8 changes: 7 additions & 1 deletion src/Internal/GenericExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ final class GenericExporter
*/
public $closureSnapshotUses;

/**
* @var bool
*/
public $trailingCommaInArray;

/**
* @param int $options
*/
Expand Down Expand Up @@ -81,6 +86,7 @@ public function __construct(int $options)
$this->skipDynamicProperties = (bool) ($options & VarExporter::SKIP_DYNAMIC_PROPERTIES);
$this->inlineNumericScalarArray = (bool) ($options & VarExporter::INLINE_NUMERIC_SCALAR_ARRAY);
$this->closureSnapshotUses = (bool) ($options & VarExporter::CLOSURE_SNAPSHOT_USES);
$this->trailingCommaInArray = (bool) ($options & VarExporter::TRAILING_COMMA_IN_ARRAY);
}

/**
Expand Down Expand Up @@ -163,7 +169,7 @@ public function exportArray(array $array, array $path, array $parentIds) : array
$prepend = var_export($key, true) . ' => ';
}

if (! $isLast) {
if (! $isLast || $this->trailingCommaInArray) {
$append = ',';
}

Expand Down
6 changes: 6 additions & 0 deletions src/VarExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ final class VarExporter
*/
public const CLOSURE_SNAPSHOT_USES = 1 << 8;

/**
* Add trailing comma for last item of an array.
* Trailing comma won't be added to inlined arrays even if this flag is used.
*/
public const TRAILING_COMMA_IN_ARRAY = 1 << 9;

/**
* @param mixed $var The variable to export.
* @param int $options A bitmask of options. Possible values are `VarExporter::*` constants.
Expand Down
21 changes: 21 additions & 0 deletions tests/VarExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,27 @@ public function testInlineNumericScalarArray()
$this->assertExportEquals($expected, $var, VarExporter::INLINE_NUMERIC_SCALAR_ARRAY);
}

public function testTrailingCommaInArray()
{
$var = [
'one' => ['hello', 'world', 123, true, false, null, 7.5],
'two' => ['hello', 'world', ['one', 'two', 'three']]
];

$expected = <<<'PHP'
[
'one' => ['hello', 'world', 123, true, false, null, 7.5],
'two' => [
'hello',
'world',
['one', 'two', 'three'],
],
]
PHP;

$this->assertExportEquals($expected, $var, VarExporter::INLINE_NUMERIC_SCALAR_ARRAY | VarExporter::TRAILING_COMMA_IN_ARRAY);
}

public function testExportDateTime()
{
$timezone = new \DateTimeZone('Europe/Berlin');
Expand Down