diff --git a/app/Restify/Repository.php b/app/Restify/Repository.php index 9e0bd7420..e6cebdb3f 100644 --- a/app/Restify/Repository.php +++ b/app/Restify/Repository.php @@ -4,7 +4,9 @@ namespace App\Restify; use Binaryk\LaravelRestify\Filters\Filter; use Binaryk\LaravelRestify\Filters\MatchFilter; +use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; use Binaryk\LaravelRestify\Repositories\Repository as RestifyRepository; +use Illuminate\Support\Arr; abstract class Repository extends RestifyRepository { @@ -16,4 +18,42 @@ abstract class Repository extends RestifyRepository { return array_map(static fn (string $type): Filter => MatchFilter::make()->setType($type)->partial(), static::$match); } + + /** + * @psalm-suppress MissingParamType + */ + public function allowToStore(RestifyRequest $request, $payload = null): RestifyRepository + { + $this->adaptJsonApiRequest($request); + + return parent::allowToStore($request, $payload); + } + + /** + * @psalm-suppress MissingParamType + */ + public function allowToPatch(RestifyRequest $request, $payload = null): RestifyRepository + { + $this->adaptJsonApiRequest($request); + + return parent::allowToPatch($request, $payload); + } + + /** + * Adapt JSON:API request to Restify request. + */ + protected function adaptJsonApiRequest(RestifyRequest $request): void + { + /** @var array $attributes */ + $attributes = $request->input('attributes') ?? []; + $relationships = $request->input('relationships') ?? []; + + // Get relationships in form of "relationshipName" => "relationshipId" + $relationships = array_map(static fn (array $relationship): int => Arr::get($relationship, 'data.id'), $relationships); + + $request->replace([ + ...$attributes, + ...$relationships, + ]); + } }