Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/Eager/Related.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -96,7 +96,6 @@ public function resolve(RestifyRequest $request, Repository $repository): self
return $this;
}


public function jsonSerialize()
{
return [
Expand Down
73 changes: 35 additions & 38 deletions src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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) {
Expand All @@ -477,7 +474,7 @@ function ($items) {
return false;
}

if (!$field->authorize($request)) {
if (! $field->authorize($request)) {
return false;
}

Expand All @@ -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) {
Expand All @@ -527,7 +524,7 @@ function ($items) {
return false;
}

if (!$field->authorize($request)) {
if (! $field->authorize($request)) {
return false;
}

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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)),
]
)
);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
});
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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();
Expand All @@ -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()
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -989,7 +986,7 @@ protected function getType(RestifyRequest $request): ?string

protected function getId(RestifyRequest $request): ?string
{
if (!static::$id) {
if (! static::$id) {
return null;
}

Expand Down Expand Up @@ -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)
Expand All @@ -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();
}

Expand Down Expand Up @@ -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(),
];
Expand Down
1 change: 0 additions & 1 deletion src/Resolvable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Binaryk\LaravelRestify;


use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
use Binaryk\LaravelRestify\Repositories\Repository;

Expand Down
14 changes: 7 additions & 7 deletions src/Services/Search/RepositorySearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '-');
Expand Down Expand Up @@ -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)
Expand All @@ -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;
Expand Down