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/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', ] ] ];