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
6 changes: 3 additions & 3 deletions src/Fields/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class BelongsToMany extends EagerField

public function __construct($attribute, $relation, $parentRepository)
{
if (!is_a(app($parentRepository), Repository::class)) {
abort(500, "Invalid parent repository [{$parentRepository}]. Expended instance of " . Repository::class);
if (! is_a(app($parentRepository), Repository::class)) {
abort(500, "Invalid parent repository [{$parentRepository}]. Expended instance of ".Repository::class);
}

parent::__construct($attribute);
Expand All @@ -55,7 +55,7 @@ public function resolve($repository, $attribute = null)
->allowToShow(app(Request::class))
->withPivots(
PivotsCollection::make($this->pivotFields)
->map(fn(Field $field) => clone $field)
->map(fn (Field $field) => clone $field)
->resolveFromPivot($item->pivot)
)
->eagerState();
Expand Down
2 changes: 1 addition & 1 deletion src/Repositories/PivotsCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class PivotsCollection extends Collection
public function resolveFromPivot(Pivot $pivot): self
{
return $this->map(function (Field $field) use ($pivot) {
return $field->resolveCallback(fn() => $pivot->{$field->attribute});
return $field->resolveCallback(fn () => $pivot->{$field->attribute});
});
}
}
80 changes: 40 additions & 40 deletions src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,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 @@ -425,16 +425,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 @@ -452,7 +452,7 @@ function ($items) {
return false;
}

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

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

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

Expand Down Expand Up @@ -530,10 +530,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 @@ -575,16 +575,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 @@ -638,14 +638,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 @@ -695,7 +695,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 @@ -726,7 +726,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 @@ -753,7 +753,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 @@ -782,7 +782,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 @@ -815,7 +815,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 @@ -847,18 +847,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 @@ -948,7 +948,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 @@ -962,7 +962,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 @@ -976,7 +976,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 @@ -1005,7 +1005,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 @@ -1017,12 +1017,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
14 changes: 7 additions & 7 deletions tests/Controllers/RepositoryAttachControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function test_attach_a_user_to_a_company()
$user = $this->mockUsers(2)->first();
$company = factory(Company::class)->create();

$response = $this->postJson('companies/' . $company->id . '/attach/users', [
$response = $this->postJson('companies/'.$company->id.'/attach/users', [
'users' => $user->id,
'is_admin' => true,
])->assertStatus(201);
Expand All @@ -32,7 +32,7 @@ public function test_pivot_field_validation()
$user = $this->mockUsers(2)->first();
$company = factory(Company::class)->create();

$this->postJson('companies/' . $company->id . '/attach/users', [
$this->postJson('companies/'.$company->id.'/attach/users', [
'users' => $user->id,
])
->assertStatus(400);
Expand All @@ -47,7 +47,7 @@ public function test_pivot_field_present_when_show()
$company->users()->attach($this->mockUsers()->first()->id);
});

$response = $this->getJson('companies/' . $company->id . '?related=users')
$response = $this->getJson('companies/'.$company->id.'?related=users')
->assertOk();

$this->assertSame(
Expand All @@ -67,7 +67,7 @@ public function test_attach_multiple_users_to_a_company()
$usersFromCompany = $this->getJson('users?viaRepository=companies&viaRepositoryId=1&viaRelationship=users');
$this->assertCount(0, $usersFromCompany->json('data'));

$response = $this->postJson('companies/' . $company->id . '/attach/users', [
$response = $this->postJson('companies/'.$company->id.'/attach/users', [
'users' => [1, 2],
'is_admin' => true,
])
Expand All @@ -91,7 +91,7 @@ public function test_after_attach_a_user_to_company_number_of_users_increased()
$this->getJson('users?viaRepository=companies&viaRepositoryId=1&viaRelationship=users')
->assertJsonCount(0, 'data');

$this->postJson('companies/' . $company->id . '/attach/users', [
$this->postJson('companies/'.$company->id.'/attach/users', [
'users' => $user->id,
'is_admin' => true,
]);
Expand All @@ -112,15 +112,15 @@ public function test_policy_to_attach_a_user_to_a_company()

$_SERVER['allow_attach_users'] = false;

$this->postJson('companies/' . $company->id . '/attach/users', [
$this->postJson('companies/'.$company->id.'/attach/users', [
'users' => $user->id,
'is_admin' => true,
])
->assertForbidden();

$_SERVER['allow_attach_users'] = true;

$this->postJson('companies/' . $company->id . '/attach/users', [
$this->postJson('companies/'.$company->id.'/attach/users', [
'users' => $user->id,
'is_admin' => true,
])
Expand Down
2 changes: 1 addition & 1 deletion tests/Controllers/RepositoryIndexControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function test_repository_with_deep_relations()
CompanyRepository::partialMock()
->expects('related')
->andReturn([
'users.posts'
'users.posts',
]);

tap(factory(Company::class)->create(), function (Company $company) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Company/CompanyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function fields(RestifyRequest $request)

BelongsToMany::make('users', 'users', UserRepository::class)->withPivot(
Field::make('is_admin')->rules('required')
)->canDetach(fn($request, $pivot) => $_SERVER['roles.canDetach.users']),
)->canDetach(fn ($request, $pivot) => $_SERVER['roles.canDetach.users']),
];
}
}