generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
withResponse
support for JSON API resources (#514)
* `withResponse` method support * undo change * Fix styling --------- Co-authored-by: romalytvynenko <[email protected]>
- Loading branch information
1 parent
1d2cabf
commit 224be1b
Showing
6 changed files
with
179 additions
and
40 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,99 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble\Infer\Analyzer; | ||
|
||
use Dedoc\Scramble\Infer; | ||
use Dedoc\Scramble\Infer\Scope\Scope; | ||
use Dedoc\Scramble\Support\IndexBuilders\Bag; | ||
use Dedoc\Scramble\Support\IndexBuilders\IndexBuilder; | ||
use Dedoc\Scramble\Support\Type\Type; | ||
use PhpParser\Node; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class MethodQuery | ||
{ | ||
private array $argumentsOverrides = []; | ||
|
||
private array $types = []; | ||
|
||
private ?Scope $scope = null; | ||
|
||
public function __construct(private Infer $infer) {} | ||
|
||
public static function make(Infer $infer): static | ||
{ | ||
return new static($infer); | ||
} | ||
|
||
public function withArgumentType(array $positionName, Type $type): static | ||
{ | ||
$this->argumentsOverrides[] = [$positionName, $type]; | ||
|
||
return $this; | ||
} | ||
|
||
public function from(Infer\Definition\ClassDefinition $classDefinition, string $methodName): static | ||
{ | ||
$bag = new Bag; | ||
|
||
if (! $methodDefinition = $classDefinition->getMethodDefinition($methodName)) { | ||
return $this; | ||
} | ||
|
||
(new MethodAnalyzer($this->infer->index, $classDefinition)) | ||
->analyze($methodDefinition, [ | ||
new class($bag, $this->argumentsOverrides) implements IndexBuilder | ||
{ | ||
public function __construct(private Bag $bag, private array $argumentsOverrides = []) {} | ||
|
||
public function afterAnalyzedNode(Scope $scope, Node $node): void | ||
{ | ||
$this->replaceArguments($scope, $node); | ||
|
||
$types = $this->bag->data['types'] ?? []; | ||
$this->bag->set('scope', $scope); | ||
|
||
if ($type = $scope->getType($node)) { | ||
$types[] = $type; | ||
} | ||
|
||
$this->bag->set('types', $types); | ||
} | ||
|
||
private function replaceArguments(Scope $scope, Node $node) | ||
{ | ||
if ($this->bag->data['_hasReplaced'] ?? false) { | ||
return; | ||
} | ||
|
||
foreach ($this->argumentsOverrides as $argumentsOverride) { | ||
[[$name, $position], $type] = $argumentsOverride; | ||
|
||
$targetName = $name ?: array_keys($scope->variables)[$position]; | ||
|
||
$scope->variables[$targetName][0]['type'] = $type; | ||
} | ||
|
||
$this->bag->set('_hasReplaced', true); | ||
} | ||
}, | ||
]); | ||
|
||
$this->scope = $bag->data['scope'] ?? null; | ||
$this->types = $bag->data['types'] ?? []; | ||
|
||
return $this; | ||
} | ||
|
||
public function getTypes(callable $typesFinder) | ||
{ | ||
return collect($this->types)->filter($typesFinder)->values(); | ||
} | ||
|
||
public function getScope(): ?Scope | ||
{ | ||
return $this->scope; | ||
} | ||
} |
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
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
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