Skip to content

Commit

Permalink
feat(core) : add jsonserializable
Browse files Browse the repository at this point in the history
  • Loading branch information
donaldinou committed Sep 23, 2024
1 parent fde9d24 commit 92b4bd5
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 1 deletion.
4 changes: 3 additions & 1 deletion SplType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
*
* @psalm-api
*/
abstract class SplType
abstract class SplType implements
\JsonSerializable,
\Stringable
{
use SplTypeTrait;

Expand Down
10 changes: 10 additions & 0 deletions SplTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ trait SplTypeTrait
// phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
protected $__default;

/**
* Specify data which should be serialized to JSON.
*
* @return mixed
*/
public function jsonSerialize()
{
return $this->__default;
}

/**
* Serialize object.
*
Expand Down
13 changes: 13 additions & 0 deletions Tests/phpunit/SplBoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,17 @@ public function testSerialization(): void
$this->assertEquals($instance, $unserialized);
$this->assertFalse($instance());
}

/**
* Unit test.
*
* @return void
*/
public function testJsonSerialization(): void
{
$instance = new DuckBool();
$json = \json_encode($instance);

$this->assertEquals("false", $json);
}
}
13 changes: 13 additions & 0 deletions Tests/phpunit/SplEnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,17 @@ public function testSerialization(): void
$this->assertEquals($instance, $unserialized);
$this->assertEquals(Month::SEPTEMBER, $instance());
}

/**
* Unit test.
*
* @return void
*/
public function testJsonSerialization(): void
{
$instance = new Month(Month::SEPTEMBER);
$json = \json_encode($instance);

$this->assertEquals("9", $json);
}
}
13 changes: 13 additions & 0 deletions Tests/phpunit/SplFloatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,17 @@ public function testSerialization(): void
$this->assertEquals($instance, $unserialized);
$this->assertEquals(22.9, $instance());
}

/**
* Unit test.
*
* @return void
*/
public function testJsonSerialization(): void
{
$instance = new DuckFloat(22.9);
$json = \json_encode($instance);

$this->assertEquals("22.9", $json);
}
}
13 changes: 13 additions & 0 deletions Tests/phpunit/SplIntTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,17 @@ public function testSerialization(): void
$this->assertEquals($instance, $unserialized);
$this->assertEquals(22, $instance());
}

/**
* Unit test.
*
* @return void
*/
public function testJsonSerialization(): void
{
$instance = new DuckInt(22);
$json = \json_encode($instance);

$this->assertEquals("22", $json);
}
}
13 changes: 13 additions & 0 deletions Tests/phpunit/SplStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,17 @@ public function testSerialization(): void
$this->assertEquals($instance, $unserialized);
$this->assertSame('hello world', $instance());
}

/**
* Unit test.
*
* @return void
*/
public function testJsonSerialization(): void
{
$instance = new DuckString("hello world");
$json = \json_encode($instance);

$this->assertEquals('"hello world"', $json);
}
}

0 comments on commit 92b4bd5

Please sign in to comment.