Skip to content

Commit

Permalink
Thor Extractor Refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
cesurapp committed Mar 20, 2024
1 parent b84658a commit b0e200a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/AbstractClass/ApiController.php
Expand Up @@ -89,7 +89,7 @@ protected function isGranted(mixed $attribute, mixed $subject = null): bool
protected function isGrantedDeny(mixed $attribute, mixed $subject = null): void
{
if (!$this->isGranted($attribute, $subject)) {
throw new AccessDeniedHttpException();
throw new AccessDeniedHttpException('Access Denied.');
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Thor/Attribute/ThorResource.php
Expand Up @@ -8,7 +8,8 @@
#[\Attribute(\Attribute::TARGET_FUNCTION | \Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)]
final class ThorResource
{
public function __construct(protected array $data = [])
// @phpstan-ignore-next-line
public function __construct(protected array $data = [], callable|string $callback = null)
{
}
}
17 changes: 14 additions & 3 deletions src/Thor/Extractor/ExtractDto.php
Expand Up @@ -221,9 +221,20 @@ private function extractDTOClass(\ReflectionClass $class): array

// Api Resource
$apiResource = $property->getAttributes(ThorResource::class);
if (str_contains($types, 'array') && count($apiResource)) {
$r = $apiResource[0]->getArguments()['data'];
$parameters[$property->getName()] = $r;
if (count($apiResource)) {
$r = $apiResource[0]->getArguments();

if (isset($r['callback'])) {
$data = call_user_func($r['callback']);
$parameters[$property->getName()] = implode('|', array_map(static fn ($v) => '?'.$v, $data));
} else {
if (!array_is_list($r['data'])) {
$parameters[$property->getName()] = $r['data'];
} else {
$parameters[$property->getName()] = implode('|', array_map(static fn ($v) => '?'.$v, $r['data']));
}
}

continue;
}

Expand Down

0 comments on commit b0e200a

Please sign in to comment.