Skip to content

Commit

Permalink
feature/add-callbacks-to-components (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
vyuldashev authored and matthew-inamdar committed Nov 18, 2019
1 parent 0438a3c commit fc83b8c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/Objects/Components.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class Components extends BaseObject
*/
protected $links;

/**
* @var \GoldSpecDigital\ObjectOrientedOAS\Objects\PathItem[]|null
*/
protected $callbacks;

/**
* @param \GoldSpecDigital\ObjectOrientedOAS\Contracts\SchemaContract[] $schemas
* @return \GoldSpecDigital\ObjectOrientedOAS\Objects\Components
Expand Down Expand Up @@ -163,6 +168,19 @@ public function links(Link ...$links): self
return $instance;
}

/**
* @param \GoldSpecDigital\ObjectOrientedOAS\Objects\PathItem[] $callbacks
* @return \GoldSpecDigital\ObjectOrientedOAS\Objects\Components
*/
public function callbacks(PathItem ...$callbacks): self
{
$instance = clone $this;

$instance->callbacks = $callbacks ?: null;

return $instance;
}

/**
* @return array
*/
Expand Down Expand Up @@ -208,6 +226,11 @@ protected function generate(): array
$links[$link->objectId] = $link;
}

$callbacks = [];
foreach ($this->callbacks ?? [] as $callback) {
$callbacks[$callback->objectId] = $callback;
}

return Arr::filter([
'schemas' => $schemas ?: null,
'responses' => $responses ?: null,
Expand All @@ -217,6 +240,7 @@ protected function generate(): array
'headers' => $headers ?: null,
'securitySchemes' => $securitySchemes ?: null,
'links' => $links ?: null,
'callbacks' => $callbacks ?: null,
]);
}
}
23 changes: 22 additions & 1 deletion tests/Objects/ComponentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use GoldSpecDigital\ObjectOrientedOAS\Objects\Header;
use GoldSpecDigital\ObjectOrientedOAS\Objects\Link;
use GoldSpecDigital\ObjectOrientedOAS\Objects\OAuthFlow;
use GoldSpecDigital\ObjectOrientedOAS\Objects\Operation;
use GoldSpecDigital\ObjectOrientedOAS\Objects\Parameter;
use GoldSpecDigital\ObjectOrientedOAS\Objects\PathItem;
use GoldSpecDigital\ObjectOrientedOAS\Objects\RequestBody;
use GoldSpecDigital\ObjectOrientedOAS\Objects\Response;
use GoldSpecDigital\ObjectOrientedOAS\Objects\Schema;
Expand Down Expand Up @@ -45,6 +47,15 @@ public function create_with_all_parameters_works()

$link = Link::create('LinkExample');

$callback = PathItem::create('MyEvent')
->route('{$request.query.callbackUrl}')
->operations(
Operation::post()->requestBody(
RequestBody::create()
->description('something happened')
)
);

$components = Components::create()
->schemas($schema)
->responses($response)
Expand All @@ -53,7 +64,8 @@ public function create_with_all_parameters_works()
->requestBodies($requestBody)
->headers($header)
->securitySchemes($securityScheme)
->links($link);
->links($link)
->callbacks($callback);

$this->assertEquals([
'schemas' => [
Expand Down Expand Up @@ -96,6 +108,15 @@ public function create_with_all_parameters_works()
'links' => [
'LinkExample' => [],
],
'callbacks' => [
'MyEvent' => [
'post' => [
'requestBody' => [
'description' => 'something happened',
],
],
],
],
], $components->toArray());
}
}

0 comments on commit fc83b8c

Please sign in to comment.