generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added an api to traverse an openapi document
- Loading branch information
1 parent
2106fc6
commit 921cc6c
Showing
4 changed files
with
81 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble; | ||
|
||
class AbstractOpenAPIObjectVisitor implements OpenAPIObjectVisitor | ||
{ | ||
public function enter(mixed $object, array $path = []) | ||
{ | ||
} | ||
|
||
public function leave(mixed $object, array $path = []) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble; | ||
|
||
interface OpenAPIObjectVisitor | ||
{ | ||
/** | ||
* Called when entering an object for traversal. | ||
* | ||
* @param mixed $object OpenAPI object being entered for traversal. | ||
* @param (string|int)[] $path The path from the root to the object being entered. When an object is a part of an array, its index is used. | ||
*/ | ||
public function enter(mixed $object, array $path = []); | ||
|
||
/** | ||
* Called when completed traversing an object. | ||
* | ||
* @param mixed $object OpenAPI object being left after traversal. | ||
* @param (string|int)[] $path The path from the root to the object being left. When an object is a part of an array, its index is used. | ||
*/ | ||
public function leave(mixed $object, array $path = []); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
use Dedoc\Scramble\AbstractOpenAPIObjectVisitor; | ||
use Dedoc\Scramble\Support\Generator\InfoObject; | ||
use Dedoc\Scramble\Support\Generator\OpenApi; | ||
use Dedoc\Scramble\Support\Generator\Operation; | ||
use Dedoc\Scramble\Support\Generator\Path; | ||
|
||
it('traverses open api document', function () { | ||
$document = new OpenApi(version: '3.1.0'); | ||
$document->setInfo(new InfoObject(title: 'app')); | ||
$document->addPath($path = new Path('/test')); | ||
$path->addOperation(new Operation('GET')); | ||
|
||
$traverser = new \Dedoc\Scramble\OpenAPITraverser([ | ||
$visitor = new class extends AbstractOpenAPIObjectVisitor { | ||
public array $paths = []; | ||
/** @inheritdoc */ | ||
public function enter($object, $path = []) | ||
{ | ||
$this->paths[] = join('.', $path); | ||
} | ||
} | ||
]); | ||
|
||
$traverser->traverse($document); | ||
|
||
expect($visitor->paths)->toBe([ | ||
'#', | ||
'#.info', | ||
'#.components', | ||
'#.paths.0', | ||
'#.paths.0.operations.GET', | ||
]); | ||
}); |