Skip to content

Commit

Permalink
Improve withPrettyPrint indentsize usage
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Nov 24, 2024
1 parent 3962454 commit ff8782c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 37 deletions.
16 changes: 12 additions & 4 deletions docs/9.0/converter/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```

<p class="message-info">Starting with version <code>9.18.1</code> use
<code>JsonConverter::withPrettyPrint</code> to directly set the indent size with the argument <code>$identSize</code>
to its call</p>

```php
$converter = JsonConverter::create()->withPrettyPrint(2);
```

will produce a JSON with an indentation size of `2`.


### Json encode depth

```php
Expand All @@ -85,7 +96,7 @@ $converter->depth; //returns the actual depth value (as used by json_encode)

### Json encode indentation

<p class="message-warning">This method is deprecated as of version <code>9.19.0</code> use
<p class="message-warning">This method is deprecated as of version <code>9.18.1</code> use
<code>JsonConverter::withPrettyPrint</code> instead and add the <code>$identSize</code> argument
to its call</p>

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

<p class="message-notice">When using <code>withoutPrettyPrint</code>, the <code>indentSize</code>
is automatically resetted to its default value of <code>4</code>.</p>

### Json encode formatter

```php
Expand Down
58 changes: 25 additions & 33 deletions src/JsonConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
};
}

/**
Expand Down Expand Up @@ -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) {
Expand Down
24 changes: 24 additions & 0 deletions src/JsonConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit ff8782c

Please sign in to comment.