Skip to content

Commit

Permalink
Add support for trailing comma in arrays. (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad authored and BenMorel committed May 10, 2024
1 parent 831d948 commit eaa8614
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
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

0 comments on commit eaa8614

Please sign in to comment.