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 @@ -19,7 +19,7 @@ class Field extends OrganicField implements JsonSerializable
public $attribute;

/**
* Field value
* Field value.
*
* @var string|callable|null
*/
Expand Down Expand Up @@ -223,7 +223,7 @@ public function getUpdatingRules(): array
* @param string|null $attribute
* @return Field
*/
public function resolveForShow($repository, $attribute = null): Field
public function resolveForShow($repository, $attribute = null): self
{
$attribute = $attribute ?? $this->attribute;

Expand All @@ -234,7 +234,7 @@ public function resolveForShow($repository, $attribute = null): Field
return $this;
}

public function resolveForIndex($repository, $attribute = null): Field
public function resolveForIndex($repository, $attribute = null): self
{
$attribute = $attribute ?? $this->attribute;

Expand Down
1 change: 0 additions & 1 deletion src/Fields/OrganicField.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/
abstract class OrganicField extends BaseField
{

/**
* The callback used to authorize viewing the filter or action.
*
Expand Down
22 changes: 11 additions & 11 deletions src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,29 @@ abstract class Repository implements RestifySearchable, JsonSerializable
public $resource;

/**
* The list of relations available for the details or index
* The list of relations available for the details or index.
*
* e.g. ?with=users
* @var array
*/
public static array $related;

/**
* The list of searchable fields
* The list of searchable fields.
*
* @var array
*/
public static array $search;

/**
* The list of matchable fields
* The list of matchable fields.
*
* @var array
*/
public static array $match;

/**
* The list of fields to be sortable
* The list of fields to be sortable.
*
* @var array
*/
Expand Down Expand Up @@ -290,7 +290,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 @@ -335,7 +335,7 @@ public function serializeIndex(RestifyRequest $request): array
}),
'type' => $this->model()->getTable(),
'attributes' => $this->resolveIndexAttributes($request),
'relationships' => $this->when(!empty($relations), $relations),
'relationships' => $this->when(! empty($relations), $relations),
'meta' => $this->when(value($this->resolveDetailsMeta($request)), $this->resolveDetailsMeta($request)),
];
}
Expand All @@ -352,11 +352,11 @@ public function resolveIndexAttributes($request)

// Resolve the show method, and attach the value to the array
$this->collectFields($request)
->filter(fn(Field $field) => !$field->isHiddenOnIndex($request, static::class))
->filter(fn (Field $field) => ! $field->isHiddenOnIndex($request, static::class))
->each(function (Field $field) use (&$resolvedAttributes) {
$resolvedAttributes[$field->attribute] = $field->resolveForIndex($this)->value;
});
$hidden = $this->collectFields($request)->filter(fn(Field $field) => $field->isHiddenOnIndex($request, $this))->pluck('attribute')->toArray();
$hidden = $this->collectFields($request)->filter(fn (Field $field) => $field->isHiddenOnIndex($request, $this))->pluck('attribute')->toArray();

return Arr::except($resolvedAttributes, $hidden);
}
Expand Down Expand Up @@ -394,9 +394,9 @@ public function index(RestifyRequest $request)

$items = $paginator->getCollection()->map(function ($value) {
return static::resolveWith($value);
})->filter(function (Repository $repository) use ($request) {
})->filter(function (self $repository) use ($request) {
return $repository->authorizedToShow($request);
})->values()->map(fn(self $item) => $this->filter($item->serializeIndex($request)));
})->values()->map(fn (self $item) => $this->filter($item->serializeIndex($request)));

