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
2 changes: 1 addition & 1 deletion src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ protected function fillAttributeFromRequest(RestifyRequest $request, $model, $at
? $attribute
: "{$bulkRow}.{$attribute}";

if(! ($request->exists($attribute) || $request->input($attribute))) {
if (! ($request->exists($attribute) || $request->input($attribute))) {
return;
}

Expand Down
71 changes: 35 additions & 36 deletions src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Binaryk\LaravelRestify\Fields\Field;
use Binaryk\LaravelRestify\Fields\FieldCollection;
use Binaryk\LaravelRestify\Filter;
use Binaryk\LaravelRestify\Http\Requests\RepositoryIndexRequest;
use Binaryk\LaravelRestify\Http\Requests\RepositoryShowRequest;
use Binaryk\LaravelRestify\Http\Requests\RepositoryStoreBulkRequest;
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
Expand Down Expand Up @@ -404,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 @@ -454,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,28 +515,28 @@ public function resolveRelationships($request): array
if (! $this->isEagerState() && $request instanceof RepositoryShowRequest) {
$this->collectFields($request)
->forEager($request, $this)
->filter(fn(EagerField $field) => $field->isShownOnShow($request, $this))
->each(fn(EagerField $field) => $withs->put($field->attribute, $field->resolve($this)->value));
->filter(fn (EagerField $field) => $field->isShownOnShow($request, $this))
->each(fn (EagerField $field) => $withs->put($field->attribute, $field->resolve($this)->value));
}

collect(str_getcsv($request->input('related')))
->filter(fn($relation) => in_array($relation, static::getRelated()))
->filter(fn ($relation) => in_array($relation, static::getRelated()))
->each(function ($relation) use ($request, $withs) {
$paginator = $this->resource->relationLoaded($relation)
? $this->resource->{$relation}
: $this->resource->{$relation}();

collect([
Builder::class => fn() => $withs->put($relation, $paginator->take($request->input('relatablePerPage') ?? (static::$defaultRelatablePerPage ?? RestifySearchable::DEFAULT_RELATABLE_PER_PAGE))->get()),
Builder::class => fn () => $withs->put($relation, $paginator->take($request->input('relatablePerPage') ?? (static::$defaultRelatablePerPage ?? RestifySearchable::DEFAULT_RELATABLE_PER_PAGE))->get()),

Relation::class => fn() => $withs->put($relation, $paginator->take($request->input('relatablePerPage') ?? (static::$defaultRelatablePerPage ?? RestifySearchable::DEFAULT_RELATABLE_PER_PAGE))->get()),
Relation::class => fn () => $withs->put($relation, $paginator->take($request->input('relatablePerPage') ?? (static::$defaultRelatablePerPage ?? RestifySearchable::DEFAULT_RELATABLE_PER_PAGE))->get()),

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() => $withs->put($relation, $paginator)
])->first(fn ($fn, $class) => $paginator instanceof $class,
fn () => $withs->put($relation, $paginator)
)();
});

Expand Down Expand Up @@ -591,14 +590,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 @@ -648,7 +647,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 @@ -679,7 +678,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 @@ -706,7 +705,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 @@ -735,7 +734,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 @@ -768,7 +767,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 @@ -800,18 +799,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 @@ -896,7 +895,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->getId($request)),
'id' => $this->when($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 @@ -909,7 +908,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 @@ -951,7 +950,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 @@ -963,12 +962,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
8 changes: 4 additions & 4 deletions tests/Fields/BelongsToFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function test_present_on_relations()
'user_id' => factory(User::class),
]);

$this->get(PostWithUserRepository::uriKey() . "/$post->id")
$this->get(PostWithUserRepository::uriKey()."/$post->id")
->assertJsonStructure([
'data' => [
'relationships' => [
Expand All @@ -61,7 +61,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 @@ -145,7 +145,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 @@ -165,7 +165,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
8 changes: 4 additions & 4 deletions tests/Fields/BelongsToManyFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function test_displays_on_relationships_show()
);
});

$this->get(CompanyWithUsersRepository::uriKey() . "/{$company->id}")
$this->get(CompanyWithUsersRepository::uriKey()."/{$company->id}")
->assertJsonStructure([
'data' => [
'relationships' => [
Expand All @@ -50,14 +50,14 @@ public function test_can_hide_relationships()

$_SERVER['hide_users_from_show'] = true;

$this->get(CompanyWithUsersRepository::uriKey() . "/{$company->id}")
$this->get(CompanyWithUsersRepository::uriKey()."/{$company->id}")
->assertJsonStructure([
'data' => [],
])->assertJsonMissing([
[
'relationships' => [
'users' => [],
]]
], ],
]);
}

Expand All @@ -77,7 +77,7 @@ public function test_ignored_when_storing()
[
'relationships' => [
'users' => [],
]]
], ],
]);
}
}
Expand Down
Loading