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
4 changes: 2 additions & 2 deletions src/Fields/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class BelongsTo 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 Down
2 changes: 0 additions & 2 deletions src/Fields/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@ class BelongsToMany extends Field
public function __construct($attribute, callable $resolveCallback = null, $repository = null)
{
parent::__construct($attribute, $resolveCallback);


}
}
5 changes: 3 additions & 2 deletions src/Fields/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class HasOne extends EagerField

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

parent::__construct($attribute);
Expand Down Expand Up @@ -44,6 +44,7 @@ public function storeChild(RestifyRequest $request, Model $parent): self

if ($model instanceof CreationAware) {
$model::createWithAttributes($request->input($this->attribute));

return $this;
}

Expand Down
70 changes: 35 additions & 35 deletions src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,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 @@ -281,8 +281,8 @@ public function collectFields(RestifyRequest $request)

if ($this instanceof Mergeable) {
$fillable = collect($this->resource->getFillable())
->filter(fn($attribute) => $fields->contains('attribute', $attribute) === false)
->map(fn($attribute) => Field::new($attribute));
->filter(fn ($attribute) => $fields->contains('attribute', $attribute) === false)
->map(fn ($attribute) => Field::new($attribute));

$fields = $fields->merge($fillable);
}
Expand All @@ -293,14 +293,14 @@ public function collectFields(RestifyRequest $request)
private function indexFields(RestifyRequest $request): Collection
{
return $this->collectFields($request)
->filter(fn(Field $field) => !$field->isHiddenOnIndex($request, $this))
->filter(fn (Field $field) => ! $field->isHiddenOnIndex($request, $this))
->values();
}

private function showFields(RestifyRequest $request): Collection
{
return $this->collectFields($request)
->filter(fn(Field $field) => !$field->isHiddenOnShow($request, $this))
->filter(fn (Field $field) => ! $field->isHiddenOnShow($request, $this))
->values();
}

Expand All @@ -321,7 +321,7 @@ private function updateBulkFields(RestifyRequest $request)
private function storeFields(RestifyRequest $request)
{
return $this->collectFields($request)
->filter(fn(Field $field) => !$field instanceof EagerField)
->filter(fn (Field $field) => ! $field instanceof EagerField)
->forStore($request, $this)
->authorizedStore($request);
}
Expand All @@ -330,7 +330,7 @@ private function hasOneFields(RestifyRequest $request)
{
return $this->collectFields($request)
->forStore($request, $this)
->filter(fn(Field $field) => $field instanceof HasOne)
->filter(fn (Field $field) => $field instanceof HasOne)
->authorizedStore($request);
}

Expand Down Expand Up @@ -423,10 +423,10 @@ public static function routes(Router $router, $attributes, $wrap = false)
public function resolveShowAttributes(RestifyRequest $request)
{
$fields = $this->showFields($request)
->filter(fn(Field $field) => $field->authorize($request))
->each(fn(Field $field) => $field->resolveForShow($this))
->map(fn(Field $field) => $field->serializeToValue($request))
->mapWithKeys(fn($value) => $value)
->filter(fn (Field $field) => $field->authorize($request))
->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 @@ -444,7 +444,7 @@ public function resolveShowAttributes(RestifyRequest $request)
return false;
}

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

Expand All @@ -465,10 +465,10 @@ public function resolveIndexAttributes($request)
{
// Resolve the show method, and attach the value to the array
$fields = $this->indexFields($request)
->filter(fn(Field $field) => $field->authorize($request))
->each(fn(Field $field) => $field->resolveForIndex($this))
->map(fn(Field $field) => $field->serializeToValue($request))
->mapWithKeys(fn($value) => $value)
->filter(fn (Field $field) => $field->authorize($request))
->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 @@ -486,7 +486,7 @@ public function resolveIndexAttributes($request)
return false;
}

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

Expand Down Expand Up @@ -553,7 +553,7 @@ public function resolveRelationships($request): array
}

$withs[$relation] = $paginator instanceof Collection
? $paginator->map(fn(Model $item) => [
? $paginator->map(fn (Model $item) => [
'attributes' => $item->toArray(),
])
: $paginator;
Expand Down Expand Up @@ -610,10 +610,10 @@ public function index(RestifyRequest $request)

return $this->response([
'meta' => $this->resolveIndexMainMeta(
$request, $items->map(fn(self $repository) => $repository->resource), RepositoryCollection::meta($paginator->toArray())
$request, $items->map(fn (self $repository) => $repository->resource), RepositoryCollection::meta($paginator->toArray())
) ?? RepositoryCollection::meta($paginator->toArray()),
'links' => RepositoryCollection::paginationLinks($paginator->toArray()),
'data' => $items->map(fn(self $repository) => $repository->serializeForIndex($request)),
'data' => $items->map(fn (self $repository) => $repository->serializeForIndex($request)),
]);
}

Expand Down Expand Up @@ -653,9 +653,9 @@ public function store(RestifyRequest $request)
}

