Repository navigation
Expand file tree
/
Copy pathSearchTypeGuard.php
More file actions
56 lines (49 loc) · 1.83 KB
/
Copy pathSearchTypeGuard.php
File metadata and controls
56 lines (49 loc) · 1.83 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
<?php
declare(strict_types=1);
namespace NyonCode\WireTable\Support;
use NyonCode\WireTable\Columns\Column;
use NyonCode\WireTable\Exceptions\TableConfigurationException;
use NyonCode\WireTable\Table;
/**
* Refuses a search value type the table's search box can never ask for.
*
* `searchAs('numeric'|'date'|'code')` only says which comparisons a column can
* answer — it does not switch comparisons on. Without
* `->search(fn (SearchConfig $s) => $s->ranges())` the term stays one literal
* substring, so `8866 01..08` is looked for verbatim and the table comes back
* empty with nothing on screen to explain why.
*
* That is a configuration mistake with exactly one reading, so it is raised as
* one rather than left to be discovered as a missing row: the declaration and
* the missing switch are named together, at render time, before anything is
* typed.
*
* Declaring `'text'` is exempt — it asserts no comparison and therefore needs
* nothing turned on.
*/
final class SearchTypeGuard
{
/**
* @param array<int, Column> $columns
*
* @throws TableConfigurationException
*/
public static function assert(Table $table, array $columns): void
{
// A table nobody can search into has no search box to configure; the
// declaration is dormant rather than contradicted.
if (! $table->isSearchable() || $table->getSearchConfig()->parsesRanges()) {
return;
}
foreach ($columns as $column) {
$declared = $column->getSearchValueType();
if ($declared === null || ! $column->isSearchable() || ! $declared->supportsComparison()) {
continue;
}
throw TableConfigurationException::searchValueTypeNeedsRanges(
$column->getName(),
$declared,
);
}
}
}