From d6082cd18efc06c8a9c3590e5ef691c87c75062d Mon Sep 17 00:00:00 2001 From: Benjamin Morel Date: Thu, 30 Jun 2022 01:33:50 +0200 Subject: [PATCH] Add INLINE_ARRAY option --- CHANGELOG.md | 4 ++++ README.md | 21 +++++++++++++++++++++ src/Internal/GenericExporter.php | 16 ++++++++++++++-- src/VarExporter.php | 6 ++++++ tests/VarExporterTest.php | 14 ++++++++++++++ 5 files changed, 59 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 15ef2bd..e2ea7b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## UNRELEASED (0.3.7) +✨ **New feature** + +- New option: `VarExporter::INLINE_ARRAY` + 🗑️ **Deprecated** - The `VarExporter::INLINE_NUMERIC_SCALAR_ARRAY` is deprecated, please use `INLINE_SCALAR_LIST` instead diff --git a/README.md b/README.md index 958e7c3..bc4f8c6 100644 --- a/README.md +++ b/README.md @@ -451,6 +451,25 @@ Disallows exporting any custom object using direct property access and bound clo Disallows exporting closures. +### `VarExporter::INLINE_ARRAY` + +Formats arrays on a single line: + +```php +VarExporter::export([ + 'one' => ['hello', 'world', 123, true, false, null, 7.5], + 'two' => ['hello', 'world', [ + 'one', + 'two', + 'three' + ]] +], VarExporter::INLINE_ARRAY); +``` + +```php +['one' => ['hello', 'world', 123, true, false, null, 7.5], 'two' => ['hello', 'world', ['one', 'two', 'three']]] +``` + ### `VarExporter::INLINE_SCALAR_LIST` Formats numeric arrays containing only scalar values on a single line: @@ -475,6 +494,8 @@ VarExporter::export([ Types considered scalar here are `int`, `bool`, `float`, `string` and `null`. +This option is a subset of `INLINE_ARRAY`, and has no effect when `INLINE_ARRAY` is used. + ### `VarExporter::TRAILING_COMMA_IN_ARRAY` Adds a trailing comma after the last item of *non-inline* arrays: diff --git a/src/Internal/GenericExporter.php b/src/Internal/GenericExporter.php index a2de531..6b520a2 100644 --- a/src/Internal/GenericExporter.php +++ b/src/Internal/GenericExporter.php @@ -46,6 +46,13 @@ final class GenericExporter */ public $skipDynamicProperties; + /** + * @psalm-readonly + * + * @var bool + */ + public $inlineArray; + /** * @psalm-readonly * @@ -102,6 +109,7 @@ public function __construct(int $options, int $indentLevel = 0) $this->addTypeHints = (bool) ($options & VarExporter::ADD_TYPE_HINTS); $this->skipDynamicProperties = (bool) ($options & VarExporter::SKIP_DYNAMIC_PROPERTIES); + $this->inlineArray = (bool) ($options & VarExporter::INLINE_ARRAY); $this->inlineScalarList = (bool) ($options & VarExporter::INLINE_SCALAR_LIST); $this->closureSnapshotUses = (bool) ($options & VarExporter::CLOSURE_SNAPSHOT_USES); $this->trailingCommaInArray = (bool) ($options & VarExporter::TRAILING_COMMA_IN_ARRAY); @@ -169,7 +177,7 @@ public function exportArray(array $array, array $path, array $parentIds) : array $current = 0; - $inline = ($this->inlineScalarList && $isList && $this->isScalarList($array)); + $inline = $this->inlineArray || ($this->inlineScalarList && $isList && $this->isScalarList($array)); foreach ($array as $key => $value) { $isLast = (++$current === $count); @@ -180,7 +188,11 @@ public function exportArray(array $array, array $path, array $parentIds) : array $exported = $this->export($value, $newPath, $parentIds); if ($inline) { - $result[] = $exported[0]; + if ($isList) { + $result[] = $exported[0]; + } else { + $result[] = var_export($key, true) . ' => ' . $exported[0]; + } } else { $prepend = ''; $append = ''; diff --git a/src/VarExporter.php b/src/VarExporter.php index f7e05db..21d6e4f 100644 --- a/src/VarExporter.php +++ b/src/VarExporter.php @@ -50,6 +50,7 @@ final class VarExporter /** * Formats lists (0-based numeric arrays) containing only scalar values on a single line. * Types considered scalar here are int, bool, float, string and null. + * This option is a subset of INLINE_ARRAY, and has no effect when INLINE_ARRAY is used. */ public const INLINE_SCALAR_LIST = 1 << 7; @@ -73,6 +74,11 @@ final class VarExporter */ public const NO_ENUMS = 1 << 10; + /** + * Formats all arrays on a single line. + */ + public const INLINE_ARRAY = 1 << 11; + /** * @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 7bdfa30..8151fb1 100644 --- a/tests/VarExporterTest.php +++ b/tests/VarExporterTest.php @@ -182,6 +182,20 @@ public function testAddReturn(): void $this->assertExportEquals($expected, $var, VarExporter::ADD_RETURN); } + public function testInlineArray(): void + { + $var = [ + 'one' => ['hello', 'world', 123, true, false, null, 7.5], + 'two' => ['hello', 'world', [1 => 'one', 'two', 'three']] + ]; + + $expected = <<<'PHP' +['one' => ['hello', 'world', 123, true, false, null, 7.5], 'two' => ['hello', 'world', [1 => 'one', 2 => 'two', 3 => 'three']]] +PHP; + + $this->assertExportEquals($expected, $var, VarExporter::INLINE_ARRAY); + } + public function testInlineScalarList(): void { $var = [