$this->hasOneFields($request)
->each(fn(HasOne $field) => $field->storeChild($request, $this->resource));
->each(fn (HasOne $field) => $field->storeChild($request, $this->resource));

$this->storeFields($request)->each(fn(Field $field) => $field->invokeAfter($request, $this->resource));
$this->storeFields($request)->each(fn (Field $field) => $field->invokeAfter($request, $this->resource));
});

static::stored($this->resource, $request);
Expand All @@ -679,7 +679,7 @@ public function storeBulk(RepositoryStoreBulkRequest $request)

$this->resource->save();

$this->storeBulkFields($request)->each(fn(Field $field) => $field->invokeAfter($request, $this->resource));
$this->storeBulkFields($request)->each(fn (Field $field) => $field->invokeAfter($request, $this->resource));

return $this->resource;
});
Expand All @@ -704,7 +704,7 @@ public function update(RestifyRequest $request, $repositoryId)
return $this->resource;
});

$this->updateFields($request)->each(fn(Field $field) => $field->invokeAfter($request, $this->resource));
$this->updateFields($request)->each(fn (Field $field) => $field->invokeAfter($request, $this->resource));

return $this->response()
->data($this->serializeForShow($request))
Expand All @@ -728,8 +728,8 @@ public function updateBulk(RestifyRequest $request, $repositoryId, int $row)
public function attach(RestifyRequest $request, $repositoryId, Collection $pivots)
{
DB::transaction(function () use ($request, $pivots) {
return $pivots->map(fn($pivot) => $pivot->forceFill($request->except($request->relatedRepository)))
->map(fn($pivot) => $pivot->save());
return $pivots->map(fn ($pivot) => $pivot->forceFill($request->except($request->relatedRepository)))
->map(fn ($pivot) => $pivot->save());
});

return $this->response()
Expand All @@ -740,7 +740,7 @@ public function attach(RestifyRequest $request, $repositoryId, Collection $pivot
public function detach(RestifyRequest $request, $repositoryId, Collection $pivots)
{
$deleted = DB::transaction(function () use ($pivots) {
return $pivots->map(fn($pivot) => $pivot->delete());
return $pivots->map(fn ($pivot) => $pivot->delete());
});

return $this->response()
Expand Down Expand Up @@ -772,18 +772,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 @@ -868,7 +868,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->getShowId($request)),
'id' => $this->when($this->resource->id, fn () => $this->getShowId($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 @@ -881,7 +881,7 @@ public function serializeForIndex(RestifyRequest $request): array
return $this->filter([
'id' => $this->when($id = $this->getShowId($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->resolveRelationships($request)), $related),
'meta' => $this->when(value($meta = $this->resolveIndexMeta($request)), $meta),
]);
Expand Down Expand Up @@ -931,12 +931,12 @@ protected static function fillBulkFields(RestifyRequest $request, Model $model,

public static function uriTo(Model $model)
{
return Restify::path() . '/' . static::uriKey() . '/' . $model->getKey();
return 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/Unit/BelongsToFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function test_field_will_be_returned_in_relations()
'user_id' => factory(User::class),
]);

$this->getJson('/restify-api/' . PostWithUserRepository::uriKey())
$this->getJson('/restify-api/'.PostWithUserRepository::uriKey())
->assertJsonStructure([
'data' => [
[
Expand All @@ -37,8 +37,8 @@ public function test_field_will_be_returned_in_relations()
'attributes',
],
],
]
]
],
],
]);
}

Expand All @@ -48,7 +48,7 @@ public function test_belongs_to_field_is_ignored_when_storing()
'user_id' => factory(User::class),
]);

$this->postJson('/restify-api/' . PostWithUserRepository::uriKey(), [
$this->postJson('/restify-api/'.PostWithUserRepository::uriKey(), [
'title' => 'New Post with user',
'user' => [
'name' => 'Eduard Lupacescu',
Expand Down
13 changes: 7 additions & 6 deletions tests/Unit/HasOneFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ public function test_has_one_will_be_returned_in_relations()
'user_id' => $user->id,
]);

$this->getJson('/restify-api/' . UserWithPostRepository::uriKey())
$this->getJson('/restify-api/'.UserWithPostRepository::uriKey())
->assertJsonStructure([
'data' => [
[
'attributes' => [
'name',
'post'
]
]
'post',
],
],
],
]);
}
Expand All @@ -47,7 +47,7 @@ public function test_has_one_field_is_saved_when_storing()
'user_id' => factory(User::class),
]);

$this->postJson('/restify-api/' . UserWithPostRepository::uriKey(), [
$this->postJson('/restify-api/'.UserWithPostRepository::uriKey(), [
'name' => 'Eduard Lupacescu',
'email' => 'eduard.lupacescu@binarcode.com',
'password' => 'secret!',
Expand Down Expand Up @@ -82,7 +82,8 @@ public static function uriKey()
}
}

class PostRepository extends Repository {
class PostRepository extends Repository
{
public static $model = Post::class;

public function fields(RestifyRequest $request)
Expand Down