Skip to content

Commit f62d60f

Browse files
authored
feat: adding filter placeholder (#531)
* feat: adding filter placeholder * Fix styling * fix: tests * fix: wip * Fix styling * fix: psalm * fix: adding test * Fix styling * fix: psalm Co-authored-by: binaryk <[email protected]>
1 parent d76fb58 commit f62d60f

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

src/Filters/Filter.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ abstract class Filter implements JsonSerializable
2727

2828
public string $description = '';
2929

30+
public string $placeholder = '';
31+
3032
public ?string $column = null;
3133

3234
public ?Closure $canSeeCallback = null;
@@ -227,6 +229,7 @@ public function jsonSerialize()
227229
'advanced' => $this->advanced,
228230
'title' => $this->title(),
229231
'description' => $this->description(),
232+
'placeholder' => $this->placeholder(),
230233
'column' => $this->column(),
231234
'key' => static::uriKey(),
232235
], function (array $initial) {
@@ -282,4 +285,23 @@ protected function description(): string
282285
{
283286
return $this->description;
284287
}
288+
289+
protected function placeholder(): string
290+
{
291+
return $this->placeholder;
292+
}
293+
294+
public function setPlaceholder(string $placeholder): self
295+
{
296+
$this->placeholder = $placeholder;
297+
298+
return $this;
299+
}
300+
301+
public function setDescription(string $description): self
302+
{
303+
$this->description = $description;
304+
305+
return $this;
306+
}
285307
}

tests/Controllers/RepositoryFilterControllerTest.php

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Binaryk\LaravelRestify\Tests\Controllers;
44

5+
use Binaryk\LaravelRestify\Filters\MatchFilter;
56
use Binaryk\LaravelRestify\Filters\SortableFilter;
67
use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostRepository;
78
use Binaryk\LaravelRestify\Tests\IntegrationTest;
@@ -34,14 +35,14 @@ public function test_available_filters_contains_matches_sortables_searches(): vo
3435
// 2 searchable
3536
->assertJson(
3637
fn (AssertableJson $json) => $json
37-
->where('data.0.rules.is_active', 'bool')
38-
->where('data.4.type', 'text')
39-
->where('data.4.column', 'title')
40-
->where('data.5.type', 'value')
41-
->where('data.5.column', 'title')
42-
->where('data.6.type', 'value')
43-
->where('data.6.column', 'id')
44-
->etc()
38+
->where('data.0.rules.is_active', 'bool')
39+
->where('data.4.type', 'text')
40+
->where('data.4.column', 'title')
41+
->where('data.5.type', 'value')
42+
->where('data.5.column', 'title')
43+
->where('data.6.type', 'value')
44+
->where('data.6.column', 'id')
45+
->etc()
4546
)
4647
->assertJsonCount(8, 'data');
4748
}
@@ -73,4 +74,26 @@ public function test_available_filters_returns_only_matches_sortables_searches()
7374
$this->getJson(PostRepository::route('filters', query: ['only' => 'searchables']))
7475
->assertJsonCount(2, 'data');
7576
}
77+
78+
public function test_filters_will_render_placeholder(): void
79+
{
80+
PostRepository::$match = [
81+
'title' => MatchFilter::make()
82+
->setDescription('Sort by title')
83+
->setPlaceholder('-title')
84+
->setType('string'),
85+
];
86+
87+
$this->getJson(PostRepository::route('filters', query: [
88+
'only' => 'matches',
89+
]))
90+
->assertJson(function (AssertableJson $json) {
91+
$json
92+
->where('data.0.placeholder', '-title')
93+
->where('data.0.description', 'Sort by title')
94+
->where('data.0.type', 'string')
95+
->where('data.0.column', 'title')
96+
->etc();
97+
});
98+
}
7699
}

tests/IntegrationTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Binaryk\LaravelRestify\Tests\Fixtures\User\UserRepository;
3030
use Illuminate\Contracts\Auth\Authenticatable;
3131
use Illuminate\Database\Eloquent\Factories\Factory;
32+
use Illuminate\Routing\Middleware\ThrottleRequests;
3233
use Illuminate\Support\Facades\Gate;
3334
use JetBrains\PhpStorm\Pure;
3435
use Mockery;
@@ -45,6 +46,8 @@ protected function setUp(): void
4546
{
4647
parent::setUp();
4748

49+
$this->withoutMiddleware(ThrottleRequests::class);
50+
4851
$this
4952
->repositories()
5053
->policies()

0 commit comments

Comments
 (0)