diff --git a/README.md b/README.md index ec4b7a3..906bc61 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/Internal/GenericExporter.php b/src/Internal/GenericExporter.php index 48c0d8d..23629ad 100644 --- a/src/Internal/GenericExporter.php +++ b/src/Internal/GenericExporter.php @@ -52,6 +52,11 @@ final class GenericExporter */ public $closureSnapshotUses; + /** + * @var bool + */ + public $trailingCommaInArray; + /** * @param int $options */ @@ -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); } /** @@ -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 = ','; } diff --git a/src/VarExporter.php b/src/VarExporter.php index 8f6e2b9..0185554 100644 --- a/src/VarExporter.php +++ b/src/VarExporter.php @@ -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. diff --git a/tests/VarExporterTest.php b/tests/VarExporterTest.php index 381a627..7462d23 100644 --- a/tests/VarExporterTest.php +++ b/tests/VarExporterTest.php @@ -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');