diff --git a/seed/php-sdk/unions/src/Union/Types/Shape.php b/seed/php-sdk/unions/src/Union/Types/Shape.php new file mode 100644 index 00000000000..d988fc3a13c --- /dev/null +++ b/seed/php-sdk/unions/src/Union/Types/Shape.php @@ -0,0 +1,93 @@ +type = $this->options['type'] ?? '_unknown'; + $this->circle = $this->options['circle'] ?? null; + $this->square = $this->options['square'] ?? null; + $this->_unknown = $this->options['_unknown'] ?? null; + } + + public static function circle( + Circle $circle + ): Shape { + return new Shape([ + 'type' => 'circle', + 'circle' => $circle + ]); + } + + public static function square( + Square $square + ): Shape { + return new Shape([ + 'type' => 'square', + 'square' => $square + ]); + } + + public static function _unknown( + mixed $_unknown + ): Shape { + return new Shape([ + 'unknown' => $_unknown + ]); + } + + public function asCircle(): Circle + { + if ($this->type == 'circle') { + return $this->circle; + } else { + throw new \Exception( + "Expected type to be 'circle'; got '$this->type.'" + ); + } + } + + public function asSquare(): Square + { + if ($this->type == 'square') { + return $this->square; + } else { + throw new \Exception( + "Expected type to be 'square'; got '$this->type.'" + ); + } + } +} diff --git a/seed/php-sdk/unions/src/Union/UnionClient.php b/seed/php-sdk/unions/src/Union/UnionClient.php index 70e8f419c9b..9a7d63d159a 100644 --- a/seed/php-sdk/unions/src/Union/UnionClient.php +++ b/seed/php-sdk/unions/src/Union/UnionClient.php @@ -10,6 +10,7 @@ use Seed\Core\Json\JsonDecoder; use JsonException; use Psr\Http\Client\ClientExceptionInterface; +use Shape; class UnionClient { @@ -36,7 +37,7 @@ public function __construct( * @throws SeedException * @throws SeedApiException */ - public function get(string $id, ?array $options = null): mixed + public function get(string $id, ?array $options = null): Shape { try { $response = $this->client->sendRequest( @@ -49,7 +50,7 @@ public function get(string $id, ?array $options = null): mixed $statusCode = $response->getStatusCode(); if ($statusCode >= 200 && $statusCode < 400) { $json = $response->getBody()->getContents(); - return JsonDecoder::decodeMixed($json); + return Shape::fromJson($json); } } catch (JsonException $e) { throw new SeedException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); @@ -72,7 +73,7 @@ public function get(string $id, ?array $options = null): mixed * @throws SeedException * @throws SeedApiException */ - public function update(mixed $request, ?array $options = null): bool + public function update(Shape $request, ?array $options = null): bool { try { $response = $this->client->sendRequest( diff --git a/seed/php-sdk/unions/tests/Seed/Core/Client/RawClientTest.php b/seed/php-sdk/unions/tests/Seed/Core/Client/RawClientTest.php index a1052cff3a5..8a99911eb73 100644 --- a/seed/php-sdk/unions/tests/Seed/Core/Client/RawClientTest.php +++ b/seed/php-sdk/unions/tests/Seed/Core/Client/RawClientTest.php @@ -12,12 +12,16 @@ use Seed\Core\Client\HttpMethod; use Seed\Core\Client\RawClient; use Seed\Core\Json\JsonApiRequest; +use Seed\Union\Types\Circle; +use Seed\Union\UnionClient; +use Shape; class RawClientTest extends TestCase { private string $baseUrl = 'https://api.example.com'; private MockHandler $mockHandler; private RawClient $rawClient; + private UnionClient $unionClient; protected function setUp(): void { @@ -25,6 +29,7 @@ protected function setUp(): void $handlerStack = HandlerStack::create($this->mockHandler); $client = new Client(['handler' => $handlerStack]); $this->rawClient = new RawClient(['client' => $client]); + $this->unionClient = new UnionClient($this->rawClient); } public function testHeaders(): void @@ -98,4 +103,27 @@ private function sendRequest(BaseApiRequest $request): void $this->fail('An exception was thrown: ' . $e->getMessage()); } } + + private function useShapeResponse(): void + { + try { + $shape = $this->unionClient->get("circle"); + $this->assertEquals($shape->type, "circle"); + $circle = $shape->circle; + $this->assertEquals($circle->radius, 1.0); + } catch (\Throwable $e) { + $this->fail('An exception was thrown: ' . $e->getMessage()); + } + } + + private function useShapeRequest(): void + { + try { + $shape = Shape::circle(new Circle(['radius' => 1.0])); + $success = $this->unionClient->update($shape); + $this->assertEquals($success, true); + } catch (\Throwable $e) { + $this->fail('An exception was thrown: ' . $e->getMessage()); + } + } }