WIP Suche implementiert, sowie Sortieren der Ergebnisse #19
@@ -172,31 +172,38 @@ class LocalArticleManager implements ArticleManagerDAO {
|
||||
// getarticlesbyauthor nutzen um auch nach kategorien zu suchen
|
||||
public function search(string $keyword): array
|
||||
{
|
||||
// 1. Alle Artikel aus der JSON-Datei laden
|
||||
$articles = $this->getAllArticles();
|
||||
$filteredArticles = [];
|
||||
|
||||
// 2. Artikel durchlaufen und filtern
|
||||
foreach ($articles as $article) {
|
||||
$inTitle = isset($article['title']) && stripos($article['title'], $keyword) !== false;
|
||||
$inContent = isset($article['content']) && stripos($article['content'], $keyword) !== false;
|
||||
if (!is_array($articles)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Wenn das Keyword im Titel ODER im Inhalt vorkommt
|
||||
if ($inTitle || $inContent) {
|
||||
// Wir mappen die JSON-Daten auf ein echtes Article-Objekt (genau wie bei getArticlesByAuthor)
|
||||
// Verwende strtolower statt mb_strtolower
|
||||
$cleanKeyword = strtolower(trim($keyword));
|
||||
|
||||
foreach ($articles as $article) {
|
||||
// Sicherstellen, dass die Felder existieren und in Kleinbuchstaben umwandeln
|
||||
$title = isset($article['title']) ? strtolower((string)$article['title']) : '';
|
||||
$content = isset($article['content']) ? strtolower((string)$article['content']) : '';
|
||||
|
||||
if (($cleanKeyword !== '' && strpos($title, $cleanKeyword) !== false) ||
|
||||
($cleanKeyword !== '' && strpos($content, $cleanKeyword) !== false)) {
|
||||
|
||||
// Falls die Article-Klasse geladen ist, wird das hier fehlerfrei ausgeführt:
|
||||
$filteredArticles[] = new Article(
|
||||
intval($article['id']),
|
||||
$article['title'],
|
||||
$article['content'],
|
||||
$article['author'],
|
||||
$article['category'],
|
||||
$article['tags'],
|
||||
$article['creationDate']
|
||||
intval($article['id'] ?? 0),
|
||||
$article['title'] ?? '',
|
||||
$article['content'] ?? '',
|
||||
$article['author'] ?? '',
|
||||
$article['category'] ?? '',
|
||||
$article['tags'] ?? '',
|
||||
$article['creationDate'] ?? ''
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $filteredArticles;
|
||||
return $filteredArticles;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user