-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathSelectFilterTrait.php
More file actions
96 lines (76 loc) · 2.11 KB
/
Copy pathSelectFilterTrait.php
File metadata and controls
96 lines (76 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
namespace Code16\Sharp\Filters;
use Code16\Sharp\Enums\FilterType;
/**
* @internal
*/
trait SelectFilterTrait
{
private bool $isMaster = false;
private bool $isSearchable = false;
private array $searchKeys = ['label'];
private ?array $cachedValues = null;
final public function isMaster(): bool
{
return $this->isMaster;
}
final public function isSearchable(): bool
{
return $this->isSearchable;
}
final public function getSearchKeys(): array
{
return $this->searchKeys;
}
final public function configureSearchable(bool $isSearchable = true): self
{
$this->isSearchable = $isSearchable;
return $this;
}
final public function configureSearchKeys(array $searchKeys): self
{
$this->searchKeys = $searchKeys;
return $this;
}
final public function configureMaster(bool $isMaster = true): self
{
$this->isMaster = $isMaster;
return $this;
}
public function fromQueryParam($value): mixed
{
return $value;
}
public function toQueryParam($value): mixed
{
return $value;
}
public function toArray(): array
{
return parent::buildArray([
'type' => FilterType::Select->value,
'multiple' => $this instanceof SelectMultipleFilter,
'required' => $this instanceof SelectRequiredFilter,
'values' => $this->formattedValues(),
'master' => $this->isMaster(),
'searchable' => $this->isSearchable(),
'searchKeys' => $this->getSearchKeys(),
]);
}
protected function formattedValues(): array
{
$values = $this->cachedValues();
if (! is_array(collect($values)->first())) {
return collect($values)
->map(fn ($label, $id) => compact('id', 'label'))
->values()
->all();
}
return $values;
}
public function cachedValues(): array
{
return $this->cachedValues ??= $this->values();
}
abstract public function values(): array;
}