Skip to content

Commit bc143f2

Browse files
committed
feat(arr): implement jsonEncode
1 parent 3ac0e3a commit bc143f2

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

packages/support/src/Arr/ManipulatesArray.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
use Closure;
88
use Stringable;
99
use Tempest\Support\Str\ImmutableString;
10+
use Tempest\Support\Str\MutableString;
1011

12+
use function Tempest\Support\Json\encode;
1113
use function Tempest\Support\tap;
1214

1315
/**
@@ -814,6 +816,16 @@ public function dd(mixed ...$dd): void
814816
ld($this->value, ...$dd);
815817
}
816818

819+
/**
820+
* Encodes the array as JSON and returns a string helper instance.
821+
*/
822+
public function jsonEncode(bool $pretty = false, bool $mutable = false): ImmutableString|MutableString
823+
{
824+
$json = encode($this->value, $pretty);
825+
826+
return $mutable ? new MutableString($json) : new ImmutableString($json);
827+
}
828+
817829
/**
818830
* Returns the underlying array of the instance.
819831
*

packages/support/tests/Arr/ManipulatesArrayTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,4 +1806,50 @@ public function test_partition(): void
18061806
{
18071807
$this->assertSame([[true, true], [false]], arr([true, true, false])->partition(fn (bool $value) => $value === true)->toArray());
18081808
}
1809+
1810+
public function test_json_encode(): void
1811+
{
1812+
$data = ['name' => 'tempest', 'version' => '1.0', 'tags' => ['php', 'framework']];
1813+
$result = arr($data)->jsonEncode();
1814+
1815+
$this->assertInstanceOf(\Tempest\Support\Str\ImmutableString::class, $result);
1816+
$this->assertSame('{"name":"tempest","version":"1.0","tags":["php","framework"]}', $result->toString());
1817+
}
1818+
1819+
public function test_json_encode_pretty(): void
1820+
{
1821+
$data = ['name' => 'tempest', 'version' => '1.0'];
1822+
$result = arr($data)->jsonEncode(pretty: true);
1823+
1824+
$this->assertInstanceOf(\Tempest\Support\Str\ImmutableString::class, $result);
1825+
$this->assertStringContainsString("{\n", $result->toString());
1826+
$this->assertStringContainsString('"name": "tempest"', $result->toString());
1827+
$this->assertStringContainsString('"version": "1.0"', $result->toString());
1828+
}
1829+
1830+
public function test_json_encode_mutable(): void
1831+
{
1832+
$data = ['name' => 'tempest', 'version' => '1.0'];
1833+
$result = arr($data)->jsonEncode(mutable: true);
1834+
1835+
$this->assertInstanceOf(\Tempest\Support\Str\MutableString::class, $result);
1836+
$this->assertSame('{"name":"tempest","version":"1.0"}', $result->toString());
1837+
}
1838+
1839+
public function test_json_encode_array(): void
1840+
{
1841+
$data = ['php', 'framework', 'tempest'];
1842+
$result = arr($data)->jsonEncode();
1843+
1844+
$this->assertInstanceOf(\Tempest\Support\Str\ImmutableString::class, $result);
1845+
$this->assertSame('["php","framework","tempest"]', $result->toString());
1846+
}
1847+
1848+
public function test_json_encode_empty_array(): void
1849+
{
1850+
$result = arr([])->jsonEncode();
1851+
1852+
$this->assertInstanceOf(\Tempest\Support\Str\ImmutableString::class, $result);
1853+
$this->assertSame('[]', $result->toString());
1854+
}
18091855
}

0 commit comments

Comments
 (0)