From b6bfd875c69705e891acde5576afbd1a33803ee3 Mon Sep 17 00:00:00 2001 From: Roberto Butti Date: Sat, 22 Jun 2024 15:00:31 +0200 Subject: [PATCH] Add toJson() method for exporting JSON string --- CHANGELOG.md | 3 ++ README.md | 14 ++++++ src/Block.php | 11 ++--- src/Traits/ExportableBlock.php | 25 +++++++++++ tests/Unit/Traits/ExportableTest.php | 64 ++++++++++++++++++++++++++++ 5 files changed, 109 insertions(+), 8 deletions(-) create mode 100644 src/Traits/ExportableBlock.php create mode 100644 tests/Unit/Traits/ExportableTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 740f874..e0d0197 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 0.1.1 - 2024-06-22 +- Add toJson() method for exporting JSON string + ## 0.1.0 - 2024-06-21 - Cleaning and refactoring the behavior of returning Block or native array in loops diff --git a/README.md b/README.md index 08ae070..70842a9 100644 --- a/README.md +++ b/README.md @@ -238,6 +238,8 @@ Array */ ``` +## Exporting data + ### Exporting to array with `toArray()` In the case you need to access the native array (associative and nester), you can use the `toArray()` method. @@ -251,6 +253,18 @@ $composerContent = Block::fromJsonFile($file); $array = $composerContent->toArray(); ``` +### Exporting to JSON string with `toJson()` +In the case you need to generate a valid JSON string with the content of the Block object, you can use the `toJson()` method. + +This is helpful when you are manipulating data with the Block class and at a certain point need to send the data in JSON string format to your own function or a function from a third-party package that expects to receive a JSON string as a parameter. + +```php +$data = Block::make($fruitsArray); +$jsonString = $data->toJson(); // JSON string with "pretty print" +``` + +## Loading Data + ### Loading Data from JSON file ```php diff --git a/src/Block.php b/src/Block.php index f577fbf..685badf 100644 --- a/src/Block.php +++ b/src/Block.php @@ -8,6 +8,7 @@ use Countable; use HiFolks\DataType\Traits\EditableBlock; use HiFolks\DataType\Traits\QueryableBlock; +use HiFolks\DataType\Traits\ExportableBlock; use Iterator; /** @@ -21,6 +22,7 @@ final class Block implements Iterator, ArrayAccess, Countable { use QueryableBlock; use EditableBlock; + use ExportableBlock; /** @var array */ private array $data; @@ -299,13 +301,6 @@ public function keys(bool $returnBlockClass = false): int|string|array|Block - /** - * Returns the native array - * @return array - */ - public function toArray(): array - { - return $this->data; - } + } diff --git a/src/Traits/ExportableBlock.php b/src/Traits/ExportableBlock.php new file mode 100644 index 0000000..1635345 --- /dev/null +++ b/src/Traits/ExportableBlock.php @@ -0,0 +1,25 @@ + + */ + public function toArray(): array + { + return $this->data; + } + + + /** + * Returns the JSON String (pretty format by default) + * @return string|false + */ + public function toJson(): string|false + { + return json_encode($this->data, JSON_PRETTY_PRINT); + } +} diff --git a/tests/Unit/Traits/ExportableTest.php b/tests/Unit/Traits/ExportableTest.php new file mode 100644 index 0000000..dae8a15 --- /dev/null +++ b/tests/Unit/Traits/ExportableTest.php @@ -0,0 +1,64 @@ + + [ + 'name' => 'Avocado', + 'fruit' => '🥑', + 'wikipedia' => 'https://en.wikipedia.org/wiki/Avocado', + 'color' => 'green', + 'rating' => 8, + ], + "apple" => + [ + 'name' => 'Apple', + 'fruit' => '🍎', + 'wikipedia' => 'https://en.wikipedia.org/wiki/Apple', + 'color' => 'red', + 'rating' => 7, + ], + "banana" => + [ + 'name' => 'Banana', + 'fruit' => '🍌', + 'wikipedia' => 'https://en.wikipedia.org/wiki/Banana', + 'color' => 'yellow', + 'rating' => 8.5, + ], + "cherry" => + [ + 'name' => 'Cherry', + 'fruit' => '🍒', + 'wikipedia' => 'https://en.wikipedia.org/wiki/Cherry', + 'color' => 'red', + 'rating' => 9, + ], +]; + +test( + 'Test toJson', + function () use ($fruitsArray): void { + $data = Block::make($fruitsArray); + expect($data->toJson())->toBeString(); + expect(strlen($data->toJson()))->toBe(773); + $string = $data->toJson(); + $data1 = Block::fromJsonString($string); + expect($data1->get("0.fruit"))->toBe($data->get("0.fruit")); + }, +); +test( + 'Test toJson with different iterateBlock', + function () use ($fruitsArray): void { + $data1 = Block::make($fruitsArray, true); + $string1 = $data1->toJson(); + $data2 = Block::make($fruitsArray, false); + $string2 = $data2->toJson(); + expect($string1)->toBeString(); + expect($string2)->toBeString(); + expect(strlen($string1))->toBe(773); + expect(strlen($string2))->toBe(773); + expect($string1)->toBe($string2); + }, +);