asdasd
This commit is contained in:
47
src/App/Search.php
Normal file
47
src/App/Search.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App;
|
||||
|
||||
final class Search
|
||||
{
|
||||
public function __construct(private ?\PDO $pdo) {}
|
||||
|
||||
public function searchEvents(string $query, int $limit = 100): array
|
||||
{
|
||||
if (!$this->pdo) return [];
|
||||
|
||||
$q = trim($query);
|
||||
if ($q === '') return [];
|
||||
|
||||
$tokens = array_filter(preg_split('/\s+/', $q) ?: [], fn($t) => $t !== '');
|
||||
if (!$tokens) {
|
||||
$tokens = [$q];
|
||||
}
|
||||
|
||||
$conditions = [];
|
||||
$params = [];
|
||||
$i = 0;
|
||||
foreach ($tokens as $tok) {
|
||||
$ph = ':kw' . $i++;
|
||||
$conditions[] = "(title LIKE $ph OR teaser_public LIKE $ph OR description LIKE $ph OR city LIKE $ph OR region LIKE $ph OR zip LIKE $ph)";
|
||||
$params[$ph] = '%' . $tok . '%';
|
||||
}
|
||||
$where = $conditions ? ('AND ' . implode(' AND ', $conditions)) : '';
|
||||
|
||||
$sql = "SELECT id, title, teaser_public, description, city, region, starts_at, visibility, allow_kids, location_label
|
||||
FROM events
|
||||
WHERE starts_at >= NOW()
|
||||
AND status != 'cancelled'
|
||||
$where
|
||||
ORDER BY starts_at ASC
|
||||
LIMIT :lim";
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
foreach ($params as $k => $v) {
|
||||
$stmt->bindValue($k, $v, \PDO::PARAM_STR);
|
||||
}
|
||||
$stmt->bindValue(':lim', $limit, \PDO::PARAM_INT);
|
||||
$stmt->execute();
|
||||
return $stmt->fetchAll(\PDO::FETCH_ASSOC) ?: [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user