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/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public function resolveForShow($repository, $attribute = null)
return;
}

if (!$this->showCallback) {
if (! $this->showCallback) {
$this->resolve($repository, $attribute);
} elseif (is_callable($this->showCallback)) {
tap($this->value ?? $this->resolveAttribute($repository, $attribute), function ($value) use ($repository, $attribute) {
Expand All @@ -319,7 +319,7 @@ public function resolveForIndex($repository, $attribute = null)
return;
}

if (!$this->indexCallback) {
if (! $this->indexCallback) {
$this->resolve($repository, $attribute);
} elseif (is_callable($this->indexCallback)) {
tap($this->value ?? $this->resolveAttribute($repository, $attribute), function ($value) use ($repository, $attribute) {
Expand All @@ -339,7 +339,7 @@ public function resolve($repository, $attribute = null)
return;
}

if (!$this->resolveCallback) {
if (! $this->resolveCallback) {
$this->value = $this->resolveAttribute($repository, $attribute);
} elseif (is_callable($this->resolveCallback)) {
tap($this->resolveAttribute($repository, $attribute), function ($value) use ($repository, $attribute) {
Expand Down
14 changes: 7 additions & 7 deletions src/Fields/OrganicField.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ public function showOnIndex($callback = true)
public function hideFromShow($callback = true)
{
$this->showOnShow = is_callable($callback) ? function () use ($callback) {
return !call_user_func_array($callback, func_get_args());
return ! call_user_func_array($callback, func_get_args());
}
: !$callback;
: ! $callback;

return $this;
}

public function hideFromIndex($callback = true)
{
$this->showOnIndex = is_callable($callback) ? function () use ($callback) {
return !call_user_func_array($callback, func_get_args());
return ! call_user_func_array($callback, func_get_args());
}
: !$callback;
: ! $callback;

return $this;
}
Expand Down Expand Up @@ -87,7 +87,7 @@ public function isHiddenOnIndex(RestifyRequest $request, $repository): bool
$this->showOnIndex = call_user_func($this->showOnIndex, $request, $repository);
}

return !$this->showOnIndex;
return ! $this->showOnIndex;
}

public function authorize(Request $request)
Expand Down Expand Up @@ -151,11 +151,11 @@ public function isReadonly(RestifyRequest $request)

public function isShownOnUpdate(RestifyRequest $request, $repository): bool
{
return !$this->isReadonly($request);
return ! $this->isReadonly($request);
}

public function isShownOnStore(RestifyRequest $request, $repository): bool
{
return !$this->isReadonly($request);
return ! $this->isReadonly($request);
}
}
42 changes: 18 additions & 24 deletions src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@
use Binaryk\LaravelRestify\Contracts\RestifySearchable;
use Binaryk\LaravelRestify\Controllers\RestResponse;
use Binaryk\LaravelRestify\Exceptions\InstanceOfException;
use Binaryk\LaravelRestify\Exceptions\UnauthorizedException;
use Binaryk\LaravelRestify\Fields\Field;
use Binaryk\LaravelRestify\Fields\FieldCollection;
use Binaryk\LaravelRestify\Http\Requests\RepositoryDestroyRequest;
use Binaryk\LaravelRestify\Http\Requests\RepositoryStoreRequest;
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
use Binaryk\LaravelRestify\Restify;
use Binaryk\LaravelRestify\Services\Search\RepositorySearchService;
use Binaryk\LaravelRestify\Traits\InteractWithSearch;
use Binaryk\LaravelRestify\Traits\PerformsQueries;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -27,7 +24,6 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use JsonSerializable;

/**
Expand Down Expand Up @@ -183,14 +179,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 @@ -290,10 +286,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 @@ -311,7 +307,7 @@ public function resolveShowAttributes(RestifyRequest $request)
return false;
}

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

Expand All @@ -332,10 +328,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 @@ -353,7 +349,7 @@ public function resolveIndexAttributes($request)
return false;
}

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

Expand Down Expand Up @@ -399,7 +395,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 @@ -449,7 +445,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 All @@ -470,7 +466,7 @@ public function store(RestifyRequest $request)
$request, $this->resource, $this->storeFields($request)
);

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

$this->resource->save();
});
Expand Down Expand Up @@ -584,7 +580,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 @@ -597,7 +593,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 @@ -638,10 +634,8 @@ 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();
}
}
6 changes: 3 additions & 3 deletions tests/Controllers/RepositoryUpdateControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function test_will_not_update_readonly_fields()
{
$user = $this->mockUsers()->first();

$post = factory(Post::class)->create(['image' => null,]);
$post = factory(Post::class)->create(['image' => null]);

$r = $this->putJson('/restify-api/posts-unauthorized-fields/'.$post->id, [
'user_id' => $user->id,
Expand Down Expand Up @@ -155,9 +155,9 @@ class AppleUpdateMergeable extends Repository implements Mergeable
public function fields(RestifyRequest $request)
{
return [
Field::make('title')->canUpdate(fn($value) => true),
Field::make('title')->canUpdate(fn ($value) => true),

Field::make('user_id')->canUpdate(fn($value) => true),
Field::make('user_id')->canUpdate(fn ($value) => true),
];
}
}
3 changes: 1 addition & 2 deletions tests/Fixtures/PostUnauthorizedFieldRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public function fields(RestifyRequest $request)

Field::new('title'),


Field::new('description')->canStore(fn() => $_SERVER['posts.description.authorized'] ?? false)
Field::new('description')->canStore(fn () => $_SERVER['posts.description.authorized'] ?? false),
];
}
}