return $this->response([
'meta' => RepositoryCollection::meta($paginator->toArray()),
Expand Down Expand Up @@ -447,7 +447,7 @@ public function store(RestifyRequest $request)

return $this->response('', RestResponse::REST_RESPONSE_CREATED_CODE)
->model($this->resource)
->header('Location', Restify::path() . '/' . static::uriKey() . '/' . $this->resource->id);
->header('Location', Restify::path().'/'.static::uriKey().'/'.$this->resource->id);
}

public function update(RestifyRequest $request, $repositoryId)
Expand Down
6 changes: 3 additions & 3 deletions src/Services/Search/RepositorySearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function prepareMatchFields(RestifyRequest $request, $query, $extra = [])
{
$model = $query->getModel();
foreach ($this->repository->getMatchByFields() as $key => $type) {
if (!$request->has($key) && !data_get($extra, "match.$key")) {
if (! $request->has($key) && ! data_get($extra, "match.$key")) {
continue;
}

Expand Down Expand Up @@ -57,7 +57,7 @@ public function prepareMatchFields(RestifyRequest $request, $query, $extra = [])
case RestifySearchable::MATCH_INTEGER:
case 'number':
case 'int':
$query->where($field, '=', (int)$match);
$query->where($field, '=', (int) $match);
break;
}
}
Expand Down Expand Up @@ -122,7 +122,7 @@ public function prepareSearchFields(RestifyRequest $request, $query, $extra = []
$likeOperator = $connectionType == 'pgsql' ? 'ilike' : 'like';

foreach ($this->repository->getSearchableFields() as $column) {
$query->orWhere($model->qualifyColumn($column), $likeOperator, '%' . $search . '%');
$query->orWhere($model->qualifyColumn($column), $likeOperator, '%'.$search.'%');
}
});

Expand Down
8 changes: 4 additions & 4 deletions src/Services/Search/SearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SearchService extends Searchable
{
public function search(RestifyRequest $request, $model)
{
if (!$model instanceof RestifySearchable) {
if (! $model instanceof RestifySearchable) {
return $model->newQuery();
}

Expand All @@ -37,7 +37,7 @@ public function prepareMatchFields(RestifyRequest $request, $query, $extra = [])
$model = $query->getModel();
if ($model instanceof RestifySearchable) {
foreach ($model::getMatchByFields() as $key => $type) {
if (!$request->has($key) && !data_get($extra, "match.$key")) {
if (! $request->has($key) && ! data_get($extra, "match.$key")) {
continue;
}

Expand Down Expand Up @@ -70,7 +70,7 @@ public function prepareMatchFields(RestifyRequest $request, $query, $extra = [])
case RestifySearchable::MATCH_INTEGER:
case 'number':
case 'int':
$query->where($field, '=', (int)$match);
$query->where($field, '=', (int) $match);
break;
}
}
Expand Down Expand Up @@ -162,7 +162,7 @@ public function prepareSearchFields(RestifyRequest $request, $query, $extra = []
$likeOperator = $connectionType == 'pgsql' ? 'ilike' : 'like';

foreach ($model::getSearchableFields() as $column) {
$query->orWhere($model->qualifyColumn($column), $likeOperator, '%' . $search . '%');
$query->orWhere($model->qualifyColumn($column), $likeOperator, '%'.$search.'%');
}
});
}
Expand Down
12 changes: 5 additions & 7 deletions tests/Controllers/RepositoryIndexControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use Binaryk\LaravelRestify\Contracts\RestifySearchable;
use Binaryk\LaravelRestify\Tests\Fixtures\Apple;
use Binaryk\LaravelRestify\Tests\Fixtures\AppleRepository;
use Binaryk\LaravelRestify\Tests\Fixtures\Post;
use Binaryk\LaravelRestify\Tests\Fixtures\UserRepository;
use Binaryk\LaravelRestify\Tests\IntegrationTest;
use Illuminate\Foundation\Testing\RefreshDatabase;

Expand Down Expand Up @@ -80,12 +78,12 @@ public function test_repository_filter_works()
public function test_repository_order()
{
AppleRepository::$sort = [
'title'
'title',
];

factory(Apple::class)->create(['title' => 'aaa',]);
factory(Apple::class)->create(['title' => 'aaa']);

factory(Apple::class)->create(['title' => 'zzz',]);
factory(Apple::class)->create(['title' => 'zzz']);

$response = $this
->getJson('restify-api/apples?sort=-title')
Expand All @@ -104,11 +102,11 @@ public function test_repository_order()

public function test_repsitory_with_relations()
{
AppleRepository::$related = ['user',];
AppleRepository::$related = ['user'];

$user = $this->mockUsers(1)->first();

factory(Apple::class)->create(['user_id' => $user->id,]);
factory(Apple::class)->create(['user_id' => $user->id]);

$response = $this->getJson('/restify-api/apples?related=user')
->assertStatus(200);
Expand Down
2 changes: 1 addition & 1 deletion tests/Factories/AppleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@

$factory->define(Apple::class, function (Faker $faker) {
return [
'title' => $faker->text(50)
'title' => $faker->text(50),
];
});
4 changes: 2 additions & 2 deletions tests/Fixtures/Apple.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class Apple extends Model

protected $fillable = [
'title',
'user_id'
'user_id',
];

public function user()
{
return $this->belongsTo(User::class);
return $this->belongsTo(User::class);
}

public function toArray()
Expand Down