From 23f712abc3996b66f4254657874d12e531ce40d7 Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Sat, 16 Jan 2021 12:14:44 +0000 Subject: [PATCH] Apply fixes from StyleCI --- src/Eager/Related.php | 3 +- src/Repositories/Repository.php | 73 +++++++++---------- src/Resolvable.php | 1 - .../Search/RepositorySearchService.php | 14 ++-- 4 files changed, 43 insertions(+), 48 deletions(-) diff --git a/src/Eager/Related.php b/src/Eager/Related.php index bb03e49a7..290bd43ca 100644 --- a/src/Eager/Related.php +++ b/src/Eager/Related.php @@ -37,7 +37,7 @@ public function __construct(string $relation, EagerField $field = null) public function isEager(): bool { - return !is_null($this->field); + return ! is_null($this->field); } public function getRelation(): string @@ -96,7 +96,6 @@ public function resolve(RestifyRequest $request, Repository $repository): self return $this; } - public function jsonSerialize() { return [ diff --git a/src/Repositories/Repository.php b/src/Repositories/Repository.php index 7a655f0d8..2e2ac961c 100644 --- a/src/Repositories/Repository.php +++ b/src/Repositories/Repository.php @@ -24,15 +24,12 @@ 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\Database\Eloquent\Relations\Relation; use Illuminate\Http\Request; use Illuminate\Http\Resources\ConditionallyLoadsAttributes; use Illuminate\Http\Resources\DelegatesToResource; use Illuminate\Pagination\AbstractPaginator; use Illuminate\Routing\Router; -use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; @@ -283,7 +280,7 @@ public static function newModel(): Model public static function query(RestifyRequest $request) { - if (!$request->isViaRepository()) { + if (! $request->isViaRepository()) { return static::newModel()->query(); } @@ -450,16 +447,16 @@ public function resolveShowAttributes(RestifyRequest $request) { $fields = $this->collectFields($request) ->forShow($request, $this) - ->filter(fn(Field $field) => $field->authorize($request)) + ->filter(fn (Field $field) => $field->authorize($request)) ->when( $this->isEagerState(), function ($items) { - return $items->filter(fn(Field $field) => !$field instanceof EagerField); + return $items->filter(fn (Field $field) => ! $field instanceof EagerField); } ) - ->each(fn(Field $field) => $field->resolveForShow($this)) - ->map(fn(Field $field) => $field->serializeToValue($request)) - ->mapWithKeys(fn($value) => $value) + ->each(fn (Field $field) => $field->resolveForShow($this)) + ->map(fn (Field $field) => $field->serializeToValue($request)) + ->mapWithKeys(fn ($value) => $value) ->all(); if ($this instanceof Mergeable) { @@ -477,7 +474,7 @@ function ($items) { return false; } - if (!$field->authorize($request)) { + if (! $field->authorize($request)) { return false; } @@ -500,16 +497,16 @@ public function resolveIndexAttributes($request) $fields = $this ->collectFields($request) ->forIndex($request, $this) - ->filter(fn(Field $field) => $field->authorize($request)) + ->filter(fn (Field $field) => $field->authorize($request)) ->when( $this->eagerState, function ($items) { - return $items->filter(fn(Field $field) => !$field instanceof EagerField); + return $items->filter(fn (Field $field) => ! $field instanceof EagerField); } ) - ->each(fn(Field $field) => $field->resolveForIndex($this)) - ->map(fn(Field $field) => $field->serializeToValue($request)) - ->mapWithKeys(fn($value) => $value) + ->each(fn (Field $field) => $field->resolveForIndex($this)) + ->map(fn (Field $field) => $field->serializeToValue($request)) + ->mapWithKeys(fn ($value) => $value) ->all(); if ($this instanceof Mergeable) { @@ -527,7 +524,7 @@ function ($items) { return false; } - if (!$field->authorize($request)) { + if (! $field->authorize($request)) { return false; } @@ -555,10 +552,10 @@ public function resolveShowPivots(RestifyRequest $request): array } return $pivots - ->filter(fn(Field $field) => $field->authorize($request)) - ->each(fn(Field $field) => $field->resolve($this)) - ->map(fn(Field $field) => $field->serializeToValue($request)) - ->mapWithKeys(fn($value) => $value) + ->filter(fn (Field $field) => $field->authorize($request)) + ->each(fn (Field $field) => $field->resolve($this)) + ->map(fn (Field $field) => $field->serializeToValue($request)) + ->mapWithKeys(fn ($value) => $value) ->all(); } @@ -637,14 +634,14 @@ public function index(RestifyRequest $request) [ 'meta' => $this->when( $meta = $this->resolveIndexMainMeta( - $request, $models = $items->map(fn(self $repository) => $repository->resource), RepositoryCollection::meta($paginator->toArray()) + $request, $models = $items->map(fn (self $repository) => $repository->resource), RepositoryCollection::meta($paginator->toArray()) ), $meta ), 'links' => $this->when( $links = $this->resolveIndexLinks($request, $models, RepositoryCollection::paginationLinks($paginator->toArray())), $links ), - 'data' => $items->map(fn(self $repository) => $repository->serializeForIndex($request)), + 'data' => $items->map(fn (self $repository) => $repository->serializeForIndex($request)), ] ) ); @@ -702,7 +699,7 @@ public function store(RestifyRequest $request) ->save(); } - $fields->each(fn(Field $field) => $field->invokeAfter($request, $this->resource)); + $fields->each(fn (Field $field) => $field->invokeAfter($request, $this->resource)); }); static::stored($this->resource, $request); @@ -733,7 +730,7 @@ public function storeBulk(RepositoryStoreBulkRequest $request) $this->resource->save(); - $fields->each(fn(Field $field) => $field->invokeAfter($request, $this->resource)); + $fields->each(fn (Field $field) => $field->invokeAfter($request, $this->resource)); return $this->resource; }); @@ -766,7 +763,7 @@ public function update(RestifyRequest $request, $repositoryId) return $fields; })->each( - fn(Field $field) => $field->invokeAfter($request, $this->resource) + fn (Field $field) => $field->invokeAfter($request, $this->resource) ); return $this->response() @@ -795,7 +792,7 @@ public function attach(RestifyRequest $request, $repositoryId, Collection $pivot $eagerField = $this->authorizeBelongsToMany($request)->belongsToManyField($request); DB::transaction(function () use ($request, $pivots, $eagerField) { - $fields = $eagerField->collectPivotFields()->filter(fn($pivotField) => $request->has($pivotField->attribute))->values(); + $fields = $eagerField->collectPivotFields()->filter(fn ($pivotField) => $request->has($pivotField->attribute))->values(); $pivots->map(function ($pivot) use ($request, $fields, $eagerField) { static::validatorForAttach($request)->validate(); @@ -822,7 +819,7 @@ public function detach(RestifyRequest $request, $repositoryId, Collection $pivot $deleted = DB::transaction(function () use ($pivots, $eagerField, $request) { return $pivots - ->map(fn($pivot) => $eagerField->authorizeToDetach($request, $pivot) && $pivot->delete()); + ->map(fn ($pivot) => $eagerField->authorizeToDetach($request, $pivot) && $pivot->delete()); }); return $this->response() @@ -860,18 +857,18 @@ public function allowToUpdate(RestifyRequest $request, $payload = null): self public function allowToAttach(RestifyRequest $request, Collection $attachers): self { - $methodGuesser = 'attach' . Str::studly($request->relatedRepository); + $methodGuesser = 'attach'.Str::studly($request->relatedRepository); - $attachers->each(fn($model) => $this->authorizeToAttach($request, $methodGuesser, $model)); + $attachers->each(fn ($model) => $this->authorizeToAttach($request, $methodGuesser, $model)); return $this; } public function allowToDetach(RestifyRequest $request, Collection $attachers): self { - $methodGuesser = 'detach' . Str::studly($request->relatedRepository); + $methodGuesser = 'detach'.Str::studly($request->relatedRepository); - $attachers->each(fn($model) => $this->authorizeToDetach($request, $methodGuesser, $model)); + $attachers->each(fn ($model) => $this->authorizeToDetach($request, $methodGuesser, $model)); return $this; } @@ -961,7 +958,7 @@ public function response($content = '', $status = 200, array $headers = []): Res public function serializeForShow(RestifyRequest $request): array { return $this->filter([ - 'id' => $this->when(optional($this->resource)->id, fn() => $this->getId($request)), + 'id' => $this->when(optional($this->resource)->id, fn () => $this->getId($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), @@ -975,7 +972,7 @@ public function serializeForIndex(RestifyRequest $request): array return $this->filter([ 'id' => $this->when($id = $this->getId($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->resolveIndexRelationships($request)), $related), 'meta' => $this->when(value($meta = $this->resolveIndexMeta($request)), $meta), 'pivots' => $this->when(value($pivots = $this->resolveIndexPivots($request)), $pivots), @@ -989,7 +986,7 @@ protected function getType(RestifyRequest $request): ?string protected function getId(RestifyRequest $request): ?string { - if (!static::$id) { + if (! static::$id) { return null; } @@ -1018,7 +1015,7 @@ private function modelAttributes(Request $request = null): Collection */ protected static function fillFields(RestifyRequest $request, Model $model, Collection $fields) { - return $fields->map(fn(Field $field) => $field->fillAttribute($request, $model)); + return $fields->map(fn (Field $field) => $field->fillAttribute($request, $model)); } protected static function fillBulkFields(RestifyRequest $request, Model $model, Collection $fields, int $bulkRow = null) @@ -1030,12 +1027,12 @@ protected static function fillBulkFields(RestifyRequest $request, Model $model, public static function uriTo(Model $model) { - return Str::replaceFirst('//', '/', Restify::path() . '/' . static::uriKey() . '/' . $model->getKey()); + return Str::replaceFirst('//', '/', 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(); } @@ -1074,7 +1071,7 @@ public function restifyjsSerialize(RestifyRequest $request): array 'sort' => static::collectFilters('sortables'), 'match' => static::collectFilters('matches'), 'searchables' => static::collectFilters('searchables'), - 'actions' => $this->resolveActions($request)->filter(fn(Action $action) => $action->isShownOnIndex( + 'actions' => $this->resolveActions($request)->filter(fn (Action $action) => $action->isShownOnIndex( $request, $this ))->values(), ]; diff --git a/src/Resolvable.php b/src/Resolvable.php index c110c95b5..5d889ba08 100644 --- a/src/Resolvable.php +++ b/src/Resolvable.php @@ -2,7 +2,6 @@ namespace Binaryk\LaravelRestify; - use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; use Binaryk\LaravelRestify\Repositories\Repository; diff --git a/src/Services/Search/RepositorySearchService.php b/src/Services/Search/RepositorySearchService.php index e49b85111..4de358d84 100644 --- a/src/Services/Search/RepositorySearchService.php +++ b/src/Services/Search/RepositorySearchService.php @@ -42,15 +42,15 @@ public function prepareMatchFields(RestifyRequest $request, $query, $extra = []) foreach ($this->repository->getMatchByFields() as $key => $type) { $negation = false; - if ($request->has('-' . $key)) { + if ($request->has('-'.$key)) { $negation = true; } - if (!$request->has($negation ? '-' . $key : $key) && !data_get($extra, "match.$key")) { + if (! $request->has($negation ? '-'.$key : $key) && ! data_get($extra, "match.$key")) { continue; } - $match = $request->input($negation ? '-' . $key : $key, data_get($extra, "match.$key")); + $match = $request->input($negation ? '-'.$key : $key, data_get($extra, "match.$key")); if ($negation) { $key = Str::after($key, '-'); @@ -170,17 +170,17 @@ public function prepareSearchFields(RestifyRequest $request, $query, $extra = [] protected function applyIndexQuery(RestifyRequest $request, Repository $repository) { - return fn($query) => $repository::indexQuery($request, $query); + return fn ($query) => $repository::indexQuery($request, $query); } protected function applyMainQuery(RestifyRequest $request, Repository $repository) { - return fn($query) => $repository::mainQuery($request, $query->with($repository::getWiths())); + return fn ($query) => $repository::mainQuery($request, $query->with($repository::getWiths())); } 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) @@ -206,7 +206,7 @@ protected function applyFilters(RestifyRequest $request, Repository $repository, 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;