Skip to content

Commit 25ababe

Browse files
authored
Implemented column definition validation (#56)
1 parent 9e7bcfa commit 25ababe

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

krait/src/Tables/BaseTable.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,27 +81,39 @@ protected function shouldRefresh(): bool
8181
* Adds a column to the table
8282
*
8383
* @param string $name - The columns name
84-
* @param string $label - The columns label
84+
* @param ?string $label - The columns label (using the name by default)
8585
* @param bool $hideLabel - Flags if the label should be visible in the header.
8686
* @param bool $datetime - Flags if the column contains datetime object.
8787
* @param bool $sortable - Flags if the column is sortable.
8888
* @param bool $fixed - Flags if the column is resizable.
8989
* @param string|null $classes - Additional classes that will be added on FE.
9090
* @param callable|null $process - The column result generation callback.
9191
* @param callable|null $sort - The column sorting callback.
92+
*
93+
* @throws Exception
9294
*/
9395
protected function column(
9496
string $name,
95-
string $label,
97+
?string $label = null,
9698
bool $hideLabel = false,
9799
bool $datetime = false,
98-
?string $dateformat = null,
100+
?string $dateFormat = null,
99101
bool $sortable = true,
100102
bool $fixed = false,
101103
?string $classes = null,
102104
?callable $process = null,
103105
?callable $sort = null,
104106
): void {
107+
if (! empty($this->columns[$name])) {
108+
throw new Exception("Column $name already exists.");
109+
}
110+
111+
if ($label === null) {
112+
$label = str_replace('_', ' ', $name);
113+
$label = str_replace('-', ' ', $label);
114+
$label = ucfirst($label);
115+
}
116+
105117
$this->columns[$name] = new TableColumnDTO(
106118
name: $name,
107119
label: $label,
@@ -112,7 +124,7 @@ protected function column(
112124
classes: $classes,
113125
process: $process,
114126
sort: $sort,
115-
dateFormat: $dateformat
127+
dateFormat: $dateFormat
116128
);
117129
}
118130

0 commit comments

Comments
 (0)