From 613452588a7588aa0ad7b3966b8983f82f529d41 Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Fri, 15 Nov 2024 18:02:54 +0100 Subject: [PATCH] deprecate JsonConverter::indentSize method --- CHANGELOG.md | 6 ++-- docs/9.0/converter/json.md | 12 ++++++-- src/JsonConverter.php | 57 ++++++++++++++++++++++++++++---------- src/JsonConverterTest.php | 15 ++-------- 4 files changed, 58 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d99e867..f627f762 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,12 +10,12 @@ All Notable changes to `Csv` will be documented in this file ### Deprecated -- None +- `JsonConverter::indentSize` ### Fixed -- None -- +- `JsonConverter::withPrettyPrint` now accepts the `$identSize` as its unique parameter. + ### Remove - None diff --git a/docs/9.0/converter/json.md b/docs/9.0/converter/json.md index afe04c8f..cd82fad7 100644 --- a/docs/9.0/converter/json.md +++ b/docs/9.0/converter/json.md @@ -85,6 +85,10 @@ $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 +JsonConverter::withPrettyPrint instead and add the $identSize argument +to its call

+ ```php public JsonConverter::indentSize(int $indentSize): self ``` @@ -93,11 +97,15 @@ This method sets the JSON indentation size value if you use the `JSON_PRETTY_PRI all other situation this value stored via this method is never used. By default, the indentation size is the same as in PHP (ie : 4 characters long). -```php -$converter = JsonConverter::create()->indentSize(2); +```diff +- $converter = JsonConverter::create()->indentSize(2); ++ $converter = JsonConverter::create()->withPrettyPrint(2); $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 3f666230..9ad31ffe 100644 --- a/src/JsonConverter.php +++ b/src/JsonConverter.php @@ -72,8 +72,6 @@ * @method JsonConverter withUnescapedSlashes() adds the JSON_HEX_TAG flag * @method JsonConverter withoutUnescapedSlashes() adds the JSON_HEX_TAG flag * @method bool useUnescapedSlashes() tells whether the JSON_HEX_TAG flag is used - * @method JsonConverter withPrettyPrint() adds the JSON_HEX_TAG flag - * @method JsonConverter withoutPrettyPrint() adds the JSON_HEX_TAG 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 @@ -230,6 +228,29 @@ public function __call(string $name, array $arguments): self|bool }; } + /** + * @param int<1, max> $indentSize + */ + public function withPrettyPrint(int $indentSize = 4): self + { + $instance = $this->addFlags(JSON_PRETTY_PRINT); + if ($indentSize === $instance->indentSize) { + return $instance; + } + + 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); + } + /** * Returns the PHP json flag associated to its method suffix to ease method lookup. */ @@ -315,19 +336,6 @@ public function depth(int $depth): self }; } - /** - * Set the indentation size. - * - * @param int<1, max> $indentSize - */ - public function indentSize(int $indentSize): self - { - return match ($indentSize) { - $this->indentSize => $this, - default => new self($this->flags, $this->depth, $indentSize, $this->formatter, $this->chunkSize), - }; - } - /** * Set the indentation size. * @@ -482,4 +490,23 @@ public function convert(iterable $records): Iterator yield ($this->jsonEncodeChunk)([$offset => $current]).$this->end; } + + /** + * DEPRECATION WARNING! This method will be removed in the next major point release. + * + * @see JsonConverter::withPrettyPrint() + * @deprecated Since version 9.19.0 + * @codeCoverageIgnore + * + * Set the indentation size. + * + * @param int<1, max> $indentSize + */ + public function indentSize(int $indentSize): self + { + return match ($indentSize) { + $this->indentSize => $this, + default => new self($this->flags, $this->depth, $indentSize, $this->formatter, $this->chunkSize), + }; + } } diff --git a/src/JsonConverterTest.php b/src/JsonConverterTest.php index d94fb51c..7a93901e 100644 --- a/src/JsonConverterTest.php +++ b/src/JsonConverterTest.php @@ -66,7 +66,6 @@ public function it_has_default_values(): void self::assertSame( $converter, $converter - ->indentSize(4) ->addFlags(0) ->removeFlags(0) ->depth(512) @@ -87,7 +86,7 @@ public function it_fails_if_the_indentation_size_is_invalud(): void { $this->expectException(InvalidArgumentException::class); - JsonConverter::create()->indentSize(0); /* @phpstan-ignore-line */ + JsonConverter::create()->withPrettyPrint(0); /* @phpstan-ignore-line */ } #[Test] @@ -95,16 +94,8 @@ public function it_fails_if_the_chunk_size_is_invalud(): void { $this->expectException(InvalidArgumentException::class); - JsonConverter::create()->chunkSize(0); /* @phpstan-ignore-line */ - } - - #[Test] - public function it_only_uses_indentation_if_pretty_print_is_present(): void - { - self::assertSame( - json_encode([['foo' => 'bar']]), - JsonConverter::create()->indentSize(23)->encode([['foo' => 'bar']]), - ); + JsonConverter::create()->chunkSize(0); + /* @phpstan-ignore-line */ } #[Test]