From ff8782c7145db5c19f2b16caf7eaa6f723128138 Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Sun, 24 Nov 2024 15:57:53 +0100 Subject: [PATCH] Improve withPrettyPrint indentsize usage --- docs/9.0/converter/json.md | 16 ++++++++--- src/JsonConverter.php | 58 ++++++++++++++++---------------------- src/JsonConverterTest.php | 24 ++++++++++++++++ 3 files changed, 61 insertions(+), 37 deletions(-) diff --git a/docs/9.0/converter/json.md b/docs/9.0/converter/json.md index cd82fad7..b2104973 100644 --- a/docs/9.0/converter/json.md +++ b/docs/9.0/converter/json.md @@ -69,6 +69,17 @@ $converter->useHexQuot(); // returns false the flag is not used $converter->flags; //returns the actual flags value (as used by json_encode) ``` +

Starting with version 9.18.1 use +JsonConverter::withPrettyPrint to directly set the indent size with the argument $identSize +to its call

+ +```php +$converter = JsonConverter::create()->withPrettyPrint(2); +``` + +will produce a JSON with an indentation size of `2`. + + ### Json encode depth ```php @@ -85,7 +96,7 @@ $converter->depth; //returns the actual depth value (as used by json_encode) ### Json encode indentation -

This method is deprecated as of version 9.19.0 use +

This method is deprecated as of version 9.18.1 use JsonConverter::withPrettyPrint instead and add the $identSize argument to its call

@@ -103,9 +114,6 @@ size is the same as in PHP (ie : 4 characters long). $converter->indentSize; //returns the value used ``` -

When using withoutPrettyPrint, the indentSize -is automatically resetted to its default value of 4.

