From 0500d18a0352160a9e23886b430a0e4d2342bd07 Mon Sep 17 00:00:00 2001 From: Eduard Lupacescu Date: Thu, 18 Nov 2021 16:13:41 +0200 Subject: [PATCH 1/3] fix: Sorting by has one --- docs-v2/content/en/search/sorting.md | 80 ++++++++++++++++--- docs/docs/4.0/filtering/filtering.md | 2 +- docs/docs/5.0/filtering/filtering.md | 2 +- src/Eager/RelatedCollection.php | 5 +- src/Fields/HasOne.php | 6 +- src/Filters/SortCollection.php | 1 - src/Filters/SortableFilter.php | 53 ++++++++---- tests/Feature/Filters/BelongsToFilterTest.php | 2 +- tests/Fields/HasOneFieldTest.php | 44 ++++++++++ tests/Fixtures/User/User.php | 3 +- 10 files changed, 163 insertions(+), 35 deletions(-) diff --git a/docs-v2/content/en/search/sorting.md b/docs-v2/content/en/search/sorting.md index 414954013..052c07e9d 100644 --- a/docs-v2/content/en/search/sorting.md +++ b/docs-v2/content/en/search/sorting.md @@ -6,6 +6,8 @@ category: Search & Filters position: 13 --- + ## Definition + During index requests, usually we have to sort by specific attributes. This requires the `$sort` configuration: ```php @@ -16,30 +18,88 @@ class PostRepository extends Repository Performing request requires the sort query param: -### Descending sorting +## Descending sorting Sorting DESC requires a minus (`-`) sign before the attribute name: - ```http request + ```http_request GET: /api/restify/posts?sort=-id ``` Sorting ASC: - ```http request + ```http_request GET: /api/restify/posts?sort=id ``` or with plus sign before the field: - ```http request + ```http_request GET: /api/restify/posts?sort=+id ``` -### Sort using BelongsTo +## Sort using relation + +Sometimes you may need to sort by a `belongsTo` or `hasOne` relationship. + +This become a breeze with Restify. Firstly you have to instruct your sort to use a relationship: + +### HasOne sorting + +Using a `related` relationship, it becomes very easy to define a sortable by has one related. + +You simply add the `->sortable()` method to the relationship: + +```php +// UserRepository.php + +public function related(): array +{ + return [ + 'post' => HasOne::make('post', PostRepository::class)->sortable('title'), + ]; +} +``` + + + +The `sortable` method accepts the column (or fully qualified column name) of the related model. + + + + +The API request will always have to use the full path to the `attributes`: + +```bash +GET: /api/restify/posts?sort=post.attributes.title +``` + +The structure of the `sort` query param value consist always from 3 parts: + +- `post` - the name of the relation defined in the `related` method +- `attributes` - a generic json:api term +- `title` - the column name from the database of the related model + +### BelongsTo sorting + +The belongsTo sorting works in a similar way. + +You simply add the `->sortable()` method to the relationship: + +```php +// PostRepository.php + +public function related(): array +{ + return [ + 'user' => BelongsTo::make('user', UserRepository::class)->sortable('name'), + ]; +} +``` + +### Using custom sortable filter -Sometimes you may need to sort by a `belongsTo` relationship. This become a breeze with Restify. Firstly you have to -instruct your sort to use a relationship: +You can override the `sorts` method, and return an instance of `SortableFilter` that might be instructed to use a relationship: ```php // PostRepository @@ -51,7 +111,7 @@ public static function sorts(): array return [ 'users.name' => SortableFilter::make() ->setColumn('users.name') - ->usingBelongsTo( + ->usingRelation( BelongsTo::make('user', 'user', UserRepository::class), ) ]; @@ -74,7 +134,7 @@ As you may notice we have typed twice the `users.name` (on the array key, and as -### Sort using closure +## Sort using closure If you have a quick sort method, you can use a closure to sort your data: @@ -92,7 +152,7 @@ public static function sorts(): array } ``` -### Get available sorts +## Get available sorts You can use the following request to get sortable attributes for a repository: diff --git a/docs/docs/4.0/filtering/filtering.md b/docs/docs/4.0/filtering/filtering.md index f34e2952b..e5951cde9 100644 --- a/docs/docs/4.0/filtering/filtering.md +++ b/docs/docs/4.0/filtering/filtering.md @@ -251,7 +251,7 @@ public static function sorts(): array return [ 'users.name' => SortableFilter::make() ->setColumn('users.name') - ->usingBelongsTo( + ->usingRelation( BelongsTo::make('user', 'user', UserRepository::class), ) ]; diff --git a/docs/docs/5.0/filtering/filtering.md b/docs/docs/5.0/filtering/filtering.md index d49d48078..4515493a3 100644 --- a/docs/docs/5.0/filtering/filtering.md +++ b/docs/docs/5.0/filtering/filtering.md @@ -251,7 +251,7 @@ public static function sorts(): array return [ 'users.name' => SortableFilter::make() ->setColumn('users.name') - ->usingBelongsTo( + ->usingRelation( BelongsTo::make('user', 'user', UserRepository::class), ) ]; diff --git a/src/Eager/RelatedCollection.php b/src/Eager/RelatedCollection.php index 207342da4..02e6ec965 100644 --- a/src/Eager/RelatedCollection.php +++ b/src/Eager/RelatedCollection.php @@ -7,6 +7,7 @@ use Binaryk\LaravelRestify\Fields\Contracts\Sortable; use Binaryk\LaravelRestify\Fields\EagerField; use Binaryk\LaravelRestify\Fields\Field; +use Binaryk\LaravelRestify\Fields\HasOne; use Binaryk\LaravelRestify\Fields\MorphToMany; use Binaryk\LaravelRestify\Filters\SortableFilter; use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; @@ -59,8 +60,8 @@ public function mapIntoSortable(): self ->map(function (Sortable $field) { $filter = SortableFilter::make(); - if ($field instanceof BelongsTo) { - return $filter->usingBelongsTo($field)->setColumn($field->qualifySortable()); + if ($field instanceof BelongsTo || $field instanceof HasOne) { + return $filter->usingRelation($field)->setColumn($field->qualifySortable()); } return null; diff --git a/src/Fields/HasOne.php b/src/Fields/HasOne.php index ed554e560..9c151e37c 100644 --- a/src/Fields/HasOne.php +++ b/src/Fields/HasOne.php @@ -2,11 +2,15 @@ namespace Binaryk\LaravelRestify\Fields; +use Binaryk\LaravelRestify\Fields\Concerns\CanSort; +use Binaryk\LaravelRestify\Fields\Contracts\Sortable; use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; use Binaryk\LaravelRestify\Repositories\Repository; -class HasOne extends EagerField +class HasOne extends EagerField implements Sortable { + use CanSort; + public function __construct($relation, $parentRepository) { if (! is_a(app($parentRepository), Repository::class)) { diff --git a/src/Filters/SortCollection.php b/src/Filters/SortCollection.php index 9b9030793..b4584fc61 100644 --- a/src/Filters/SortCollection.php +++ b/src/Filters/SortCollection.php @@ -68,7 +68,6 @@ public function hydrateDefinition(Repository $repository): SortCollection return $relatedSortableFilter->syncDirection($filter->direction()); } - if (! array_key_exists($filter->column, $repository::sorts())) { return $filter; } diff --git a/src/Filters/SortableFilter.php b/src/Filters/SortableFilter.php index cb37060a2..c5afc752e 100644 --- a/src/Filters/SortableFilter.php +++ b/src/Filters/SortableFilter.php @@ -5,6 +5,7 @@ use Binaryk\LaravelRestify\Fields\BelongsTo; use Binaryk\LaravelRestify\Fields\Contracts\Sortable; use Binaryk\LaravelRestify\Fields\EagerField; +use Binaryk\LaravelRestify\Fields\HasOne; use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; use Closure; use Illuminate\Database\Eloquent\Builder; @@ -17,7 +18,7 @@ class SortableFilter extends Filter public string $direction = 'asc'; - private BelongsTo $belongsToField; + private HasOne|BelongsTo $relation; private Closure $resolver; @@ -29,23 +30,23 @@ class SortableFilter extends Filter * @param string $value * @return Builder */ - public function filter(RestifyRequest $request, Builder | Relation $query, $value) + public function filter(RestifyRequest $request, Builder|Relation $query, $value) { if (isset($this->resolver) && is_callable($this->resolver)) { return call_user_func($this->resolver, $request, $query, $value); } - if (isset($this->belongsToField)) { - if (! $this->belongsToField->authorize($request)) { + if (isset($this->relation)) { + if (!$this->relation->authorize($request)) { return $query; } // This approach could be rewritten using join. $query->orderBy( - $this->belongsToField->getRelatedModel($this->repository)::select($this->qualifyColumn()) + $this->relation->getRelatedModel($this->repository)::select($this->qualifyColumn()) ->whereColumn( - $this->belongsToField->getQualifiedKey($this->repository), - $this->belongsToField->getRelatedKey($this->repository) + $this->relationForeignColumn(), + $this->relationRelatedColumn(), ) ->orderBy($this->qualifyColumn(), $value) ->take(1), @@ -58,38 +59,38 @@ public function filter(RestifyRequest $request, Builder | Relation $query, $valu $query->orderBy($this->column, $value); } - public function usingBelongsTo(BelongsTo $field): self + public function usingRelation(HasOne|BelongsTo $field): self { - $this->belongsToField = $field; + $this->relation = $field; return $this; } public function getSortableEager(): ?Sortable { - if (! $this->hasEager()) { + if (!$this->hasEager()) { return null; } - if (! $this->getEager() instanceof Sortable) { + if (!$this->getEager() instanceof Sortable) { return null; } return $this->getEager(); } - public function getEager(): EagerField | Sortable | null + public function getEager(): EagerField|Sortable|null { - if (! $this->hasEager()) { + if (!$this->hasEager()) { return null; } - return $this->belongsToField; + return $this->relation; } public function hasEager(): bool { - return isset($this->belongsToField) && $this->belongsToField instanceof EagerField; + return isset($this->relation) && $this->relation instanceof EagerField; } public function asc(): self @@ -122,7 +123,7 @@ public function resolveFrontendColumn(): self */ $tablePlural = Str::plural(Str::before($this->column, '.')); - $this->column = $tablePlural . '.' . Str::after($this->column, '.'); + $this->column = $tablePlural.'.'.Str::after($this->column, '.'); } return $this; @@ -130,7 +131,7 @@ public function resolveFrontendColumn(): self public function syncDirection(string $direction = null): self { - if (! is_null($direction) && in_array($direction, ['asc', 'desc'])) { + if (!is_null($direction) && in_array($direction, ['asc', 'desc'])) { $this->direction = $direction; return $this; @@ -161,4 +162,22 @@ public function usingClosure(Closure $closure): self return $this; } + + private function relationForeignColumn(): string + { + if ($this->relation instanceof HasOne) { + return $this->relation->getRelation($this->repository)->getQualifiedForeignKeyName(); + } + + return $this->relation->getQualifiedKey($this->repository); + } + + private function relationRelatedColumn(): string + { + if ($this->relation instanceof HasOne) { + return $this->relation->getRelation($this->repository)->getQualifiedParentKeyName(); + } + + return $this->relation->getRelatedKey($this->repository); + } } diff --git a/tests/Feature/Filters/BelongsToFilterTest.php b/tests/Feature/Filters/BelongsToFilterTest.php index 80a121ab3..893592ee1 100644 --- a/tests/Feature/Filters/BelongsToFilterTest.php +++ b/tests/Feature/Filters/BelongsToFilterTest.php @@ -19,7 +19,7 @@ public function test_can_filter_using_belongs_to_field(): void ]; PostRepository::$sort = [ - 'users.attributes.name' => SortableFilter::make()->setColumn('users.name')->usingBelongsTo( + 'users.attributes.name' => SortableFilter::make()->setColumn('users.name')->usingRelation( BelongsTo::make('user', UserRepository::class), ), ]; diff --git a/tests/Fields/HasOneFieldTest.php b/tests/Fields/HasOneFieldTest.php index cf2dd27b3..2c3742dbe 100644 --- a/tests/Fields/HasOneFieldTest.php +++ b/tests/Fields/HasOneFieldTest.php @@ -2,9 +2,11 @@ namespace Binaryk\LaravelRestify\Tests\Fields; +use Binaryk\LaravelRestify\Fields\BelongsTo; use Binaryk\LaravelRestify\Fields\Field; use Binaryk\LaravelRestify\Fields\FieldCollection; use Binaryk\LaravelRestify\Fields\HasOne; +use Binaryk\LaravelRestify\Filters\SortableFilter; use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; use Binaryk\LaravelRestify\Repositories\Repository; use Binaryk\LaravelRestify\Restify; @@ -12,8 +14,10 @@ use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostPolicy; use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostRepository; use Binaryk\LaravelRestify\Tests\Fixtures\User\User; +use Binaryk\LaravelRestify\Tests\Fixtures\User\UserRepository; use Binaryk\LaravelRestify\Tests\IntegrationTest; use Illuminate\Support\Facades\Gate; +use Illuminate\Testing\Fluent\AssertableJson; class HasOneFieldTest extends IntegrationTest { @@ -82,6 +86,46 @@ public function test_field_ignored_when_storing() 'post' => 'wew', ])->assertCreated(); } + + public function test_can_sort_using_has_one_to_field(): void + { + UserRepository::$related = [ + 'post' => HasOne::make('post', PostRepository::class)->sortable('posts.title'), + ]; + + Post::factory()->create([ + 'title' => 'Zez', + 'user_id' => User::factory()->create([ + 'name' => 'Last', + ]), + ]); + + Post::factory()->create([ + 'title' => 'Abc', + 'user_id' => User::factory()->create([ + 'name' => 'First', + ]), + ]); + + $this + ->getJson(UserRepository::uriKey().'?related=post&sort=-post.attributes.title&perPage=5') + ->assertJson(function (AssertableJson $assertableJson) { + $assertableJson + ->where('data.1.attributes.name', 'First') + ->where('data.0.attributes.name', 'Last') + ->etc(); + }); + + $this + ->getJson(UserRepository::uriKey().'?related=post&sort=post.attributes.title&perPage=5') + ->assertJson(function (AssertableJson $assertableJson) { + $assertableJson + ->where('data.0.attributes.name', 'First') + ->where('data.1.attributes.name', 'Last') + ->etc(); + }); + + } } class UserWithPostRepository extends Repository diff --git a/tests/Fixtures/User/User.php b/tests/Fixtures/User/User.php index 04018c4bb..089d74bb1 100644 --- a/tests/Fixtures/User/User.php +++ b/tests/Fixtures/User/User.php @@ -9,6 +9,7 @@ use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Query\Builder; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; @@ -89,7 +90,7 @@ public function posts(): HasMany return $this->hasMany(Post::class); } - public function post() + public function post(): HasOne { return $this->hasOne(Post::class); } From 5ecd6503c8429e746c15f84380d09a4e3298a29e Mon Sep 17 00:00:00 2001 From: Eduard Lupacescu Date: Fri, 19 Nov 2021 16:28:36 +0200 Subject: [PATCH 2/3] fix: Return null for hasMany and belongsToMany --- src/Fields/BelongsToMany.php | 7 ++----- src/Fields/HasMany.php | 7 ++----- src/Repositories/Repository.php | 12 +++++++++--- tests/Fields/BelongsToFieldTest.php | 1 + tests/Fields/HasManyTest.php | 22 ++++++++++++---------- tests/Fields/MorphOneFieldTest.php | 6 ++++-- 6 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/Fields/BelongsToMany.php b/src/Fields/BelongsToMany.php index a4b202308..53afccbd0 100644 --- a/src/Fields/BelongsToMany.php +++ b/src/Fields/BelongsToMany.php @@ -64,11 +64,8 @@ public function resolve($repository, $attribute = null) ->resolveFromPivot($item->pivot) ) ->eagerState(); - } catch (AuthorizationException $e) { - $class = get_class($item); - $policy = get_class(Gate::getPolicyFor($item)); - - abort(403, "You are not authorized to see the [{$class}] relationship from the HasMany field from the BelongsTo field. Check the [show] method from the [$policy]"); + } catch (AuthorizationException) { + return null; } }); diff --git a/src/Fields/HasMany.php b/src/Fields/HasMany.php index 0094ea6f6..21fc1dbbb 100644 --- a/src/Fields/HasMany.php +++ b/src/Fields/HasMany.php @@ -46,11 +46,8 @@ public function resolve($repository, $attribute = null) return $this->repositoryClass::resolveWith($item) ->allowToShow(app(Request::class)) ->eagerState(); - } catch (AuthorizationException $e) { - $class = get_class($item); - $policy = get_class(Gate::getPolicyFor($item)); - - abort(403, "You are not authorized to see the [{$class}] relationship from the HasMany field from the BelongsTo field. Check the [show] method from the [$policy]"); + } catch (AuthorizationException) { + return null; } }); diff --git a/src/Repositories/Repository.php b/src/Repositories/Repository.php index e35b010f3..4a34ff9c9 100644 --- a/src/Repositories/Repository.php +++ b/src/Repositories/Repository.php @@ -515,9 +515,15 @@ public function resolveRelationships($request): array return $collection->forIndex($request, $this); }) ->mapIntoRelated($request) - ->map(function (Related $related) use ($request) { - return $related->resolve($request, $this)->getValue(); - })->all(); + ->map(fn (Related $related) => $related->resolve($request, $this)->getValue()) + ->map(function(mixed $items) { + if ($items instanceof Collection) { + return $items->filter(); + } + + return $items; + }) + ->all(); } /** diff --git a/tests/Fields/BelongsToFieldTest.php b/tests/Fields/BelongsToFieldTest.php index 443b13b64..dec0a0c7e 100644 --- a/tests/Fields/BelongsToFieldTest.php +++ b/tests/Fields/BelongsToFieldTest.php @@ -15,6 +15,7 @@ use Binaryk\LaravelRestify\Tests\Fixtures\User\UserRepository; use Binaryk\LaravelRestify\Tests\IntegrationTest; use Illuminate\Support\Facades\Gate; +use Illuminate\Testing\Fluent\AssertableJson; class BelongsToFieldTest extends IntegrationTest { diff --git a/tests/Fields/HasManyTest.php b/tests/Fields/HasManyTest.php index 82df34f47..f9128866d 100644 --- a/tests/Fields/HasManyTest.php +++ b/tests/Fields/HasManyTest.php @@ -13,6 +13,7 @@ use Binaryk\LaravelRestify\Tests\IntegrationTest; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Gate; +use Illuminate\Testing\Fluent\AssertableJson; class HasManyTest extends IntegrationTest { @@ -36,7 +37,7 @@ protected function tearDown(): void Repository::clearResolvedInstances(); } - public function test_has_many_present_on_relations() + public function test_has_many_present_on_relations(): void { $user = User::factory()->create(); @@ -59,7 +60,7 @@ public function test_has_many_present_on_relations() ]); } - public function test_has_many_paginated_on_relation() + public function test_has_many_paginated_on_relation(): void { $user = tap($this->mockUsers()->first(), function ($user) { $this->mockPosts($user->id, 22); @@ -69,7 +70,7 @@ public function test_has_many_paginated_on_relation() ->assertJsonCount(20, 'data.relationships.posts'); } - public function test_has_many_unauthorized_see_relationship_posts() + public function test_has_many_filter_unauthorized_to_see_relationship_posts(): void { $_SERVER['restify.post.show'] = false; @@ -79,7 +80,8 @@ public function test_has_many_unauthorized_see_relationship_posts() }); $this->getJson(UserWithPosts::uriKey()."/$user->id?related=posts") - ->assertForbidden(); + ->assertOk() + ->assertJson(fn(AssertableJson $json) => $json->count('data.relationships.posts', 0)->etc()); } public function test_field_ignored_when_storing() @@ -107,7 +109,7 @@ public function test_can_display_other_pages() field('email'), field('password'), - HasMany::make('posts', PostRepository::class), + HasMany::make('posts', PostRepository::class), ]); $this->getJson(UserWithPosts::uriKey()."/{$u->id}/posts?perPage=5") @@ -131,7 +133,7 @@ public function test_can_apply_filters(): void field('email'), field('password'), - HasMany::make('posts', PostRepository::class), + HasMany::make('posts', PostRepository::class), ]); $this->getJson(UserWithPosts::uriKey()."/{$u->id}/posts?title=wew") @@ -155,7 +157,7 @@ public function test_filter_unauthorized_posts() field('email'), field('password'), - HasMany::make('posts', PostRepository::class), + HasMany::make('posts', PostRepository::class), ]); $this->getJson(UserWithPosts::uriKey()."/{$u->id}/posts") @@ -183,7 +185,7 @@ public function test_can_store() field('email'), field('password'), - HasMany::make('posts', PostRepository::class), + HasMany::make('posts', PostRepository::class), ]); $this->postJson(UserWithPosts::uriKey()."/{$u->id}/posts", [ @@ -204,7 +206,7 @@ public function test_can_show(): void field('email'), field('password'), - HasMany::make('posts', PostRepository::class), + HasMany::make('posts', PostRepository::class), ]); $this->getJson(UserWithPosts::to("$userId/posts/$post->id"), [ @@ -300,7 +302,7 @@ class UserWithPosts extends Repository public static function related(): array { return [ - 'posts' => HasMany::make('posts', PostRepository::class), + 'posts' => HasMany::make('posts', PostRepository::class), ]; } diff --git a/tests/Fields/MorphOneFieldTest.php b/tests/Fields/MorphOneFieldTest.php index 8a275f94a..cd508050a 100644 --- a/tests/Fields/MorphOneFieldTest.php +++ b/tests/Fields/MorphOneFieldTest.php @@ -30,13 +30,15 @@ protected function tearDown(): void Repository::clearResolvedInstances(); } - public function test_morph_one_present_on_show_when_specified_related() + public function test_morph_one_present_on_show_when_specified_related(): void { $post = Post::factory()->create([ 'user_id' => User::factory(), ]); - $relationships = $this->getJson(PostWithMophOneRepository::uriKey()."/$post->id?related=user") + $relationships = $this + ->withoutExceptionHandling() + ->getJson(PostWithMophOneRepository::uriKey()."/$post->id?related=user") ->assertJsonStructure([ 'data' => [ 'relationships' => [ From ccff182d9275757a00daa6a8f4a5727e4dd54f86 Mon Sep 17 00:00:00 2001 From: binaryk Date: Fri, 19 Nov 2021 14:28:59 +0000 Subject: [PATCH 3/3] Fix styling --- src/Fields/BelongsToMany.php | 1 - src/Fields/HasMany.php | 1 - src/Filters/SortableFilter.php | 10 +++++----- src/Repositories/Repository.php | 2 +- tests/Fields/BelongsToFieldTest.php | 1 - tests/Fields/HasManyTest.php | 2 +- tests/Fields/HasOneFieldTest.php | 3 --- 7 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/Fields/BelongsToMany.php b/src/Fields/BelongsToMany.php index 53afccbd0..15c0bed6b 100644 --- a/src/Fields/BelongsToMany.php +++ b/src/Fields/BelongsToMany.php @@ -9,7 +9,6 @@ use Closure; use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Gate; class BelongsToMany extends EagerField { diff --git a/src/Fields/HasMany.php b/src/Fields/HasMany.php index 21fc1dbbb..f74ae4da1 100644 --- a/src/Fields/HasMany.php +++ b/src/Fields/HasMany.php @@ -7,7 +7,6 @@ use Binaryk\LaravelRestify\Repositories\Repository; use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Gate; class HasMany extends EagerField { diff --git a/src/Filters/SortableFilter.php b/src/Filters/SortableFilter.php index c5afc752e..ce90c2446 100644 --- a/src/Filters/SortableFilter.php +++ b/src/Filters/SortableFilter.php @@ -37,7 +37,7 @@ public function filter(RestifyRequest $request, Builder|Relation $query, $value) } if (isset($this->relation)) { - if (!$this->relation->authorize($request)) { + if (! $this->relation->authorize($request)) { return $query; } @@ -68,11 +68,11 @@ public function usingRelation(HasOne|BelongsTo $field): self public function getSortableEager(): ?Sortable { - if (!$this->hasEager()) { + if (! $this->hasEager()) { return null; } - if (!$this->getEager() instanceof Sortable) { + if (! $this->getEager() instanceof Sortable) { return null; } @@ -81,7 +81,7 @@ public function getSortableEager(): ?Sortable public function getEager(): EagerField|Sortable|null { - if (!$this->hasEager()) { + if (! $this->hasEager()) { return null; } @@ -131,7 +131,7 @@ public function resolveFrontendColumn(): self public function syncDirection(string $direction = null): self { - if (!is_null($direction) && in_array($direction, ['asc', 'desc'])) { + if (! is_null($direction) && in_array($direction, ['asc', 'desc'])) { $this->direction = $direction; return $this; diff --git a/src/Repositories/Repository.php b/src/Repositories/Repository.php index 4a34ff9c9..baf671a1c 100644 --- a/src/Repositories/Repository.php +++ b/src/Repositories/Repository.php @@ -516,7 +516,7 @@ public function resolveRelationships($request): array }) ->mapIntoRelated($request) ->map(fn (Related $related) => $related->resolve($request, $this)->getValue()) - ->map(function(mixed $items) { + ->map(function (mixed $items) { if ($items instanceof Collection) { return $items->filter(); } diff --git a/tests/Fields/BelongsToFieldTest.php b/tests/Fields/BelongsToFieldTest.php index dec0a0c7e..443b13b64 100644 --- a/tests/Fields/BelongsToFieldTest.php +++ b/tests/Fields/BelongsToFieldTest.php @@ -15,7 +15,6 @@ use Binaryk\LaravelRestify\Tests\Fixtures\User\UserRepository; use Binaryk\LaravelRestify\Tests\IntegrationTest; use Illuminate\Support\Facades\Gate; -use Illuminate\Testing\Fluent\AssertableJson; class BelongsToFieldTest extends IntegrationTest { diff --git a/tests/Fields/HasManyTest.php b/tests/Fields/HasManyTest.php index f9128866d..0b97125d6 100644 --- a/tests/Fields/HasManyTest.php +++ b/tests/Fields/HasManyTest.php @@ -81,7 +81,7 @@ public function test_has_many_filter_unauthorized_to_see_relationship_posts(): v $this->getJson(UserWithPosts::uriKey()."/$user->id?related=posts") ->assertOk() - ->assertJson(fn(AssertableJson $json) => $json->count('data.relationships.posts', 0)->etc()); + ->assertJson(fn (AssertableJson $json) => $json->count('data.relationships.posts', 0)->etc()); } public function test_field_ignored_when_storing() diff --git a/tests/Fields/HasOneFieldTest.php b/tests/Fields/HasOneFieldTest.php index 2c3742dbe..915ecefcc 100644 --- a/tests/Fields/HasOneFieldTest.php +++ b/tests/Fields/HasOneFieldTest.php @@ -2,11 +2,9 @@ namespace Binaryk\LaravelRestify\Tests\Fields; -use Binaryk\LaravelRestify\Fields\BelongsTo; use Binaryk\LaravelRestify\Fields\Field; use Binaryk\LaravelRestify\Fields\FieldCollection; use Binaryk\LaravelRestify\Fields\HasOne; -use Binaryk\LaravelRestify\Filters\SortableFilter; use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; use Binaryk\LaravelRestify\Repositories\Repository; use Binaryk\LaravelRestify\Restify; @@ -124,7 +122,6 @@ public function test_can_sort_using_has_one_to_field(): void ->where('data.1.attributes.name', 'Last') ->etc(); }); - } }