Skip to content

Commit

Permalink
Merge pull request #90 from abbasudo/analysis-e0WMr9
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
abbasudo authored Oct 13, 2024
2 parents 62e096d + 1ac8f52 commit 5afa55a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 16 deletions.
21 changes: 9 additions & 12 deletions src/Filters/Resolve.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Resolve
public function __construct(FilterList $filterList, Model $model)
{
$this->filterList = $filterList;
$this->model = $model;
$this->model = $model;
}

/**
Expand All @@ -46,10 +46,9 @@ public function __construct(FilterList $filterList, Model $model)
* @param array|string $values
*
* @throws Exception
*
* @return void
* @throws Exception
*
* @return void
*/
public function apply(Builder $query, string $field, array|string $values): void
{
Expand All @@ -66,10 +65,9 @@ public function apply(Builder $query, string $field, array|string $values): void
* @param Closure $closure
*
* @throws Exception
*
* @return bool
* @throws Exception
*
* @return bool
*/
private function safe(Closure $closure): bool
{
Expand Down Expand Up @@ -110,10 +108,9 @@ private function validate(array|string $values = [])
* @param array|string|null $filters
*
* @throws Exception
*
* @return void
* @throws Exception
*
* @return void
*/
private function filter(Builder $query, string $field, array|string|null $filters): void
{
Expand All @@ -123,17 +120,17 @@ private function filter(Builder $query, string $field, array|string|null $filter
// Resolve the filter using the appropriate strategy
if ($this->filterList->get($field) !== null) {
//call apply method of the appropriate filter class
$this->safe(fn() => $this->applyFilterStrategy($query, $field, $filters));
$this->safe(fn () => $this->applyFilterStrategy($query, $field, $filters));
} else {
// If the field is not recognized as a filter strategy, it is treated as a relation
$this->safe(fn() => $this->applyRelationFilter($query, $field, $filters));
$this->safe(fn () => $this->applyRelationFilter($query, $field, $filters));
}
}

/**
* @param Builder $query
* @param string $operator
* @param array $filters
* @param string $operator
* @param array $filters
*
* @return void
*/
Expand Down Expand Up @@ -190,7 +187,7 @@ private function relation(Builder $query, Closure $callback)
{
// remove the last field until its empty
$field = array_shift($this->fields);
$query->whereHas($field, fn($subQuery) => $this->applyRelations($subQuery, $callback));
$query->whereHas($field, fn ($subQuery) => $this->applyRelations($subQuery, $callback));
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Traits/Filterable.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ private function bootFilter(): void

app()->bind(Resolve::class, function () {
$resolver = $this->getFilterResolver();

return new $resolver(app(FilterList::class), $this);
});
}
Expand Down
2 changes: 1 addition & 1 deletion tests/App/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Post extends Model

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

public function comments(): HasMany
Expand Down
5 changes: 2 additions & 3 deletions tests/Feature/RelationFilterTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

use Abbasudo\Purity\Tests\App\Models\Book;
use Abbasudo\Purity\Tests\App\Models\Post;
use Abbasudo\Purity\Tests\App\Models\Product;
use Abbasudo\Purity\Tests\App\Models\User;
Expand Down Expand Up @@ -87,12 +86,12 @@ public function it_can_filter_by_has_one_relation(): void

$product->book()->create([
'name' => 'book',
'description' => 'book for product'
'description' => 'book for product',
]);

$response = $this->getJson('/products?filters[book][name][$eq]=book');

$response->assertOk();
$response->assertJsonCount(1);
}
}
}

0 comments on commit 5afa55a

Please sign in to comment.