- ### Json encode formatter ```php diff --git a/src/JsonConverter.php b/src/JsonConverter.php index 0eac513d..2ba87846 100644 --- a/src/JsonConverter.php +++ b/src/JsonConverter.php @@ -53,47 +53,48 @@ * @template T * * @method JsonConverter withHexTag() adds the JSON_HEX_TAG flag - * @method JsonConverter withoutHexTag() adds the JSON_HEX_TAG flag + * @method JsonConverter withoutHexTag() removes the JSON_HEX_TAG flag * @method bool useHexTag() tells whether the JSON_HEX_TAG flag is used * @method JsonConverter withHexAmp() adds the JSON_HEX_TAG flag - * @method JsonConverter withoutHexAmp() adds the JSON_HEX_TAG flag + * @method JsonConverter withoutHexAmp() removes the JSON_HEX_TAG flag * @method bool useHexAmp() tells whether the JSON_HEX_TAG flag is used * @method JsonConverter withHexApos() adds the JSON_HEX_TAG flag - * @method JsonConverter withoutHexApos() adds the JSON_HEX_TAG flag + * @method JsonConverter withoutHexApos() removes the JSON_HEX_TAG flag * @method bool useHexApos() tells whether the JSON_HEX_TAG flag is used * @method JsonConverter withHexQuot() adds the JSON_HEX_TAG flag - * @method JsonConverter withoutHexQuot() adds the JSON_HEX_TAG flag + * @method JsonConverter withoutHexQuot() removes the JSON_HEX_TAG flag * @method bool useHexQuot() tells whether the JSON_HEX_TAG flag is used * @method JsonConverter withForceObject() adds the JSON_HEX_TAG flag - * @method JsonConverter withoutForceObject() adds the JSON_HEX_TAG flag + * @method JsonConverter withoutForceObject() removes the JSON_HEX_TAG flag * @method bool useForceObject() tells whether the JSON_HEX_TAG flag is used * @method JsonConverter withNumericCheck() adds the JSON_HEX_TAG flag - * @method JsonConverter withoutNumericCheck() adds the JSON_HEX_TAG flag + * @method JsonConverter withoutNumericCheck() removes the JSON_HEX_TAG flag * @method bool useNumericCheck() tells whether the JSON_HEX_TAG flag is used * @method JsonConverter withUnescapedSlashes() adds the JSON_HEX_TAG flag - * @method JsonConverter withoutUnescapedSlashes() adds the JSON_HEX_TAG flag + * @method JsonConverter withoutUnescapedSlashes() removes the JSON_HEX_TAG flag * @method bool useUnescapedSlashes() tells whether the JSON_HEX_TAG flag is used + * @method JsonConverter withoutPrettyPrint() removes the JSON_PRETTY_PRINT flag * @method bool usePrettyPrint() tells whether the JSON_HEX_TAG flag is used * @method JsonConverter withUnescapedUnicode() adds the JSON_HEX_TAG flag - * @method JsonConverter withoutUnescapedUnicode() adds the JSON_HEX_TAG flag + * @method JsonConverter withoutUnescapedUnicode() removes the JSON_HEX_TAG flag * @method bool useUnescapedUnicode() tells whether the JSON_HEX_TAG flag is used * @method JsonConverter withPartialOutputOnError() adds the JSON_HEX_TAG flag - * @method JsonConverter withoutPartialOutputOnError() adds the JSON_HEX_TAG flag + * @method JsonConverter withoutPartialOutputOnError() removes the JSON_HEX_TAG flag * @method bool usePartialOutputOnError() tells whether the JSON_HEX_TAG flag is used * @method JsonConverter withPreserveZeroFraction() adds the JSON_HEX_TAG flag - * @method JsonConverter withoutPreserveZeroFraction() adds the JSON_HEX_TAG flag + * @method JsonConverter withoutPreserveZeroFraction() removes the JSON_HEX_TAG flag * @method bool usePreserveZeroFraction() tells whether the JSON_HEX_TAG flag is used * @method JsonConverter withUnescapedLineTerminators() adds the JSON_HEX_TAG flag - * @method JsonConverter withoutUnescapedLineTerminators() adds the JSON_HEX_TAG flag + * @method JsonConverter withoutUnescapedLineTerminators() removes the JSON_HEX_TAG flag * @method bool useUnescapedLineTerminators() tells whether the JSON_HEX_TAG flag is used * @method JsonConverter withInvalidUtf8Ignore() adds the JSON_HEX_TAG flag - * @method JsonConverter withoutInvalidUtf8Ignore() adds the JSON_HEX_TAG flag + * @method JsonConverter withoutInvalidUtf8Ignore() removes the JSON_HEX_TAG flag * @method bool useInvalidUtf8Ignore() tells whether the JSON_HEX_TAG flag is used * @method JsonConverter withInvalidUtf8Substitute() adds the JSON_HEX_TAG flag - * @method JsonConverter withoutInvalidUtf8Substitute() adds the JSON_HEX_TAG flag + * @method JsonConverter withoutInvalidUtf8Substitute() removes the JSON_HEX_TAG flag * @method bool useInvalidUtf8Substitute() tells whether the JSON_HEX_TAG flag is used * @method JsonConverter withThrowOnError() adds the JSON_HEX_TAG flag - * @method JsonConverter withoutThrowOnError() adds the JSON_HEX_TAG flag + * @method JsonConverter withoutThrowOnError() removes the JSON_HEX_TAG flag * @method bool useThrowOnError() tells whether the JSON_HEX_TAG flag is used */ final class JsonConverter @@ -230,26 +231,17 @@ public function __call(string $name, array $arguments): self|bool } /** - * @param int<1, max> $indentSize + * @param int<1, max>|null $indentSize */ - public function withPrettyPrint(int $indentSize = 4): self + public function withPrettyPrint(?int $indentSize = null): self { - $instance = $this->addFlags(JSON_PRETTY_PRINT); - if ($indentSize === $instance->indentSize) { - return $instance; - } + $flags = $this->flags | JSON_PRETTY_PRINT; + $indentSize = $indentSize ?? $this->indentSize; - return new self($instance->flags, $instance->depth, $indentSize, $instance->formatter, $instance->chunkSize); - } - - public function withoutPrettyPrint(): self - { - $instance = $this->removeFlags(JSON_PRETTY_PRINT); - if (4 === $instance->indentSize) { - return $instance; - } - - return new self($instance->flags, $instance->depth, 4, $instance->formatter, $instance->chunkSize); + return match (true) { + $flags === $this->flags && $indentSize === $this->indentSize => $this, + default => new self($flags, $this->depth, $indentSize, $this->formatter, $this->chunkSize), + }; } /** @@ -496,14 +488,14 @@ public function convert(iterable $records): Iterator * DEPRECATION WARNING! This method will be removed in the next major point release. * * @see JsonConverter::withPrettyPrint() - * @deprecated Since version 9.19.0 + * @deprecated Since version 9.18.1 * @codeCoverageIgnore * * Set the indentation size. * * @param int<1, max> $indentSize */ - #[Deprecated(message:'use League\Csv\JsonConverter::withPrettyPrint() instead', since:'league/csv:9.19.0')] + #[Deprecated(message:'use League\Csv\JsonConverter::withPrettyPrint() instead', since:'league/csv:9.18.1')] public function indentSize(int $indentSize): self { return match ($indentSize) { diff --git a/src/JsonConverterTest.php b/src/JsonConverterTest.php index bb081e92..05a5838c 100644 --- a/src/JsonConverterTest.php +++ b/src/JsonConverterTest.php @@ -172,4 +172,28 @@ public function it_fails_to_write_to_the_destination_path_if_it_is_open_in_read_ JsonConverter::create()->save([['foo' => 'bar']], $stream); } + + #[Test] + public function it_can_set_the_indentation_size_using_pretty_print(): void + { + $converter = JsonConverter::create(); + self::assertSame(4, $converter->indentSize); + self::assertFalse($converter->usePrettyPrint()); + + $converter = $converter->withPrettyPrint(); + self::assertSame(4, $converter->indentSize); + self::assertTrue($converter->usePrettyPrint()); + + $converter = $converter->withPrettyPrint(2); + self::assertSame(2, $converter->indentSize); + self::assertTrue($converter->usePrettyPrint()); + + $converter = $converter->withPrettyPrint(); + self::assertSame(2, $converter->indentSize); + self::assertTrue($converter->usePrettyPrint()); + + $converter = $converter->withoutPrettyPrint(); + self::assertSame(2, $converter->indentSize); + self::assertFalse($converter->usePrettyPrint()); + } }