From 372d46fdf2dbe85bfb672dd8f3324260c7f357b0 Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Mon, 11 May 2020 11:31:23 +0000 Subject: [PATCH] Apply fixes from StyleCI --- routes/api.php | 3 +-- src/BooleanFilter.php | 1 - src/Repositories/Repository.php | 4 +-- src/SelectFilter.php | 1 - .../Search/RepositorySearchService.php | 17 ++++++------ .../RepositoryFilterControllerTest.php | 26 +++++++++---------- tests/Fixtures/Post/PostRepository.php | 2 +- 7 files changed, 24 insertions(+), 30 deletions(-) diff --git a/routes/api.php b/routes/api.php index b44788a43..dec06ba36 100644 --- a/routes/api.php +++ b/routes/api.php @@ -13,7 +13,7 @@ Route::get('/search', '\\'.GlobalSearchController::class); // Filters -Route::get('/{repository}/filters', '\\' . RepositoryFilterController::class); +Route::get('/{repository}/filters', '\\'.RepositoryFilterController::class); // API CRUD Route::get('/{repository}', '\\'.RepositoryIndexController::class); @@ -22,4 +22,3 @@ Route::patch('/{repository}/{repositoryId}', '\\'.RepositoryUpdateController::class); Route::put('/{repository}/{repositoryId}', '\\'.RepositoryUpdateController::class); Route::delete('/{repository}/{repositoryId}', '\\'.RepositoryDestroyController::class); - diff --git a/src/BooleanFilter.php b/src/BooleanFilter.php index 1b4015bdf..df6e435b1 100644 --- a/src/BooleanFilter.php +++ b/src/BooleanFilter.php @@ -8,7 +8,6 @@ abstract class BooleanFilter extends Filter { public $type = 'boolean'; - public function resolve(RestifyRequest $request, $filter) { $keyValues = collect($this->options($request))->mapWithKeys(function ($key) use ($filter) { diff --git a/src/Repositories/Repository.php b/src/Repositories/Repository.php index e16108ef9..79bfc648a 100644 --- a/src/Repositories/Repository.php +++ b/src/Repositories/Repository.php @@ -207,8 +207,6 @@ public function filters(RestifyRequest $request) return []; } - - /** * @param RestifyRequest $request * @return FieldCollection @@ -695,7 +693,7 @@ public static function uriTo(Model $model) public function availableFilters(RestifyRequest $request) { - return collect($this->filter($this->filters($request)))->each(fn(Filter $filter) => $filter->authorizedToSee($request)) + return collect($this->filter($this->filters($request)))->each(fn (Filter $filter) => $filter->authorizedToSee($request)) ->values(); } } diff --git a/src/SelectFilter.php b/src/SelectFilter.php index 6c99102ac..4a43c7235 100644 --- a/src/SelectFilter.php +++ b/src/SelectFilter.php @@ -5,5 +5,4 @@ abstract class SelectFilter extends Filter { public $type = 'select'; - } diff --git a/src/Services/Search/RepositorySearchService.php b/src/Services/Search/RepositorySearchService.php index 6ed373bae..ee4542f90 100644 --- a/src/Services/Search/RepositorySearchService.php +++ b/src/Services/Search/RepositorySearchService.php @@ -27,7 +27,7 @@ public function prepareMatchFields(RestifyRequest $request, $query, $extra = []) { $model = $query->getModel(); foreach ($this->repository->getMatchByFields() as $key => $type) { - if (!$request->has($key) && !data_get($extra, "match.$key")) { + if (! $request->has($key) && ! data_get($extra, "match.$key")) { continue; } @@ -60,7 +60,7 @@ public function prepareMatchFields(RestifyRequest $request, $query, $extra = []) case RestifySearchable::MATCH_INTEGER: case 'number': case 'int': - $query->where($field, '=', (int)$match); + $query->where($field, '=', (int) $match); break; } } @@ -125,7 +125,7 @@ public function prepareSearchFields(RestifyRequest $request, $query, $extra = [] $likeOperator = $connectionType == 'pgsql' ? 'ilike' : 'like'; foreach ($this->repository->getSearchableFields() as $column) { - $query->orWhere($model->qualifyColumn($column), $likeOperator, '%' . $search . '%'); + $query->orWhere($model->qualifyColumn($column), $likeOperator, '%'.$search.'%'); } }); @@ -176,12 +176,12 @@ public function setOrder($query, $param) protected function applyIndexQuery(RestifyRequest $request, Repository $repository) { - return fn($query) => $repository::indexQuery($request, $query); + return fn ($query) => $repository::indexQuery($request, $query); } protected function applyFilters(RestifyRequest $request, Repository $repository, $query) { - if (!empty($request->filters)) { + if (! empty($request->filters)) { $filters = json_decode(base64_decode($request->filters), true); collect($filters) @@ -191,21 +191,20 @@ protected function applyFilters(RestifyRequest $request, Repository $repository, return $filter['class'] === $availableFilter->key(); }); - if (is_null($matchingFilter)) { return false; } - if ($matchingFilter->invalidPayloadValue($request, $filter['value'])) { + if ($matchingFilter->invalidPayloadValue($request, $filter['value'])) { return false; - }; + } $matchingFilter->resolve($request, $filter['value']); return $matchingFilter; }) ->filter() - ->each(fn(Filter $filter) => $filter->filter($request, $query, $filter->value)); + ->each(fn (Filter $filter) => $filter->filter($request, $query, $filter->value)); } return $query; diff --git a/tests/Controllers/RepositoryFilterControllerTest.php b/tests/Controllers/RepositoryFilterControllerTest.php index 1e5ff5901..e174c8796 100644 --- a/tests/Controllers/RepositoryFilterControllerTest.php +++ b/tests/Controllers/RepositoryFilterControllerTest.php @@ -24,8 +24,8 @@ public function test_can_get_available_filters() public function test_the_boolean_filter_is_applied() { - factory(Post::class)->create(['is_active' => false,]); - factory(Post::class)->create(['is_active' => true,]); + factory(Post::class)->create(['is_active' => false]); + factory(Post::class)->create(['is_active' => true]); $filters = base64_encode(json_encode([ [ @@ -33,12 +33,12 @@ public function test_the_boolean_filter_is_applied() 'value' => [ 'is_active' => false, ], - ] + ], ])); $response = $this ->withoutExceptionHandling() - ->getJson('restify-api/posts?filters=' . $filters) + ->getJson('restify-api/posts?filters='.$filters) ->dump() ->assertStatus(200); @@ -47,19 +47,19 @@ public function test_the_boolean_filter_is_applied() public function test_the_select_filter_is_applied() { - factory(Post::class)->create(['category' => 'movie',]); - factory(Post::class)->create(['category' => 'article',]); + factory(Post::class)->create(['category' => 'movie']); + factory(Post::class)->create(['category' => 'article']); $filters = base64_encode(json_encode([ [ 'class' => SelectCategoryFilter::class, - 'value' => 'article' - ] + 'value' => 'article', + ], ])); $response = $this ->withExceptionHandling() - ->getJson('restify-api/posts?filters=' . $filters) + ->getJson('restify-api/posts?filters='.$filters) ->assertStatus(200); $this->assertCount(1, $response->json('data')); @@ -67,8 +67,8 @@ public function test_the_select_filter_is_applied() public function test_the_timestamp_filter_is_applied() { - factory(Post::class)->create(['created_at' => now()->addYear(),]); - factory(Post::class)->create(['created_at' => now()->subYear(),]); + factory(Post::class)->create(['created_at' => now()->addYear()]); + factory(Post::class)->create(['created_at' => now()->subYear()]); $filters = base64_encode(json_encode([ [ @@ -78,12 +78,12 @@ public function test_the_timestamp_filter_is_applied() [ 'class' => CreatedAfterDateFilter::class, 'value' => now()->addWeek()->timestamp, - ] + ], ])); $response = $this ->withExceptionHandling() - ->getJson('restify-api/posts?filters=' . $filters) + ->getJson('restify-api/posts?filters='.$filters) ->dump() ->assertStatus(200); diff --git a/tests/Fixtures/Post/PostRepository.php b/tests/Fixtures/Post/PostRepository.php index 5dd52427a..8c607079d 100644 --- a/tests/Fixtures/Post/PostRepository.php +++ b/tests/Fixtures/Post/PostRepository.php @@ -53,7 +53,7 @@ public function fieldsForStore(RestifyRequest $request) public function filters(RestifyRequest $request) { return [ - ActiveBooleanFilter::new()->canSee(fn() => true), + ActiveBooleanFilter::new()->canSee(fn () => true), SelectCategoryFilter::new(), CreatedAfterDateFilter::new(), ];