Skip to content

Commit

Permalink
Improve SimpleDTO::toJson (#284)
Browse files Browse the repository at this point in the history
Co-authored-by: Deeka Wong <[email protected]>
  • Loading branch information
huangdijia and huangdijia authored Jul 17, 2023
1 parent 20f3e5e commit 30f013b
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/SimpleDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,24 @@ public function toArray(): array
/**
* Returns the DTO validated data in a JSON string format.
*/
public function toJson(bool $pretty = false): string
public function toJson(int|bool $flags = 0, int $depth = 512): string
{
return $pretty
? json_encode($this->buildDataForExport(), JSON_PRETTY_PRINT)
: json_encode($this->buildDataForExport());
// compat with the flags is bool type
$flags = (int) match ($flags) {
true => JSON_PRETTY_PRINT,
false => 0,
default => $flags,
};

return json_encode($this->buildDataForExport(), $flags, $depth);
}

/**
* Returns the DTO validated data in a pretty JSON string format.
*/
public function toPrettyJson(): string
public function toPrettyJson(int $flags = 0, int $depth = 512): string
{
return json_encode($this->buildDataForExport(), JSON_PRETTY_PRINT);
return $this->toJson($flags | JSON_PRETTY_PRINT, $depth);
}

/**
Expand Down

0 comments on commit 30f013b

Please sign in to comment.