Skip to content

Commit

Permalink
deprecate JsonConverter::indentSize method
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Nov 15, 2024
1 parent c0619cb commit 6134525
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 32 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions docs/9.0/converter/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ $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
<code>JsonConverter::withPrettyPrint</code> instead and add the <code>$identSize</code> argument
to its call</p>

```php
public JsonConverter::indentSize(int $indentSize): self
```
Expand All @@ -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
```

<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
57 changes: 42 additions & 15 deletions src/JsonConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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),
};
}
}
15 changes: 3 additions & 12 deletions src/JsonConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public function it_has_default_values(): void
self::assertSame(
$converter,
$converter
->indentSize(4)
->addFlags(0)
->removeFlags(0)
->depth(512)
Expand All @@ -87,24 +86,16 @@ 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]
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);

Check failure on line 97 in src/JsonConverterTest.php

View workflow job for this annotation

GitHub Actions / PHP on 8.3 - prefer-stable -

Parameter #1 $chunkSize of method League\Csv\JsonConverter<mixed>::chunkSize() expects int<1, max>, 0 given.
/* @phpstan-ignore-line */

Check failure on line 98 in src/JsonConverterTest.php

View workflow job for this annotation

GitHub Actions / PHP on 8.3 - prefer-stable -

No error to ignore is reported on line 98.
}

#[Test]
Expand Down

0 comments on commit 6134525

Please sign in to comment.