diff --git a/src/Http/Requests/InteractWithRepositories.php b/src/Http/Requests/InteractWithRepositories.php index 02af2e5d2..e88b9c0a8 100644 --- a/src/Http/Requests/InteractWithRepositories.php +++ b/src/Http/Requests/InteractWithRepositories.php @@ -44,7 +44,7 @@ public function repository($key = null): ?Repository ]), 404); } - if (!$repository::authorizedToUseRepository($this)) { + if (! $repository::authorizedToUseRepository($this)) { throw new UnauthorizedException(__('Unauthorized to view repository :name. See "allowRestify" policy.', [ 'name' => $repository, ]), 403); @@ -114,7 +114,7 @@ public function newRepositoryWith($model, $uriKey = null) */ public function newQueryWithoutScopes($uriKey = null) { - if (!$this->isViaRepository()) { + if (! $this->isViaRepository()) { return $this->model($uriKey)->newQueryWithoutScopes(); } diff --git a/src/Repositories/Repository.php b/src/Repositories/Repository.php index bd47c20a6..b4b4f1c82 100644 --- a/src/Repositories/Repository.php +++ b/src/Repositories/Repository.php @@ -13,7 +13,6 @@ use Binaryk\LaravelRestify\Traits\InteractWithSearch; use Binaryk\LaravelRestify\Traits\PerformsQueries; use Illuminate\Contracts\Pagination\LengthAwarePaginator; -use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Http\Request; use Illuminate\Http\Resources\ConditionallyLoadsAttributes; @@ -181,7 +180,7 @@ public static function newModel(): Model public static function query(RestifyRequest $request) { - if (!$request->isViaRepository()) { + if (! $request->isViaRepository()) { return static::newModel()->query(); } @@ -227,8 +226,8 @@ public function collectFields(RestifyRequest $request) if ($this instanceof Mergeable) { $fillable = collect($this->resource->getFillable()) - ->filter(fn($attribute) => $fields->contains('attribute', $attribute) === false) - ->map(fn($attribute) => Field::new($attribute)); + ->filter(fn ($attribute) => $fields->contains('attribute', $attribute) === false) + ->map(fn ($attribute) => Field::new($attribute)); $fields = $fields->merge($fillable); } @@ -239,14 +238,14 @@ public function collectFields(RestifyRequest $request) private function indexFields(RestifyRequest $request): Collection { return $this->collectFields($request) - ->filter(fn(Field $field) => !$field->isHiddenOnIndex($request, $this)) + ->filter(fn (Field $field) => ! $field->isHiddenOnIndex($request, $this)) ->values(); } private function showFields(RestifyRequest $request): Collection { return $this->collectFields($request) - ->filter(fn(Field $field) => !$field->isHiddenOnShow($request, $this)) + ->filter(fn (Field $field) => ! $field->isHiddenOnShow($request, $this)) ->values(); } @@ -346,10 +345,10 @@ public static function routes(Router $router, $attributes, $wrap = false) public function resolveShowAttributes(RestifyRequest $request) { $fields = $this->showFields($request) - ->filter(fn(Field $field) => $field->authorize($request)) - ->each(fn(Field $field) => $field->resolveForShow($this)) - ->map(fn(Field $field) => $field->serializeToValue($request)) - ->mapWithKeys(fn($value) => $value) + ->filter(fn (Field $field) => $field->authorize($request)) + ->each(fn (Field $field) => $field->resolveForShow($this)) + ->map(fn (Field $field) => $field->serializeToValue($request)) + ->mapWithKeys(fn ($value) => $value) ->all(); if ($this instanceof Mergeable) { @@ -367,7 +366,7 @@ public function resolveShowAttributes(RestifyRequest $request) return false; } - if (!$field->authorize($request)) { + if (! $field->authorize($request)) { return false; } @@ -388,10 +387,10 @@ public function resolveIndexAttributes($request) { // Resolve the show method, and attach the value to the array $fields = $this->indexFields($request) - ->filter(fn(Field $field) => $field->authorize($request)) - ->each(fn(Field $field) => $field->resolveForIndex($this)) - ->map(fn(Field $field) => $field->serializeToValue($request)) - ->mapWithKeys(fn($value) => $value) + ->filter(fn (Field $field) => $field->authorize($request)) + ->each(fn (Field $field) => $field->resolveForIndex($this)) + ->map(fn (Field $field) => $field->serializeToValue($request)) + ->mapWithKeys(fn ($value) => $value) ->all(); if ($this instanceof Mergeable) { @@ -409,7 +408,7 @@ public function resolveIndexAttributes($request) return false; } - if (!$field->authorize($request)) { + if (! $field->authorize($request)) { return false; } @@ -455,7 +454,7 @@ public function resolveRelationships($request): array /** * @var AbstractPaginator $paginator */ $paginator = $this->resource->{$relation}()->paginate($request->get('relatablePerPage') ?? (static::$defaultRelatablePerPage ?? RestifySearchable::DEFAULT_RELATABLE_PER_PAGE)); - $withs[$relation] = $paginator->getCollection()->map(fn(Model $item) => [ + $withs[$relation] = $paginator->getCollection()->map(fn (Model $item) => [ 'attributes' => $item->toArray(), ]); } @@ -507,7 +506,7 @@ public function index(RestifyRequest $request) return static::resolveWith($value); })->filter(function (self $repository) use ($request) { return $repository->authorizedToShow($request); - })->values()->map(fn(self $repository) => $repository->serializeForIndex($request)); + })->values()->map(fn (self $repository) => $repository->serializeForIndex($request)); return $this->response([ 'meta' => RepositoryCollection::meta($paginator->toArray()), @@ -535,8 +534,7 @@ public function store(RestifyRequest $request) $this->resource->save(); } - - $this->storeFields($request)->each(fn(Field $field) => $field->invokeAfter($request, $this->resource)); + $this->storeFields($request)->each(fn (Field $field) => $field->invokeAfter($request, $this->resource)); }); static::stored($this->resource, $request); @@ -634,7 +632,7 @@ public function response($content = '', $status = 200, array $headers = []): Res public function serializeForShow(RestifyRequest $request): array { return $this->filter([ - 'id' => $this->when($this->resource->id, fn() => $this->getShowId($request)), + 'id' => $this->when($this->resource->id, fn () => $this->getShowId($request)), 'type' => $this->when($type = $this->getType($request), $type), 'attributes' => $request->isShowRequest() ? $this->resolveShowAttributes($request) : $this->resolveIndexAttributes($request), 'relationships' => $this->when(value($related = $this->resolveRelationships($request)), $related), @@ -647,7 +645,7 @@ public function serializeForIndex(RestifyRequest $request): array return $this->filter([ 'id' => $this->when($id = $this->getShowId($request), $id), 'type' => $this->when($type = $this->getType($request), $type), - 'attributes' => $this->when((bool)$attrs = $this->resolveIndexAttributes($request), $attrs), + 'attributes' => $this->when((bool) $attrs = $this->resolveIndexAttributes($request), $attrs), 'relationships' => $this->when(value($related = $this->resolveRelationships($request)), $related), 'meta' => $this->when(value($meta = $this->resolveIndexMeta($request)), $meta), ]); @@ -690,6 +688,6 @@ protected static function fillFields(RestifyRequest $request, Model $model, Coll public static function uriTo(Model $model) { - return Restify::path() . '/' . static::uriKey() . '/' . $model->getKey(); + return Restify::path().'/'.static::uriKey().'/'.$model->getKey(); } } diff --git a/src/Services/Search/RepositorySearchService.php b/src/Services/Search/RepositorySearchService.php index e58a03517..cdb304d96 100644 --- a/src/Services/Search/RepositorySearchService.php +++ b/src/Services/Search/RepositorySearchService.php @@ -5,7 +5,6 @@ use Binaryk\LaravelRestify\Contracts\RestifySearchable; use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; use Binaryk\LaravelRestify\Repositories\Repository; -use Illuminate\Database\Eloquent\Builder; class RepositorySearchService extends Searchable { diff --git a/tests/Controllers/RelatedIndexControllerTest.php b/tests/Controllers/RelatedIndexControllerTest.php index a1431e7dc..41cffc2d0 100644 --- a/tests/Controllers/RelatedIndexControllerTest.php +++ b/tests/Controllers/RelatedIndexControllerTest.php @@ -75,9 +75,9 @@ public function test_can_update_post_belongs_to_a_user() factory(User::class)->create(); factory(User::class)->create(); - factory(Post::class)->create(['title' => 'Post title', 'user_id' => 1,]); + factory(Post::class)->create(['title' => 'Post title', 'user_id' => 1]); - factory(Post::class)->create(['title' => 'Post title', 'user_id' => 2,]); + factory(Post::class)->create(['title' => 'Post title', 'user_id' => 2]); $response = $this->putJson('restify-api/posts/1?viaRepository=users&viaRepositoryId=1&viaRelationship=posts', [ 'title' => 'Post updated title', @@ -95,9 +95,9 @@ public function test_can_destroy_post_belongs_to_a_user() factory(User::class)->create(); factory(User::class)->create(); - factory(Post::class)->create(['title' => 'Post title', 'user_id' => 1,]); + factory(Post::class)->create(['title' => 'Post title', 'user_id' => 1]); - factory(Post::class)->create(['title' => 'Post title', 'user_id' => 2,]); + factory(Post::class)->create(['title' => 'Post title', 'user_id' => 2]); $this->deleteJson('restify-api/posts/1?viaRepository=users&viaRepositoryId=1&viaRelationship=posts')->assertStatus(204); @@ -114,9 +114,9 @@ public function test_policy_check_before_destroy_post_belongs_to_a_user() factory(User::class)->create(); - factory(Post::class)->create(['title' => 'Post title', 'user_id' => 1,]); + factory(Post::class)->create(['title' => 'Post title', 'user_id' => 1]); - factory(Post::class)->create(['title' => 'Post title', 'user_id' => 2,]); + factory(Post::class)->create(['title' => 'Post title', 'user_id' => 2]); $this->deleteJson('restify-api/posts/1?viaRepository=users&viaRepositoryId=1&viaRelationship=posts')->assertStatus(403);