From 88f3529c12d9f561025d3330a5ba2bc201b5b1e7 Mon Sep 17 00:00:00 2001 From: Ferror Date: Sat, 3 Feb 2024 00:10:40 +0100 Subject: [PATCH] OperationRenderer --- src/Attribute/Channel.php | 8 ++++ src/Attribute/Operation.php | 12 ++++++ src/Schema/V3/OperationRenderer.php | 15 ++++++- tests/Examples/UserSignedUp.php | 2 +- .../Unit/Schema/V3/OperationRendererTest.php | 42 +++++++++++++++---- 5 files changed, 70 insertions(+), 9 deletions(-) diff --git a/src/Attribute/Channel.php b/src/Attribute/Channel.php index 54e5585..b84fe61 100644 --- a/src/Attribute/Channel.php +++ b/src/Attribute/Channel.php @@ -15,4 +15,12 @@ public function __construct( public ChannelType $type = ChannelType::SUBSCRIBE, ) { } + + public function toArray(): array + { + return [ + 'name' => $this->name, + 'type' => $this->type->value, + ]; + } } diff --git a/src/Attribute/Operation.php b/src/Attribute/Operation.php index ad76fc4..743c3a6 100644 --- a/src/Attribute/Operation.php +++ b/src/Attribute/Operation.php @@ -16,4 +16,16 @@ public function __construct( public array $channels = [], ) { } + + public function toArray(): array + { + return [ + 'name' => $this->name, + 'type' => $this->type->value, + 'channels' => array_map( + static fn (Channel $channel) => $channel->toArray(), + $this->channels + ), + ]; + } } diff --git a/src/Schema/V3/OperationRenderer.php b/src/Schema/V3/OperationRenderer.php index 47910fc..ac86384 100644 --- a/src/Schema/V3/OperationRenderer.php +++ b/src/Schema/V3/OperationRenderer.php @@ -8,6 +8,19 @@ { public function render(array $document): array { - return []; + $operations = []; + + foreach ($document['operations'] as $operation) { + foreach ($operation['channels'] as $channel) { + $operations[$operation['name']] = [ + 'action' => $operation['type'], + 'channel' => [ + '$ref' => '#/channels/' . $channel['name'], + ], + ]; + } + } + + return $operations; } } diff --git a/tests/Examples/UserSignedUp.php b/tests/Examples/UserSignedUp.php index db34c3f..da461aa 100644 --- a/tests/Examples/UserSignedUp.php +++ b/tests/Examples/UserSignedUp.php @@ -15,7 +15,7 @@ * This class represents a SIMPLE example of documenting by AttributeStrategy. */ #[Message(name: 'UserSignedUp')] -#[Operation(name: 'user_signed_up', channels: [new Channel('user_signed_up')])] +#[Operation(name: 'UserSignedUpOperation', channels: [new Channel('UserSignedUpChannel')])] final readonly class UserSignedUp { public function __construct( diff --git a/tests/Unit/Schema/V3/OperationRendererTest.php b/tests/Unit/Schema/V3/OperationRendererTest.php index bd87481..ddc835f 100644 --- a/tests/Unit/Schema/V3/OperationRendererTest.php +++ b/tests/Unit/Schema/V3/OperationRendererTest.php @@ -14,17 +14,30 @@ public function testItRendersSendAction(): void { $renderer = new OperationRenderer(); - $document = []; + $document = [ + 'name' => 'UserSignedUp', + 'properties' => [], + 'operations' => [ + [ + 'name' => 'UserSignedUpOperation', + 'type' => 'send', + 'channels' => [ + [ + 'name' => 'UserSignedUpChannel', + 'type' => 'subscribe', + ] + ], + ] + ], + ]; $actual = $renderer->render($document); $expected = [ 'UserSignedUpOperation' => [ 'action' => 'send', - 'messages' => [ - 'UserSignedUp' => [ - '$ref' => '#/components/messages/UserSignedUp', - ] + 'channel' => [ + '$ref' => '#/channels/UserSignedUpChannel', ] ] ]; @@ -36,7 +49,22 @@ public function testItRendersReceiveAction(): void { $renderer = new OperationRenderer(); - $document = []; + $document = [ + 'name' => 'UserSignedUp', + 'properties' => [], + 'operations' => [ + [ + 'name' => 'UserSignedUpOperation', + 'type' => 'receive', + 'channels' => [ + [ + 'name' => 'UserSignedUpChannel', + 'type' => 'subscribe', + ] + ], + ] + ], + ]; $actual = $renderer->render($document); @@ -44,7 +72,7 @@ public function testItRendersReceiveAction(): void 'UserSignedUpOperation' => [ 'action' => 'receive', 'channel' => [ - '$ref' => '#/channels/UserSignedUpChannel' + '$ref' => '#/channels/UserSignedUpChannel', ] ] ];