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
75 changes: 37 additions & 38 deletions src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,16 +403,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->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->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 Down Expand Up @@ -453,16 +453,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 Down Expand Up @@ -516,17 +516,16 @@ public function resolveRelationships($request): array
->forEager($request, $this)
->merge(
collect(static::getRelated())
->filter(fn($value, $key) => $value instanceof EagerField)
->filter(fn(Field $field) => $field->authorize($request))
->filter(fn ($value, $key) => $value instanceof EagerField)
->filter(fn (Field $field) => $field->authorize($request))
->unique()
)
->filter(fn(EagerField $field) => in_array($field->attribute, str_getcsv($request->input('related'))))
->filter(fn (EagerField $field) => in_array($field->attribute, str_getcsv($request->input('related'))))
->unique('attribute');


collect(str_getcsv($request->input('related')))
->filter(fn($relation) => in_array($relation, static::gerRelatedKeys()))
->each(function ($relation) use ($request, $withs, $definedEagers) {
->filter(fn ($relation) => in_array($relation, static::gerRelatedKeys()))
->each(function ($relation) use ($request, $withs) {
if (Str::contains($relation, '.')) {
$this->resource->loadMissing($relation);

Expand All @@ -542,16 +541,16 @@ public function resolveRelationships($request): array
: $this->resource->{$relation}();

collect([
Builder::class => fn() => $withs->put($relation, (static::$relatedCast)::fromBuilder($request, $paginator, $this)),
Builder::class => fn () => $withs->put($relation, (static::$relatedCast)::fromBuilder($request, $paginator, $this)),

Relation::class => fn() => $withs->put($relation, (static::$relatedCast)::fromRelation($request, $paginator, $this)),
Relation::class => fn () => $withs->put($relation, (static::$relatedCast)::fromRelation($request, $paginator, $this)),

Collection::class => fn() => $withs->put($relation, $paginator),
Collection::class => fn () => $withs->put($relation, $paginator),

Model::class => fn() => fn() => $withs->put($relation, $paginator),
Model::class => fn () => fn () => $withs->put($relation, $paginator),

])->first(fn($fn, $class) => $paginator instanceof $class,
fn() => fn() => $withs->put($relation, $paginator)
])->first(fn ($fn, $class) => $paginator instanceof $class,
fn () => fn () => $withs->put($relation, $paginator)
)();
});

Expand Down Expand Up @@ -605,14 +604,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 @@ -662,7 +661,7 @@ public function store(RestifyRequest $request)
}
}

$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 @@ -693,7 +692,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 All @@ -720,7 +719,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 @@ -749,7 +748,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 Down Expand Up @@ -782,7 +781,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 @@ -814,18 +813,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 @@ -910,7 +909,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 @@ -923,7 +922,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),
]);
Expand Down Expand Up @@ -965,7 +964,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 @@ -977,12 +976,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
2 changes: 1 addition & 1 deletion src/Traits/InteractWithSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static function getRelated()
public static function gerRelatedKeys(): array
{
return collect(static::getRelated())
->map(fn($value, $key) => is_numeric($key) ? $value : $key)
->map(fn ($value, $key) => is_numeric($key) ? $value : $key)
->values()
->all();
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Fields/BelongsToFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function test_present_on_show_when_specified_related()
//
// $this->assertNull($relationships);

$relationships = $this->get(PostWithUserRepository::uriKey() . "/$post->id?related=user")
$relationships = $this->get(PostWithUserRepository::uriKey()."/$post->id?related=user")
->json('data.relationships');

dd($relationships);
Expand All @@ -66,7 +66,7 @@ public function test_unauthorized_see_relationship()
tap(factory(Post::class)->create([
'user_id' => factory(User::class),
]), function ($post) {
$this->get(PostWithUserRepository::uriKey() . "/{$post->id}")
$this->get(PostWithUserRepository::uriKey()."/{$post->id}")
->assertForbidden();
});
}
Expand Down Expand Up @@ -150,7 +150,7 @@ public function test_field_used_when_updating()
'user_id' => factory(User::class),
]), function ($post) {
$newOwner = factory(User::class)->create();
$this->put(PostWithUserRepository::uriKey() . "/{$post->id}", [
$this->put(PostWithUserRepository::uriKey()."/{$post->id}", [
'title' => 'Can change post owner.',
'user' => $newOwner->id,
])->assertOk();
Expand All @@ -170,7 +170,7 @@ public function test_unauthorized_via_policy_when_updating()
]), function ($post) {
$firstOwnerId = $post->user->id;
$newOwner = factory(User::class)->create();
$this->put(PostWithUserRepository::uriKey() . "/{$post->id}", [
$this->put(PostWithUserRepository::uriKey()."/{$post->id}", [
'title' => 'Can change post owner.',
'user' => $newOwner->id,
])->assertForbidden();
Expand Down