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
3 changes: 1 addition & 2 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
use Illuminate\Support\Facades\Route;

// Global Search...
Route::get('/search', '\\' . GlobalSearchController::class);

Route::get('/search', '\\'.GlobalSearchController::class);

// API CRUD
Route::get('/{repository}', '\\'.RepositoryIndexController::class);
Expand Down
40 changes: 20 additions & 20 deletions src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,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 @@ -235,14 +235,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 @@ -342,10 +342,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 @@ -363,7 +363,7 @@ public function resolveShowAttributes(RestifyRequest $request)
return false;
}

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

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

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

Expand Down Expand Up @@ -451,7 +451,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 @@ -499,7 +499,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 @@ -522,7 +522,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 @@ -620,7 +620,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 @@ -633,7 +633,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 @@ -676,6 +676,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();
}
}
11 changes: 5 additions & 6 deletions src/Restify.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ public static function repositoriesFrom($directory)
$repositories = [];

foreach ((new Finder)->in($directory)->files() as $repository) {
$repository = $namespace . str_replace(
$repository = $namespace.str_replace(
['/', '.php'],
['\\', ''],
Str::after($repository->getPathname(), app_path() . DIRECTORY_SEPARATOR)
Str::after($repository->getPathname(), app_path().DIRECTORY_SEPARATOR)
);

if (is_subclass_of($repository, Repository::class) && (new ReflectionClass($repository))->isInstantiable()) {
Expand All @@ -122,7 +122,7 @@ public static function repositoriesFrom($directory)
public static function path($plus = null)
{
if (isset($plus)) {
return config('restify.base', '/restify-api') . '/' . $plus;
return config('restify.base', '/restify-api').'/'.$plus;
} else {
return config('restify.base', '/restify-api');
}
Expand Down Expand Up @@ -162,8 +162,8 @@ public static function exceptionHandler($callback)
public static function globallySearchableRepositories(RestifyRequest $request)
{
return collect(static::$repositories)
->filter(fn($repository) => $repository::authorizedToUseRepository($request))
->filter(fn($repository) => $repository::$globallySearchable)
->filter(fn ($repository) => $repository::authorizedToUseRepository($request))
->filter(fn ($repository) => $repository::$globallySearchable)
->sortBy(static::sortResourcesWith())
->all();
}
Expand All @@ -174,5 +174,4 @@ public static function sortResourcesWith()
return $resource::label();
};
}

}
1 change: 0 additions & 1 deletion src/Services/Search/GlobalSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,3 @@ protected function getSearchResults()
return collect($results)->sortKeys()->all();
}
}

2 changes: 1 addition & 1 deletion src/Services/Search/RepositorySearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,6 @@ public function setOrder($query, $param)

protected function applyIndexQuery(RestifyRequest $request, Repository $repository)
{
return fn($query) => $repository::indexQuery($request, $query);
return fn ($query) => $repository::indexQuery($request, $query);
}
}
2 changes: 0 additions & 2 deletions tests/Controllers/GlobalSearchControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public function test_global_search_returns_matches()
factory(User::class)->create(['name' => 'First user']);
factory(User::class)->create(['name' => 'Second user']);


$response = $this
->withoutExceptionHandling()
->getJson('/restify-api/search?search=Second');
Expand All @@ -39,7 +38,6 @@ public function test_global_search_filter_out_unauthorized_repositories()
factory(Post::class)->create();
factory(User::class)->create();


$response = $this
->withoutExceptionHandling()
->getJson('/restify-api/search?search=1');
Expand Down
4 changes: 2 additions & 2 deletions tests/Controllers/RepositoryIndexControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Binaryk\LaravelRestify\Tests\Controllers;

use Binaryk\LaravelRestify\Contracts\RestifySearchable;
use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostRepository;
use Binaryk\LaravelRestify\Tests\Fixtures\Post\Post;
use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostRepository;
use Binaryk\LaravelRestify\Tests\IntegrationTest;
use Illuminate\Foundation\Testing\RefreshDatabase;

Expand Down Expand Up @@ -124,7 +124,7 @@ public function test_index_unmergeable_repository_containes_only_explicitly_defi
'attributes' => [
'user_id',
'title',
'description'
'description',
],
]],
]);
Expand Down
6 changes: 3 additions & 3 deletions tests/Controllers/RepositoryShowControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function test_show_will_take_into_consideration_show_callback()
{
$_SERVER['postAuthorize.can.see.title'] = true;

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

$response = $this->getJson('/restify-api/post-authorizes/1');

Expand All @@ -59,7 +59,7 @@ public function test_show_will_take_into_consideration_show_callback()

public function test_show_unmergeable_repository_containes_only_explicitly_defined_fields()
{
factory(Post::class)->create(['title' => 'Eduard',]);
factory(Post::class)->create(['title' => 'Eduard']);

$response = $this->getJson('/restify-api/posts/1')
->assertJsonStructure([
Expand All @@ -78,7 +78,7 @@ public function test_show_unmergeable_repository_containes_only_explicitly_defin

public function test_show_mergeable_repository_containes_model_attributes_and_local_fields()
{
factory(Post::class)->create(['title' => 'Eduard',]);
factory(Post::class)->create(['title' => 'Eduard']);

$this->getJson('/restify-api/posts-mergeable/1')
->assertJsonStructure([
Expand Down
1 change: 0 additions & 1 deletion tests/Controllers/RepositoryUpdateControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,3 @@ public function test_will_not_update_readonly_fields()
$this->assertNull($r->json('data.attributes.image'));
}
}

5 changes: 2 additions & 3 deletions tests/Fixtures/Post/PostAuthorizeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ class PostAuthorizeRepository extends Repository

public static $globallySearchable = false;


public function fields(RestifyRequest $request)
{
return [
Field::new('user_id'),

Field::new('title')
->canSee(fn() => $_SERVER['postAuthorize.can.see.title'])
->showCallback(fn($value) => strtoupper($value)),
->canSee(fn () => $_SERVER['postAuthorize.can.see.title'])
->showCallback(fn ($value) => strtoupper($value)),

Field::new('description')->storingRules('required')->messages([
'required' => 'Description field is required',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function fields(RestifyRequest $request)
return [
Field::new('user_id'),

Field::new('title')->canUpdate(fn() => $_SERVER['posts.authorizable.title']),
Field::new('title')->canUpdate(fn () => $_SERVER['posts.authorizable.title']),
];
}
}