30 lines
1.1 KiB
PHP
30 lines
1.1 KiB
PHP
<?php
|
|
|
|
class Helper {
|
|
/**
|
|
* Generate SQL Filter condition (space separated) for a given column.
|
|
*
|
|
* @param string|null $filterValue The filter value to match against.
|
|
* @param string $columnName The name of the column in the database table.
|
|
* @return string The SQL condition generated based on the filter value and column name.
|
|
* @noinspection PhpMissingParamTypeInspection
|
|
*/
|
|
public static function generateFilterCondition($filterValue, string $columnName): string {
|
|
$sql = "";
|
|
|
|
if (is_array($filterValue)) {
|
|
if (isset($filterValue['from']) && isset($filterValue['to'])) {
|
|
$sql = " AND `$columnName` >= " . $filterValue['from'] . " AND `$columnName` <= " . $filterValue['to'];
|
|
}
|
|
} else if ($filterValue === "0" || $filterValue === "1") {
|
|
$sql .= " AND `$columnName` = " . $filterValue;
|
|
} else if (!empty($filterValue)) {
|
|
$filterItems = explode(" ", $filterValue);
|
|
foreach ($filterItems as $item) {
|
|
$sql .= " AND `$columnName` LIKE '%" . $item . "%'";
|
|
}
|
|
}
|
|
|
|
return $sql;
|
|
}
|
|
} |