diff --git a/php/model/LocalArticleManager.php b/php/model/LocalArticleManager.php index cfccf68..05d76a0 100644 --- a/php/model/LocalArticleManager.php +++ b/php/model/LocalArticleManager.php @@ -170,34 +170,42 @@ 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 = []; + public function search(string $keyword): array { + $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; - - // 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) - $filteredArticles[] = new Article( - intval($article['id']), - $article['title'], - $article['content'], - $article['author'], - $article['category'], - $article['tags'], - $article['creationDate'] - ); - } - } - - return $filteredArticles; + // Sicherheits-Check: Falls getAllArticles aus irgendeinem Grund kein Array liefert + if (!is_array($articles)) { + return []; } + // Suchbegriff trimmen und in Kleinbuchstaben umwandeln für besseren Vergleich + $cleanKeyword = mb_strtolower(trim($keyword)); + + foreach ($articles as $article) { + // Sicherstellen, dass die Felder existieren und Strings sind + $title = isset($article['title']) ? mb_strtolower((string)$article['title']) : ''; + $content = isset($article['content']) ? mb_strtolower((string)$article['content']) : ''; + + // Suche im Titel ODER im Inhalt + if (($cleanKeyword !== '' && strpos($title, $cleanKeyword) !== false) || + ($cleanKeyword !== '' && strpos($content, $cleanKeyword) !== false)) { + + // Wir mappen die Daten auf das Article-Objekt (wie in deiner getArticlesByAuthor) + $filteredArticles[] = new Article( + intval($article['id'] ?? 0), + $article['title'] ?? '', + $article['content'] ?? '', + $article['author'] ?? '', + $article['category'] ?? '', + $article['tags'] ?? '', + $article['creationDate'] ?? '' + ); + } + } + + return $filteredArticles; +} + } ?> \ No newline at end of file