-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add toJson() method for exporting JSON string
- Loading branch information
1 parent
e70ddee
commit b6bfd87
Showing
5 changed files
with
109 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
); |