Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toJson() method for exporting JSON string #5

Merged
merged 1 commit into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand Down
11 changes: 3 additions & 8 deletions src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Countable;
use HiFolks\DataType\Traits\EditableBlock;
use HiFolks\DataType\Traits\QueryableBlock;
use HiFolks\DataType\Traits\ExportableBlock;
use Iterator;

/**
Expand All @@ -21,6 +22,7 @@ final class Block implements Iterator, ArrayAccess, Countable
{
use QueryableBlock;
use EditableBlock;
use ExportableBlock;

/** @var array<int|string, mixed> */
private array $data;
Expand Down Expand Up @@ -299,13 +301,6 @@ public function keys(bool $returnBlockClass = false): int|string|array|Block



/**
* Returns the native array
* @return array<int|string, mixed>
*/
public function toArray(): array
{
return $this->data;
}


}
25 changes: 25 additions & 0 deletions src/Traits/ExportableBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace HiFolks\DataType\Traits;

trait ExportableBlock
{
/**
* Returns the native array
* @return array<int|string, mixed>
*/
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);
}
}
64 changes: 64 additions & 0 deletions tests/Unit/Traits/ExportableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

use HiFolks\DataType\Block;

$fruitsArray = [
"avocado" =>
[
'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);
},
);