Skip to content

Commit

Permalink
Improve JSONConverter::chunkSize implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Oct 13, 2024
1 parent a422157 commit d70d5af
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/JsonConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

use function array_filter;
use function array_reduce;
use function array_values;
use function get_defined_constants;
use function is_resource;
use function is_string;
Expand Down Expand Up @@ -136,12 +135,16 @@ public static function create(): self
/**
* @param int<1, max> $depth
* @param int<1, max> $indentSize
* @param Closure(T, array-key): mixed $formatter
* @param int<1, max> $chunkSize
*
* @throws InvalidArgumentException
*/
private function __construct(int $flags, int $depth, int $indentSize, Closure $formatter, int $chunkSize)
{
json_encode([], $flags & ~JSON_THROW_ON_ERROR, $depth);

JSON_ERROR_NONE === json_last_error() || throw new InvalidArgumentException('The flags or the depth given are not valid JSON encoding parameters in PHP; '.json_last_error_msg());
JSON_ERROR_NONE === ($errorCode = json_last_error()) || throw new InvalidArgumentException('The flags or the depth given are not valid JSON encoding parameters in PHP; '.json_last_error_msg(), $errorCode);
1 <= $indentSize || throw new InvalidArgumentException('The indentation space must be greater or equal to 1.');
1 <= $chunkSize || throw new InvalidArgumentException('The chunk size must be greater or equal to 1.');

Expand Down Expand Up @@ -221,9 +224,9 @@ private function prettyPrint(string $json): string
public function __call(string $name, array $arguments): self|bool
{
return match (true) {
str_starts_with($name, 'without') => $this->removeFlags(self::methodToFlag()[lcfirst(substr($name, 7))] ?? throw new BadMethodCallException('The method "'.self::class.'::'.$name.'" does not exist.')),
str_starts_with($name, 'with') => $this->addFlags(self::methodToFlag()[lcfirst(substr($name, 4))] ?? throw new BadMethodCallException('The method "'.self::class.'::'.$name.'" does not exist.')),
str_starts_with($name, 'use') => $this->useFlags(self::methodToFlag()[lcfirst(substr($name, 3))] ?? throw new BadMethodCallException('The method "'.self::class.'::'.$name.'" does not exist.')),
str_starts_with($name, 'without') => $this->removeFlags(self::suffixToFlag()[lcfirst(substr($name, 7))] ?? throw new BadMethodCallException('The method "'.self::class.'::'.$name.'" does not exist.')),
str_starts_with($name, 'with') => $this->addFlags(self::suffixToFlag()[lcfirst(substr($name, 4))] ?? throw new BadMethodCallException('The method "'.self::class.'::'.$name.'" does not exist.')),
str_starts_with($name, 'use') => $this->useFlags(self::suffixToFlag()[lcfirst(substr($name, 3))] ?? throw new BadMethodCallException('The method "'.self::class.'::'.$name.'" does not exist.')),
default => throw new BadMethodCallException('The method "'.self::class.'::'.$name.'" does not exist.'),
};
}
Expand All @@ -233,12 +236,12 @@ public function __call(string $name, array $arguments): self|bool
*
* @return array<string, int>
*/
private static function methodToFlag(): array
private static function suffixToFlag(): array
{
static $methods;
static $suffix2Flag;

if (null === $methods) {
$methods = [];
if (null === $suffix2Flag) {
$suffix2Flag = [];
/** @var array<string, int> $jsonFlags */
$jsonFlags = get_defined_constants(true)['json'];
$jsonEncodeFlags = array_filter(
Expand All @@ -248,11 +251,11 @@ private static function methodToFlag(): array
);

foreach ($jsonEncodeFlags as $name => $value) {
$methods[lcfirst(str_replace('_', '', ucwords(strtolower(substr($name, 5)), '_')))] = $value;
$suffix2Flag[lcfirst(str_replace('_', '', ucwords(strtolower(substr($name, 5)), '_')))] = $value;
}
}

return $methods;
return $suffix2Flag;
}

/**
Expand Down

0 comments on commit d70d5af

Please sign in to comment.