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/Http/Requests/InteractWithRepositories.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function repository($key = null): ?Repository
]), 404);
}

if (!$repository::authorizedToUseRepository($this)) {
if (! $repository::authorizedToUseRepository($this)) {
throw new UnauthorizedException(__('Unauthorized to view repository :name. See "allowRestify" policy.', [
'name' => $repository,
]), 403);
Expand Down Expand Up @@ -114,7 +114,7 @@ public function newRepositoryWith($model, $uriKey = null)
*/
public function newQueryWithoutScopes($uriKey = null)
{
if (!$this->isViaRepository()) {
if (! $this->isViaRepository()) {
return $this->model($uriKey)->newQueryWithoutScopes();
}

Expand Down
44 changes: 21 additions & 23 deletions src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
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\Http\Request;
use Illuminate\Http\Resources\ConditionallyLoadsAttributes;
Expand Down Expand Up @@ -181,7 +180,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 @@ -227,8 +226,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 @@ -239,14 +238,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 Down Expand Up @@ -346,10 +345,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 @@ -367,7 +366,7 @@ public function resolveShowAttributes(RestifyRequest $request)
return false;
}

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

Expand All @@ -388,10 +387,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 @@ -409,7 +408,7 @@ public function resolveIndexAttributes($request)
return false;
}

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

Expand Down Expand Up @@ -455,7 +454,7 @@ public function resolveRelationships($request): array
/** * @var AbstractPaginator $paginator */
$paginator = $this->resource->{$relation}()->paginate($request->get('relatablePerPage') ?? (static::$defaultRelatablePerPage ?? RestifySearchable::DEFAULT_RELATABLE_PER_PAGE));

$withs[$relation] = $paginator->getCollection()->map(fn(Model $item) => [
$withs[$relation] = $paginator->getCollection()->map(fn (Model $item) => [
'attributes' => $item->toArray(),
]);
}
Expand Down Expand Up @@ -507,7 +506,7 @@ public function index(RestifyRequest $request)
return static::resolveWith($value);
})->filter(function (self $repository) use ($request) {
return $repository->authorizedToShow($request);
})->values()->map(fn(self $repository) => $repository->serializeForIndex($request));
})->values()->map(fn (self $repository) => $repository->serializeForIndex($request));

return $this->response([
'meta' => RepositoryCollection::meta($paginator->toArray()),
Expand Down Expand Up @@ -535,8 +534,7 @@ public function store(RestifyRequest $request)
$this->resource->save();
}


$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 Down Expand Up @@ -634,7 +632,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 @@ -647,7 +645,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 @@ -690,6 +688,6 @@ protected static function fillFields(RestifyRequest $request, Model $model, Coll

public static function uriTo(Model $model)
{
return Restify::path() . '/' . static::uriKey() . '/' . $model->getKey();
return Restify::path().'/'.static::uriKey().'/'.$model->getKey();
}
}
1 change: 0 additions & 1 deletion src/Services/Search/RepositorySearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Binaryk\LaravelRestify\Contracts\RestifySearchable;
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
use Binaryk\LaravelRestify\Repositories\Repository;
use Illuminate\Database\Eloquent\Builder;

class RepositorySearchService extends Searchable
{
Expand Down
12 changes: 6 additions & 6 deletions tests/Controllers/RelatedIndexControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ public function test_can_update_post_belongs_to_a_user()
factory(User::class)->create();
factory(User::class)->create();

factory(Post::class)->create(['title' => 'Post title', 'user_id' => 1,]);
factory(Post::class)->create(['title' => 'Post title', 'user_id' => 1]);

factory(Post::class)->create(['title' => 'Post title', 'user_id' => 2,]);
factory(Post::class)->create(['title' => 'Post title', 'user_id' => 2]);

$response = $this->putJson('restify-api/posts/1?viaRepository=users&viaRepositoryId=1&viaRelationship=posts', [
'title' => 'Post updated title',
Expand All @@ -95,9 +95,9 @@ public function test_can_destroy_post_belongs_to_a_user()
factory(User::class)->create();
factory(User::class)->create();

factory(Post::class)->create(['title' => 'Post title', 'user_id' => 1,]);
factory(Post::class)->create(['title' => 'Post title', 'user_id' => 1]);

factory(Post::class)->create(['title' => 'Post title', 'user_id' => 2,]);
factory(Post::class)->create(['title' => 'Post title', 'user_id' => 2]);

$this->deleteJson('restify-api/posts/1?viaRepository=users&viaRepositoryId=1&viaRelationship=posts')->assertStatus(204);

Expand All @@ -114,9 +114,9 @@ public function test_policy_check_before_destroy_post_belongs_to_a_user()

factory(User::class)->create();

factory(Post::class)->create(['title' => 'Post title', 'user_id' => 1,]);
factory(Post::class)->create(['title' => 'Post title', 'user_id' => 1]);

factory(Post::class)->create(['title' => 'Post title', 'user_id' => 2,]);
factory(Post::class)->create(['title' => 'Post title', 'user_id' => 2]);

$this->deleteJson('restify-api/posts/1?viaRepository=users&viaRepositoryId=1&viaRelationship=posts')->assertStatus(403);

Expand Down