diff --git a/src/Controllers/RestController.php b/src/Controllers/RestController.php index 39f2b530d..eae58dfc2 100644 --- a/src/Controllers/RestController.php +++ b/src/Controllers/RestController.php @@ -125,7 +125,6 @@ protected function response($data = null, $status = 200, array $headers = []) return $this->response; } - /** * @param $modelClass * @param array $filters @@ -144,7 +143,6 @@ public function search($modelClass, $filters = []) $paginator = $results->paginate($this->request()->get('perPage') ?? ($modelClass::$defaultPerPage ?? RestifySearchable::DEFAULT_PER_PAGE)); $items = $paginator->getCollection()->map->serializeForIndex($this->request()); - return array_merge($paginator->toArray(), [ 'data' => $items, ]); diff --git a/src/Http/Requests/InteractWithRepositories.php b/src/Http/Requests/InteractWithRepositories.php index d86a2fb10..164827970 100644 --- a/src/Http/Requests/InteractWithRepositories.php +++ b/src/Http/Requests/InteractWithRepositories.php @@ -9,7 +9,6 @@ use Illuminate\Database\Eloquent\Model; /** - * @package Binaryk\LaravelRestify\Http\Requests; * @author Eduard Lupacescu */ trait InteractWithRepositories @@ -45,7 +44,7 @@ public function repository() ]), 404); } - if ( ! $repository::authorizedToViewAny($this)) { + if (! $repository::authorizedToViewAny($this)) { throw new UnauthorizedException(__('Unauthorized to view repository :name.', [ 'name' => $repository, ]), 403); diff --git a/src/Http/Requests/RestifyRequest.php b/src/Http/Requests/RestifyRequest.php index d57f75a34..711031f43 100644 --- a/src/Http/Requests/RestifyRequest.php +++ b/src/Http/Requests/RestifyRequest.php @@ -10,5 +10,4 @@ class RestifyRequest extends FormRequest { use InteractWithRepositories; - } diff --git a/src/Repositories/Repository.php b/src/Repositories/Repository.php index 29ff1b3ba..9e16c2bc8 100644 --- a/src/Repositories/Repository.php +++ b/src/Repositories/Repository.php @@ -5,7 +5,6 @@ use Binaryk\LaravelRestify\Traits\AuthorizableModels; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; -use Illuminate\Http\Request; use Illuminate\Support\Str; /** diff --git a/src/Services/Search/SearchService.php b/src/Services/Search/SearchService.php index ef9d2e981..f8d670484 100644 --- a/src/Services/Search/SearchService.php +++ b/src/Services/Search/SearchService.php @@ -32,7 +32,7 @@ public function search(RestifyRequest $request, Model $model) } /** - * Will prepare the eloquent array to return + * Will prepare the eloquent array to return. * * @return array */ @@ -54,7 +54,7 @@ protected function prepare() } /** - * Prepare eloquent exact fields + * Prepare eloquent exact fields. * * @param $fields * @@ -100,7 +100,7 @@ protected function prepareIn($fields) } /** - * Prepare eloquent exact fields + * Prepare eloquent exact fields. * * @param $fields * @@ -117,16 +117,16 @@ protected function prepareOperator($fields) foreach ($values as $field => $value) { $qualifiedField = $this->model->qualifyColumn($field); switch ($key) { - case "gte": + case 'gte': $this->builder->where($qualifiedField, '>=', $value); break; - case "gt": + case 'gt': $this->builder->where($qualifiedField, '>', $value); break; - case "lte": + case 'lte': $this->builder->where($qualifiedField, '<=', $value); break; - case "lt": + case 'lt': $this->builder->where($qualifiedField, '<', $value); break; } @@ -138,7 +138,7 @@ protected function prepareOperator($fields) } /** - * Prepare eloquent exact fields + * Prepare eloquent exact fields. * * @param $fields * @@ -146,7 +146,7 @@ protected function prepareOperator($fields) */ protected function prepareMatchFields() { - foreach($this->model::getMatchByFields() as $key => $type) { + foreach ($this->model::getMatchByFields() as $key => $type) { if (! $this->request->has($key) && ! data_get($this->fixedInput, "match.$key")) { continue; } @@ -186,7 +186,7 @@ protected function prepareMatchFields() } /** - * Prepare eloquent order by + * Prepare eloquent order by. * * @param $sort * @@ -214,7 +214,7 @@ protected function prepareOrders($sort) } /** - * Prepare relations + * Prepare relations. * * @return $this */ @@ -243,7 +243,7 @@ protected function prepareRelations() } /** - * Prepare search + * Prepare search. * * @param $search * @return $this @@ -252,7 +252,7 @@ protected function prepareSearchFields($search) { $this->builder->where(function (Builder $query) use ($search) { /** - * @var RestifySearchable|Model $model + * @var RestifySearchable|Model */ $model = $query->getModel(); @@ -263,7 +263,6 @@ protected function prepareSearchFields($search) ($connectionType != 'pgsql' || $search <= PHP_INT_MAX) && in_array($query->getModel()->getKeyName(), $model::getSearchableFields()); - if ($canSearchPrimaryKey) { $query->orWhere($query->getModel()->getQualifiedKeyName(), $search); } @@ -271,7 +270,7 @@ protected function prepareSearchFields($search) $likeOperator = $connectionType == 'pgsql' ? 'ilike' : 'like'; foreach ($this->model::getSearchableFields() as $column) { - $query->orWhere($model->qualifyColumn($column), $likeOperator, '%' . $search . '%'); + $query->orWhere($model->qualifyColumn($column), $likeOperator, '%'.$search.'%'); } }); @@ -279,7 +278,7 @@ protected function prepareSearchFields($search) } /** - * Set order + * Set order. * * @param $param * @@ -289,6 +288,7 @@ public function setOrder($param) { if ($param === 'random') { $this->builder->inRandomOrder(); + return $this; } diff --git a/src/Services/Search/Searchable.php b/src/Services/Search/Searchable.php index 87e880b62..35ca074c1 100644 --- a/src/Services/Search/Searchable.php +++ b/src/Services/Search/Searchable.php @@ -6,9 +6,6 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Http\Request; -/** - * @package Binaryk\LaravelRestify\Services\Search; - */ abstract class Searchable { /** @@ -17,7 +14,7 @@ abstract class Searchable protected $request; /** - * @var RestifySearchable|Model $model + * @var RestifySearchable|Model */ protected $model; @@ -26,7 +23,6 @@ abstract class Searchable */ protected $fixedInput; - /** * @param $input * diff --git a/src/Traits/AuthorizableModels.php b/src/Traits/AuthorizableModels.php index 319ee36b9..9580f2381 100644 --- a/src/Traits/AuthorizableModels.php +++ b/src/Traits/AuthorizableModels.php @@ -9,8 +9,8 @@ use Illuminate\Support\Str; /** - * Could be used as a trait in a model class and in a repository class - * + * Could be used as a trait in a model class and in a repository class. + * * @author Eduard Lupacescu */ trait AuthorizableModels diff --git a/tests/Controllers/RepositoryIndexControllerTest.php b/tests/Controllers/RepositoryIndexControllerTest.php index 68ac6b001..c60496309 100644 --- a/tests/Controllers/RepositoryIndexControllerTest.php +++ b/tests/Controllers/RepositoryIndexControllerTest.php @@ -13,7 +13,7 @@ class RepositoryIndexControllerTest extends IntegrationTest { public function test_list_resource() - { + { factory(User::class)->create(); factory(User::class)->create(); factory(User::class)->create(); @@ -24,7 +24,6 @@ public function test_list_resource() $response->assertJsonCount(3, 'data.data'); } - public function test_the_rest_controller_can_paginate() { $this->mockUsers(50); @@ -80,12 +79,12 @@ public function test_search_query_works() 'data' => [ 'data' => [$expected], 'current_page' => 1, - 'first_page_url' => "http://localhost/restify-api/users?page=1", + 'first_page_url' => 'http://localhost/restify-api/users?page=1', 'from' => 1, 'last_page' => 1, - 'last_page_url' => "http://localhost/restify-api/users?page=1", + 'last_page_url' => 'http://localhost/restify-api/users?page=1', 'next_page_url' => null, - 'path' => "http://localhost/restify-api/users", + 'path' => 'http://localhost/restify-api/users', 'per_page' => 15, 'prev_page_url' => null, 'to' => 1, @@ -101,12 +100,12 @@ public function test_search_query_works() 'data' => [ 'data' => [], 'current_page' => 1, - 'first_page_url' => "http://localhost/restify-api/users?page=1", + 'first_page_url' => 'http://localhost/restify-api/users?page=1', 'from' => 1, 'last_page' => 1, - 'last_page_url' => "http://localhost/restify-api/users?page=1", + 'last_page_url' => 'http://localhost/restify-api/users?page=1', 'next_page_url' => null, - 'path' => "http://localhost/restify-api/users", + 'path' => 'http://localhost/restify-api/users', 'per_page' => 15, 'prev_page_url' => null, 'to' => 1, @@ -125,8 +124,6 @@ public function test_that_desc_sort_query_param_works() $this->assertSame($response->data['data'][0]['id'], 10); $this->assertSame($response->data['data'][9]['id'], 1); - - } public function test_that_asc_sort_query_param_works() @@ -173,12 +170,12 @@ public function test_that_match_param_works() 'data' => [ 'data' => [$expected], 'current_page' => 1, - 'first_page_url' => "http://localhost/restify-api/users?page=1", + 'first_page_url' => 'http://localhost/restify-api/users?page=1', 'from' => 1, 'last_page' => 1, - 'last_page_url' => "http://localhost/restify-api/users?page=1", + 'last_page_url' => 'http://localhost/restify-api/users?page=1', 'next_page_url' => null, - 'path' => "http://localhost/restify-api/users", + 'path' => 'http://localhost/restify-api/users', 'per_page' => 15, 'prev_page_url' => null, 'to' => 1, diff --git a/tests/Factories/PostFactory.php b/tests/Factories/PostFactory.php index 6894c5c32..95ceef360 100644 --- a/tests/Factories/PostFactory.php +++ b/tests/Factories/PostFactory.php @@ -1,7 +1,6 @@ */ class Post extends Model @@ -14,6 +13,6 @@ class Post extends Model 'id', 'user_id', 'title', - 'description' + 'description', ]; } diff --git a/tests/Fixtures/User.php b/tests/Fixtures/User.php index fe2ee0139..b71e68a8a 100644 --- a/tests/Fixtures/User.php +++ b/tests/Fixtures/User.php @@ -21,9 +21,9 @@ class User extends Authenticatable implements Passportable, MustVerifyEmail, Res InteractWithSearch; public static $search = ['id', 'email']; - public static $sort = ['id',]; - public static $match = ['id' => 'int', 'email' => 'string',]; - public static $in = ['id' => 'int',]; + public static $sort = ['id']; + public static $match = ['id' => 'int', 'email' => 'string']; + public static $in = ['id' => 'int']; public static $withs = ['posts']; /** diff --git a/tests/InteractWithModels.php b/tests/InteractWithModels.php index 80292d118..7aa7e5be0 100644 --- a/tests/InteractWithModels.php +++ b/tests/InteractWithModels.php @@ -24,7 +24,7 @@ public function mockUsers($count = 1, $predefinedEmails = []) $i++; } - foreach($predefinedEmails as $email) { + foreach ($predefinedEmails as $email) { $users->push(factory(User::class)->create([ 'email' => $email, ])); @@ -44,7 +44,7 @@ public function mockPosts($userId, $count = 1) $i = 0; while ($i < $count) { $users->push(factory(Post::class)->create( - ['user_id' => $userId,] + ['user_id' => $userId] )); $i++; }