Skip to content

Commit

Permalink
implement filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
narcoticfresh committed May 23, 2024
1 parent c80b9c5 commit c831188
Showing 1 changed file with 54 additions and 9 deletions.
63 changes: 54 additions & 9 deletions src/Graviton/RestBundle/Trait/SchemaTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
namespace Graviton\RestBundle\Trait;

use Graviton\CommonBundle\CommonUtils;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Yaml\Yaml;
Expand All @@ -21,29 +22,73 @@ trait SchemaTrait
/**
* gets the response
*
* @param string $filepath filepath
* @param string $format format
* @param string $filepath filepath
* @param string $format format
* @param ?string $excludePaths paths to exclude
* @param ?string $includePaths paths to include
*
* @return Response response
*/
public function getResponseFromSchemaFile(string $filepath, string $format) : Response
{
public function getResponseFromSchemaFile(
string $filepath,
string $format,
?string $excludePaths,
?string $includePaths
) : Response {
return $this->getResponseFromSchema(
\json_decode(file_get_contents($filepath), true),
$format
$format,
$excludePaths,
$includePaths
);
}

/**
* return the response from a schema
*
* @param array $schema schema
* @param string $format format
* @param array $schema schema
* @param string $format format
* @param ?string $excludePaths paths to exclude
* @param ?string $includePaths paths to include
*
* @return Response response
*/
public function getResponseFromSchema(array $schema, string $format) : Response
{
public function getResponseFromSchema(
array $schema,
string $format,
?string $excludePaths,
?string $includePaths
) : Response {
// excludes?
if (!is_null($excludePaths)) {
$definedSet = array_keys($schema['paths']);
foreach ($definedSet as $key => $path) {
if (CommonUtils::subjectMatchesStringWildcards($excludePaths, $path)) {
unset($definedSet[$key]);
}
}

// now, do we have some includes? we only do this when there are excludes first!
if (!is_null($includePaths)) {
foreach ($schema['paths'] as $key => $path) {
if (CommonUtils::subjectMatchesStringWildcards($includePaths, $key)) {
// add id
$definedSet[] = $key;
}
}
}

// cleanup
natsort($definedSet);

$newPaths = [];
foreach ($definedSet as $setMember) {
$newPaths[$setMember] = $schema['paths'][$setMember];
}

$schema['paths'] = $newPaths;
}

if ($format == 'json') {
return new JsonResponse($schema, 200, []);
}
Expand Down

0 comments on commit c831188

Please sign in to comment.