diff --git a/src/Contracts/Resources/ResponseContract.php b/src/Contracts/Resources/ResponseContract.php index bd3e81a..aadba07 100644 --- a/src/Contracts/Resources/ResponseContract.php +++ b/src/Contracts/Resources/ResponseContract.php @@ -5,6 +5,7 @@ use Atproto\API\App\Bsky\Actor\GetProfile; use Atproto\API\Com\Atrproto\Repo\CreateRecord; use Atproto\API\Com\Atrproto\Repo\UploadBlob; +use Atproto\Contracts\Stringable; use Atproto\Exceptions\Resource\BadAssetCallException; /** @@ -12,7 +13,7 @@ * @see CreateRecord * @see UploadBlob */ -interface ResponseContract +interface ResponseContract extends Stringable, \JsonSerializable { /** * @param string $name @@ -27,4 +28,7 @@ public function get($offset); * @return bool */ public function exist(string $name): bool; + + public function __toString(): string; + public function jsonSerialize(): array; } diff --git a/src/Responses/BaseResponse.php b/src/Responses/BaseResponse.php index da5afdd..791881c 100644 --- a/src/Responses/BaseResponse.php +++ b/src/Responses/BaseResponse.php @@ -106,4 +106,14 @@ private function parse(string $name) return $value; } + + public function __toString(): string + { + return json_encode($this->jsonSerialize()); + } + + public function jsonSerialize(): array + { + return $this->content; + } } diff --git a/tests/Unit/Responses/BaseResponseTest.php b/tests/Unit/Responses/BaseResponseTest.php index f6bb926..a8128f5 100644 --- a/tests/Unit/Responses/BaseResponseTest.php +++ b/tests/Unit/Responses/BaseResponseTest.php @@ -50,6 +50,15 @@ public function testMagicCall() $this->assertInstanceOf(ExampleObject::class, $result); } + public function testResponseCanBeSerialized(): void + { + $expected = json_encode([ + 'example' => 'some value', + ]); + + $this->assertJsonStringEqualsJsonString($expected, (string) $this->resource); + $this->assertJsonStringEqualsJsonString($expected, json_encode($this->resource)); + } } class TestableResponse implements ResponseContract