Skip to content

Commit

Permalink
Merge pull request #50 from shahmal1yev/bss-104
Browse files Browse the repository at this point in the history
[BSS-104] Make response component as serializable
  • Loading branch information
shahmal1yev authored Jan 25, 2025
2 parents 3d18782 + 053b382 commit ed7d774
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Contracts/Resources/ResponseContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
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;

/**
* @see GetProfile
* @see CreateRecord
* @see UploadBlob
*/
interface ResponseContract
interface ResponseContract extends Stringable, \JsonSerializable
{
/**
* @param string $name
Expand All @@ -27,4 +28,7 @@ public function get($offset);
* @return bool
*/
public function exist(string $name): bool;

public function __toString(): string;
public function jsonSerialize(): array;
}
10 changes: 10 additions & 0 deletions src/Responses/BaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
9 changes: 9 additions & 0 deletions tests/Unit/Responses/BaseResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ed7d774

Please sign in to comment.