Skip to content

Commit a80dcaa

Browse files
aguinganddvlpp
andauthored
Allow default filter value for all filters (#660)
* Allow default filter value for all filters * cleanup * Add unit test --------- Co-authored-by: philippe <[email protected]>
1 parent cb1aa9f commit a80dcaa

15 files changed

+412
-281
lines changed

src/EntityList/Filters/HiddenFilter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,9 @@ public function toArray(): array
2727
{
2828
return [];
2929
}
30+
31+
public function defaultValue(): mixed
32+
{
33+
return null;
34+
}
3035
}

src/Filters/AutocompleteRemoteFilter.php

Lines changed: 2 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2,90 +2,12 @@
22

33
namespace Code16\Sharp\Filters;
44

5-
use Code16\Sharp\Enums\FilterType;
6-
75
abstract class AutocompleteRemoteFilter extends Filter
86
{
9-
private bool $isMaster = false;
10-
private int $debounceDelay = 300;
11-
private int $searchMinChars = 1;
12-
private array $cache = [];
13-
14-
final public function configureMaster(bool $isMaster = true): self
15-
{
16-
$this->isMaster = $isMaster;
17-
18-
return $this;
19-
}
7+
use AutocompleteRemoteFilterTrait;
208

21-
final public function configureDebounceDelay(int $delay): self
9+
public function defaultValue(): mixed
2210
{
23-
$this->debounceDelay = $delay;
24-
25-
return $this;
26-
}
27-
28-
final public function configureSearchMinChars(int $minChars): self
29-
{
30-
$this->searchMinChars = $minChars;
31-
32-
return $this;
33-
}
34-
35-
public function fromQueryParam($value): mixed
36-
{
37-
if ($value) {
38-
if ($this->cache[$value] ?? null) {
39-
return $this->cache[$value];
40-
}
41-
if ($label = $this->valueLabelFor($value)) {
42-
return $this->cache[$value] = [
43-
'id' => $value,
44-
'label' => $label,
45-
];
46-
}
47-
}
48-
4911
return null;
5012
}
51-
52-
public function toQueryParam($value): mixed
53-
{
54-
return is_array($value) ? $value['id'] : $value;
55-
}
56-
57-
public function toArray(): array
58-
{
59-
return parent::buildArray([
60-
'type' => FilterType::AutocompleteRemote->value,
61-
'master' => $this->isMaster,
62-
'required' => $this instanceof AutocompleteRemoteRequiredFilter,
63-
// 'multiple' => $this instanceof AutocompleteRemoteMultipleFilter,
64-
'debounceDelay' => $this->debounceDelay,
65-
'searchMinChars' => $this->searchMinChars,
66-
]);
67-
}
68-
69-
final public function format(array $values)
70-
{
71-
if (! is_array(collect($values)->first())) {
72-
return collect($values)
73-
->map(function ($label, $id) {
74-
return compact('id', 'label');
75-
})
76-
->values()
77-
->all();
78-
}
79-
80-
return $values;
81-
}
82-
83-
public function formatRawValue(mixed $value): mixed
84-
{
85-
return $value ? $value['id'] : null;
86-
}
87-
88-
abstract public function values(string $query): array;
89-
90-
abstract public function valueLabelFor(string $id): ?string;
9113
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
namespace Code16\Sharp\Filters;
4+
5+
use Code16\Sharp\Enums\FilterType;
6+
7+
/**
8+
* @internal
9+
*/
10+
trait AutocompleteRemoteFilterTrait
11+
{
12+
private bool $isMaster = false;
13+
private int $debounceDelay = 300;
14+
private int $searchMinChars = 1;
15+
private array $cache = [];
16+
17+
final public function configureMaster(bool $isMaster = true): self
18+
{
19+
$this->isMaster = $isMaster;
20+
21+
return $this;
22+
}
23+
24+
final public function configureDebounceDelay(int $delay): self
25+
{
26+
$this->debounceDelay = $delay;
27+
28+
return $this;
29+
}
30+
31+
final public function configureSearchMinChars(int $minChars): self
32+
{
33+
$this->searchMinChars = $minChars;
34+
35+
return $this;
36+
}
37+
38+
public function fromQueryParam($value): mixed
39+
{
40+
if ($value) {
41+
if ($this->cache[$value] ?? null) {
42+
return $this->cache[$value];
43+
}
44+
if ($label = $this->valueLabelFor($value)) {
45+
return $this->cache[$value] = [
46+
'id' => $value,
47+
'label' => $label,
48+
];
49+
}
50+
}
51+
52+
return null;
53+
}
54+
55+
public function toQueryParam($value): mixed
56+
{
57+
return is_array($value) ? $value['id'] : $value;
58+
}
59+
60+
public function defaultValue(): mixed
61+
{
62+
return null;
63+
}
64+
65+
public function toArray(): array
66+
{
67+
return parent::buildArray([
68+
'type' => FilterType::AutocompleteRemote->value,
69+
'master' => $this->isMaster,
70+
'required' => $this instanceof AutocompleteRemoteRequiredFilter,
71+
// 'multiple' => $this instanceof AutocompleteRemoteMultipleFilter,
72+
'debounceDelay' => $this->debounceDelay,
73+
'searchMinChars' => $this->searchMinChars,
74+
]);
75+
}
76+
77+
final public function format(array $values)
78+
{
79+
if (! is_array(collect($values)->first())) {
80+
return collect($values)
81+
->map(function ($label, $id) {
82+
return compact('id', 'label');
83+
})
84+
->values()
85+
->all();
86+
}
87+
88+
return $values;
89+
}
90+
91+
public function formatRawValue(mixed $value): mixed
92+
{
93+
return $value ? $value['id'] : null;
94+
}
95+
96+
abstract public function values(string $query): array;
97+
98+
abstract public function valueLabelFor(string $id): ?string;
99+
}

src/Filters/AutocompleteRemoteRequiredFilter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Code16\Sharp\Filters;
44

5-
abstract class AutocompleteRemoteRequiredFilter extends AutocompleteRemoteFilter
5+
abstract class AutocompleteRemoteRequiredFilter extends Filter
66
{
7+
use AutocompleteRemoteFilterTrait;
8+
79
abstract public function defaultValue(): mixed;
810
}

src/Filters/CheckFilter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public function toQueryParam($value): mixed
1616
return $value;
1717
}
1818

