Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Dedoc Verison and Spatie Query Builder Version. #2

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
"minimum-stability": "dev",
"require": {
"php": "^8.1",

"dedoc/scramble": "^0.9.0 || ^0.10.0 || ^0.11.0",
"spatie/laravel-query-builder": "^5.0 || ^6.0"

},
"require-dev": {
"laravel/pint": "^v1.1.0",
Expand Down
13 changes: 8 additions & 5 deletions src/AllowedSortsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace Exonn\ScrambleSpatieQueryBuilder;

use Dedoc\Scramble\Extensions\OperationExtension;
use Dedoc\Scramble\Support\Generator\Combined\AnyOf;
use Dedoc\Scramble\Support\Generator\Operation;
use Dedoc\Scramble\Support\Generator\Parameter;
use Dedoc\Scramble\Support\Generator\Schema;
use Dedoc\Scramble\Support\Generator\Types\ArrayType;
use Dedoc\Scramble\Support\Generator\Types\ObjectType;
use Dedoc\Scramble\Support\Generator\Types\StringType;
use Dedoc\Scramble\Support\RouteInfo;

Expand Down Expand Up @@ -38,12 +38,15 @@ public function handle(Operation $operation, RouteInfo $routeInfo)
array_map(fn ($value) => '-'.$value, $values)
));

$objectType = new ObjectType;
foreach ($arrayType->items->enum as $value) {
$objectType->addProperty($value, new StringType);
}
$parameter = new Parameter(config($this->configKey), 'query');

$parameter->setSchema(Schema::fromType((new AnyOf)->setItems([
new StringType,
$arrayType,
])))->example($this->examples);
$parameter->setSchema(Schema::fromType(new StringType))
->description('Sort the results by the given fields. Available fields: '.implode(', ', array_map(fn ($value) => "`$value`", $arrayType->items->enum)).'. You can sort by multiple options by separating them with a comma. To sort in descending order, use - sign in front of the sort, for example: `-name`.')
->example($this->examples);

$halt = $this->runHooks($operation, $parameter);
if (! $halt) {
Expand Down
47 changes: 42 additions & 5 deletions src/InferHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

use Dedoc\Scramble\Infer\Services\FileParser;
use Dedoc\Scramble\Support\RouteInfo;
use Illuminate\Support\Facades\Log;
use PhpParser\Node;
use PhpParser\NodeFinder;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\AllowedSort;

class InferHelper
{
Expand All @@ -18,9 +21,43 @@ public function inferValues(Node\Expr\MethodCall $methodCall, RouteInfo $routeIn
return [];
}

// ->allowedIncludes(['posts', 'posts.author'])
if ($methodCall->args[0]->value instanceof Node\Expr\Array_) {
return array_map(fn (Node\Expr\ArrayItem $item) => $item->value->value, $methodCall->args[0]->value->items);
return array_map(function (Node\Expr\ArrayItem $item) {
if ($item->value instanceof Node\Scalar\String_) {
return $item->value->value;
}
if ($item->value instanceof Node\Expr\StaticCall) {

if ($item->value->class instanceof Node\Name
&& $item->value->class->toString() === 'Spatie\QueryBuilder\AllowedFilter'
&& $item->value->name instanceof Node\Identifier
&& $item->value->name->name === 'scope') {
return $item->value->args[0]->value->value;
}
// Check if the static call is AllowedSort::custom
if ($item->value->class instanceof Node\Name
&& $item->value->class->toString() === 'Spatie\QueryBuilder\AllowedSort'
&& $item->value->name instanceof Node\Identifier
&& $item->value->name->name === 'custom') {
return $item->value->args[0]->value->value;
}
return $this->inferValueFromStaticCall($item->value);
}

if ($item->value->name->name === 'default') {
if ($item->value->var instanceof Node\Expr\StaticCall
&& $item->value->var->class instanceof Node\Name
&& $item->value->var->class->toString() === 'Spatie\QueryBuilder\AllowedSort'
&& $item->value->var->name instanceof Node\Identifier
&& $item->value->var->name->name === 'custom') {
$customSortName = $item->value->var->args[0]->value->value;
return $customSortName;
}
return $item->value->var->args[0]->value->value;
}

return self::NOT_SUPPORTED_KEY;
}, $methodCall->args[0]->value->items);
}

// ->allowedIncludes('posts', 'posts.author')
Expand Down Expand Up @@ -75,7 +112,7 @@ function (Node\ArrayItem $item) {
if ($item->value instanceof Node\Scalar\String_) {
return $item->value->value;
}
// AllowedFilter::callback(...), AllowedSort::callback
//AllowedFilter::callback(...), AllowedSort::callback
if ($item->value instanceof Node\Expr\StaticCall) {
return $this->inferValueFromStaticCall($item->value);
}
Expand Down Expand Up @@ -125,9 +162,9 @@ public function inferValuesFromPropertyFetch(Node\Expr\PropertyFetch $node, Rout
public function inferValueFromStaticCall(Node\Expr\StaticCall $node)
{
switch ($node->class->name) {
case 'AllowedFilter':
case AllowedFilter::class:
return $this->inferValueFromAllowedFilter($node);
case 'AllowedSort':
case AllowedSort::class:
return $this->inferValueFromAllowedSort($node);
default:
return self::NOT_SUPPORTED_KEY;
Expand Down