diff --git a/routes/api.php b/routes/api.php index d81e86b6b..2a9908094 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,31 +1,30 @@ attach( $request, $request->repositoryId, collect(Arr::wrap($request->input($request->relatedRepository))) - ->map(fn($relatedRepositoryId) => $this->initializePivot( + ->map(fn ($relatedRepositoryId) => $this->initializePivot( $request, $model->{$request->viaRelationship ?? $request->relatedRepository}(), $relatedRepositoryId )) ); @@ -61,5 +61,4 @@ protected function initializePivot(RestifyRequest $request, $relationship, $rela return $pivot; } - } diff --git a/src/Http/Controllers/RepositoryDetachController.php b/src/Http/Controllers/RepositoryDetachController.php index a7c366c12..1adeb4a1d 100644 --- a/src/Http/Controllers/RepositoryDetachController.php +++ b/src/Http/Controllers/RepositoryDetachController.php @@ -17,7 +17,7 @@ public function __invoke(RepositoryDetachRequest $request) return $repository->detach( $request, $request->repositoryId, collect(Arr::wrap($request->input($request->relatedRepository))) - ->map(fn($relatedRepositoryId) => $this->initializePivot( + ->map(fn ($relatedRepositoryId) => $this->initializePivot( $request, $model->{$request->viaRelationship ?? $request->relatedRepository}(), $relatedRepositoryId )) ); @@ -61,5 +61,4 @@ protected function initializePivot(RestifyRequest $request, $relationship, $rela return $pivot; } - } diff --git a/src/Http/Requests/InteractWithRepositories.php b/src/Http/Requests/InteractWithRepositories.php index cf7009273..7d2030908 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(); } @@ -170,7 +170,6 @@ public function findRelatedModelOrFail() }); } - public function findRelatedQuery($relatedRepository = null, $relatedRepositoryId = null) { return $this->repository($relatedRepository ?? request('relatedRepository'))::newModel() @@ -178,7 +177,6 @@ public function findRelatedQuery($relatedRepository = null, $relatedRepositoryId ->whereKey($relatedRepositoryId ?? request('relatedRepositoryId')); } - protected function findPivot(RestifyRequest $request, $model) { $pivot = $model->{$request->relatedRepository}()->getPivotAccessor(); @@ -193,7 +191,7 @@ public function viaParentModel() { $parent = $this->repository($this->viaRepository); - return once(fn() => $parent::newModel()->newQueryWithoutScopes()->whereKey($this->viaRepositoryId)->firstOrFail()); + return once(fn () => $parent::newModel()->newQueryWithoutScopes()->whereKey($this->viaRepositoryId)->firstOrFail()); } public function viaQuery() @@ -217,6 +215,4 @@ public function relatedRepository() { return Restify::repositoryForKey($this->relatedRepository); } - - } diff --git a/src/Http/Requests/RepositoryAttachRequest.php b/src/Http/Requests/RepositoryAttachRequest.php index 1586752ae..972af0822 100644 --- a/src/Http/Requests/RepositoryAttachRequest.php +++ b/src/Http/Requests/RepositoryAttachRequest.php @@ -4,5 +4,4 @@ class RepositoryAttachRequest extends RestifyRequest { - } diff --git a/src/Http/Requests/RepositoryDetachRequest.php b/src/Http/Requests/RepositoryDetachRequest.php index 7dfabde02..15987806d 100644 --- a/src/Http/Requests/RepositoryDetachRequest.php +++ b/src/Http/Requests/RepositoryDetachRequest.php @@ -4,5 +4,4 @@ class RepositoryDetachRequest extends RestifyRequest { - } diff --git a/src/Repositories/Repository.php b/src/Repositories/Repository.php index 7377370ea..7ed21bdf2 100644 --- a/src/Repositories/Repository.php +++ b/src/Repositories/Repository.php @@ -8,7 +8,6 @@ use Binaryk\LaravelRestify\Fields\Field; use Binaryk\LaravelRestify\Fields\FieldCollection; use Binaryk\LaravelRestify\Filter; -use Binaryk\LaravelRestify\Http\Requests\RepositoryAttachRequest; use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; use Binaryk\LaravelRestify\Restify; use Binaryk\LaravelRestify\Services\Search\RepositorySearchService; @@ -182,7 +181,7 @@ public static function newModel(): Model public static function query(RestifyRequest $request) { - if (!$request->isViaRepository()) { + if (! $request->isViaRepository()) { return static::newModel()->query(); } @@ -239,8 +238,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); } @@ -251,14 +250,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(); } @@ -358,10 +357,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) { @@ -379,7 +378,7 @@ public function resolveShowAttributes(RestifyRequest $request) return false; } - if (!$field->authorize($request)) { + if (! $field->authorize($request)) { return false; } @@ -400,10 +399,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) { @@ -421,7 +420,7 @@ public function resolveIndexAttributes($request) return false; } - if (!$field->authorize($request)) { + if (! $field->authorize($request)) { return false; } @@ -467,7 +466,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(), ]); } @@ -519,7 +518,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()), @@ -547,7 +546,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); @@ -578,8 +577,8 @@ public function update(RestifyRequest $request, $repositoryId) public function attach(RestifyRequest $request, $repositoryId, Collection $pivots) { DB::transaction(function () use ($request, $pivots) { - return $pivots->map(fn($pivot) => $pivot->forceFill($request->except($request->relatedRepository))) - ->map(fn($pivot) => $pivot->save()); + return $pivots->map(fn ($pivot) => $pivot->forceFill($request->except($request->relatedRepository))) + ->map(fn ($pivot) => $pivot->save()); }); return $this->response() @@ -590,7 +589,7 @@ public function attach(RestifyRequest $request, $repositoryId, Collection $pivot public function detach(RestifyRequest $request, $repositoryId, Collection $pivots) { $deleted = DB::transaction(function () use ($request, $pivots) { - return $pivots->map(fn($pivot) => $pivot->delete()); + return $pivots->map(fn ($pivot) => $pivot->delete()); }); return $this->response() @@ -668,7 +667,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), @@ -681,7 +680,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), ]); @@ -724,12 +723,12 @@ 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(); } 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/tests/Controllers/RepositoryAttachControllerTest.php b/tests/Controllers/RepositoryAttachControllerTest.php index e04174d6c..24e2ade68 100644 --- a/tests/Controllers/RepositoryAttachControllerTest.php +++ b/tests/Controllers/RepositoryAttachControllerTest.php @@ -12,7 +12,7 @@ public function test_attach_a_user_to_a_company() $user = $this->mockUsers(2)->first(); $company = factory(Company::class)->create(); - $response = $this->postJson('restify-api/companies/' . $company->id . '/attach/users', [ + $response = $this->postJson('restify-api/companies/'.$company->id.'/attach/users', [ 'users' => $user->id, 'is_admin' => true, ]) @@ -32,7 +32,7 @@ public function test_attach_multiple_users_to_a_company() $usersFromCompany = $this->getJson('/restify-api/users?viaRepository=companies&viaRepositoryId=1&viaRelationship=users'); $this->assertCount(0, $usersFromCompany->json('data')); - $response = $this->postJson('restify-api/companies/' . $company->id . '/attach/users', [ + $response = $this->postJson('restify-api/companies/'.$company->id.'/attach/users', [ 'users' => [1, 2], 'is_admin' => true, ]) @@ -56,7 +56,7 @@ public function test_after_attach_a_user_to_company_number_of_users_increased() $usersFromCompany = $this->getJson('/restify-api/users?viaRepository=companies&viaRepositoryId=1&viaRelationship=users'); $this->assertCount(0, $usersFromCompany->json('data')); - $this->postJson('restify-api/companies/' . $company->id . '/attach/users', [ + $this->postJson('restify-api/companies/'.$company->id.'/attach/users', [ 'users' => $user->id, ]); diff --git a/tests/Controllers/RepositoryDetachControllerTest.php b/tests/Controllers/RepositoryDetachControllerTest.php index a7fb2c42d..8d049a70c 100644 --- a/tests/Controllers/RepositoryDetachControllerTest.php +++ b/tests/Controllers/RepositoryDetachControllerTest.php @@ -14,7 +14,7 @@ public function test_detach_a_user_from_a_company() $company->users()->attach($user->id); $usersFromCompany = $this->getJson('/restify-api/users?viaRepository=companies&viaRepositoryId=1&viaRelationship=users'); $this->assertCount(1, $usersFromCompany->json('data')); - $this->postJson('restify-api/companies/' . $company->id . '/detach/users', [ + $this->postJson('restify-api/companies/'.$company->id.'/detach/users', [ 'users' => $user->id, ]) ->assertStatus(204); @@ -31,13 +31,12 @@ public function test_detach_multiple_users_from_a_company() $usersFromCompany = $this->getJson('/restify-api/users?viaRepository=companies&viaRepositoryId=1&viaRelationship=users'); $this->assertCount(3, $usersFromCompany->json('data')); - $this->postJson('restify-api/companies/' . $company->id . '/detach/users', [ - 'users' => [1, 2] + $this->postJson('restify-api/companies/'.$company->id.'/detach/users', [ + 'users' => [1, 2], ]) ->assertStatus(204); $usersFromCompany = $this->getJson('/restify-api/users?viaRepository=companies&viaRepositoryId=1&viaRelationship=users'); $this->assertCount(1, $usersFromCompany->json('data')); } - } diff --git a/tests/Fixtures/Company/Company.php b/tests/Fixtures/Company/Company.php index 6caf50442..d1ed1dc86 100644 --- a/tests/Fixtures/Company/Company.php +++ b/tests/Fixtures/Company/Company.php @@ -16,7 +16,7 @@ public function users() { return $this->belongsToMany(User::class, 'company_user', 'company_id', 'user_id') ->withPivot([ - 'is_admin' + 'is_admin', ]) ->withTimestamps(); } diff --git a/tests/Fixtures/User/User.php b/tests/Fixtures/User/User.php index 39fea7f61..7cab46a0e 100644 --- a/tests/Fixtures/User/User.php +++ b/tests/Fixtures/User/User.php @@ -85,7 +85,7 @@ public function posts() public function companies() { return $this->belongsToMany(Company::class, 'company_user', 'user_id', 'company_id')->withPivot([ - 'is_admin' + 'is_admin', ])->withTimestamps(); }