19+
public function defaultValue(): bool
20+
{
21+
return false;
22+
}
23+
1924
public function toArray(): array
2025
{
2126
return parent::buildArray([

src/Filters/DateRangeFilter.php

Lines changed: 3 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -2,119 +2,12 @@
22

33
namespace Code16\Sharp\Filters;
44

5-
use Carbon\Carbon;
6-
use Code16\Sharp\Enums\FilterType;
7-
use Code16\Sharp\Filters\DateRange\DateRangeFilterValue;
8-
use Code16\Sharp\Filters\DateRange\DateRangePreset;
9-
105
abstract class DateRangeFilter extends Filter
116
{
12-
private string $dateFormat = 'YYYY-MM-DD';
13-
private bool $isMondayFirst = true;
14-
15-
/** @var DateRangePreset[] */
16-
private ?array $presets = null;
17-
18-
final public function configureDateFormat(string $dateFormat): self
19-
{
20-
$this->dateFormat = $dateFormat;
21-
22-
return $this;
23-
}
24-
25-
final public function configureMondayFirst(bool $isMondayFirst = true): self
26-
{
27-
$this->isMondayFirst = $isMondayFirst;
28-
29-
return $this;
30-
}
31-
32-
final public function configureShowPresets(bool $showPresets = true, ?array $presets = null): self
33-
{
34-
if ($showPresets) {
35-
$this->presets = $presets !== null
36-
? $presets
37-
: [
38-
DateRangePreset::today(),
39-
DateRangePreset::yesterday(),
40-
DateRangePreset::last7days(),
41-
DateRangePreset::last30days(),
42-
DateRangePreset::last365days(),
43-
DateRangePreset::thisMonth(),
44-
DateRangePreset::lastMonth(),
45-
DateRangePreset::thisYear(),
46-
DateRangePreset::lastYear(),
47-
];
48-
} else {
49-
$this->presets = [];
50-
}
51-
52-
return $this;
53-
}
54-
55-
final public function getDateFormat(): string
56-
{
57-
return $this->dateFormat;
58-
}
59-
60-
final public function isMondayFirst(): bool
61-
{
62-
return $this->isMondayFirst;
63-
}
64-
65-
/**
66-
* @internal
67-
*/
68-
final public function fromQueryParam($value): ?array
69-
{
70-
if (! $value) {
71-
return null;
72-
}
73-
74-
[$start, $end] = explode('..', $value);
75-
$start = Carbon::createFromFormat('Ymd', $start)->startOfDay();
76-
$end = Carbon::createFromFormat('Ymd', $end)->endOfDay();
77-
78-
return [
79-
'start' => $start->format('Y-m-d'),
80-
'end' => $end->format('Y-m-d'),
81-
'formatted' => [
82-
'start' => $start->isoFormat($this->getDateFormat()),
83-
'end' => $end->isoFormat($this->getDateFormat()),
84-
],
85-
];
86-
}
87-
88-
/**
89-
* @internal
90-
*/
91-
final public function toQueryParam($value): ?string
92-
{
93-
if (! $value) {
94-
return null;
95-
}
96-
97-
return sprintf(
98-
'%s..%s',
99-
Carbon::parse($value['start'])->format('Ymd'),
100-
Carbon::parse($value['end'])->format('Ymd'),
101-
);
102-
}
103-
104-
public function toArray(): array
105-
{
106-
return parent::buildArray([
107-
'type' => FilterType::DateRange->value,
108-
'required' => $this instanceof DateRangeRequiredFilter,
109-
'mondayFirst' => $this->isMondayFirst(),
110-
'presets' => collect($this->presets)
111-
->map(fn (DateRangePreset $preset) => $preset->toArray())
112-
->toArray(),
113-
]);
114-
}
7+
use DateRangeFilterTrait;
1158

116-
public function formatRawValue(mixed $value): DateRangeFilterValue
9+
public function defaultValue(): mixed
11710
{
118-
return new DateRangeFilterValue(Carbon::parse($value['start']), Carbon::parse($value['end']));
11+
return null;
11912
}
12013
}

0 commit comments

Comments
 (0)