Skip to content

Commit 2a2fd95

Browse files
committed
feat(str): implement jsonDecode
1 parent bc143f2 commit 2a2fd95

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

packages/support/src/Str/ManipulatesString.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
use Stringable;
1111
use Tempest\Intl;
1212
use Tempest\Support\Arr\ImmutableArray;
13+
use Tempest\Support\Arr\MutableArray;
1314
use Tempest\Support\Random;
1415
use Tempest\Support\Regex;
1516

1617
use function Tempest\Support\arr;
18+
use function Tempest\Support\Json\decode;
1719
use function Tempest\Support\tap;
1820

1921
/**
@@ -865,6 +867,16 @@ public function dump(mixed ...$dumps): self
865867
return $this;
866868
}
867869

870+
/**
871+
* Decodes the JSON string and returns an array helper instance.
872+
*/
873+
public function jsonDecode(bool $mutable = false): ImmutableArray|MutableArray
874+
{
875+
$decoded = decode($this->value);
876+
877+
return $mutable ? new MutableArray($decoded) : new ImmutableArray($decoded);
878+
}
879+
868880
/**
869881
* Converts to a scalar string.
870882
*/

packages/support/tests/Str/ManipulatesStringTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,4 +750,44 @@ public function test_uuid(): void
750750
{
751751
$this->assertTrue(str()->uuid()->isUuid());
752752
}
753+
754+
public function test_json_decode(): void
755+
{
756+
$json = '{"name": "tempest", "version": "1.0", "tags": ["php", "framework"]}';
757+
$result = str($json)->jsonDecode();
758+
759+
$this->assertInstanceOf(\Tempest\Support\Arr\ImmutableArray::class, $result);
760+
$this->assertSame('tempest', $result->get('name'));
761+
$this->assertSame('1.0', $result->get('version'));
762+
$this->assertInstanceOf(\Tempest\Support\Arr\ImmutableArray::class, $result->get('tags'));
763+
$this->assertSame(['php', 'framework'], $result->get('tags')->toArray());
764+
}
765+
766+
public function test_json_decode_mutable(): void
767+
{
768+
$json = '{"name": "tempest", "version": "1.0"}';
769+
$result = str($json)->jsonDecode(mutable: true);
770+
771+
$this->assertInstanceOf(\Tempest\Support\Arr\MutableArray::class, $result);
772+
$this->assertSame('tempest', $result->get('name'));
773+
$this->assertSame('1.0', $result->get('version'));
774+
}
775+
776+
public function test_json_decode_array(): void
777+
{
778+
$json = '["php", "framework", "tempest"]';
779+
$result = str($json)->jsonDecode();
780+
781+
$this->assertInstanceOf(\Tempest\Support\Arr\ImmutableArray::class, $result);
782+
$this->assertSame('php', $result->get(0));
783+
$this->assertSame('framework', $result->get(1));
784+
$this->assertSame('tempest', $result->get(2));
785+
}
786+
787+
public function test_json_decode_invalid_json(): void
788+
{
789+
$this->expectException(\Tempest\Support\Json\Exception\JsonCouldNotBeDecoded::class);
790+
791+
str('invalid json')->jsonDecode();
792+
}
753793
}

0 commit comments

Comments
 (0)