Skip to content

Commit

Permalink
introduce forceReplace and forceOffsetUnset macros to Request
Browse files Browse the repository at this point in the history
  • Loading branch information
Marius committed Dec 3, 2024
1 parent b43a466 commit 2c6b073
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Http/Controllers/ResourceControllerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function update(string $identifier, Request $request): JsonResponse
$this->resourceService->getIgnoreExternalUpdateFor()
)));
/** $request can contain also files so, overwrite this function to handle them */
$request->replace($model->getDirty());
$request->forceReplace($model->getDirty());

return GeneralHelper::app(JsonResponse::class, [
'data' => $this->resourceService->update($identifier, $this->validateUpdateRequest($request))
Expand All @@ -161,7 +161,7 @@ public function update(string $identifier, Request $request): JsonResponse
throw $e;
}

$request->replace($all);
$request->forceReplace($all);

return $this->create($request);
}
Expand Down
37 changes: 37 additions & 0 deletions src/Providers/CrudProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;

class CrudProvider extends ServiceProvider
Expand Down Expand Up @@ -37,5 +38,41 @@ function (...$arguments): Model {
return $model::query()->useWritePdo()->where($model->getPrimaryKeyFilter())->firstOrFail();
}
);

Request::macro('forceOffsetUnset', function (string $offset): Request {
/** @var Request $this */

if ($this->json()->has($offset) > 0) {
$this->json()->remove($offset);
}

if ($this->query->has($offset)) {
$this->query->remove($offset);
}

if ($this->request->has($offset)) {
$this->request->remove($offset);
}

return $this;
});

Request::macro('forceReplace', function (array $data): Request {
/** @var Request $this */

if ($this->json()->count() > 0) {
$this->json()->replace($data);
}

if ($this->query->count() > 0) {
$this->query->replace($data);
}

if ($this->request->count() > 0) {
$this->request->replace($data);
}

return $this;
});
}
}

0 comments on commit 2c6b073

Please sign in to comment.