anpassung von der search function
This commit is contained in:
@@ -170,42 +170,34 @@ class LocalArticleManager implements ArticleManagerDAO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// getarticlesbyauthor nutzen um auch nach kategorien zu suchen
|
// getarticlesbyauthor nutzen um auch nach kategorien zu suchen
|
||||||
public function search(string $keyword): array {
|
public function search(string $keyword): array
|
||||||
|
{
|
||||||
|
// 1. Alle Artikel aus der JSON-Datei laden
|
||||||
$articles = $this->getAllArticles();
|
$articles = $this->getAllArticles();
|
||||||
$filteredArticles = [];
|
$filteredArticles = [];
|
||||||
|
|
||||||
// Sicherheits-Check: Falls getAllArticles aus irgendeinem Grund kein Array liefert
|
// 2. Artikel durchlaufen und filtern
|
||||||
if (!is_array($articles)) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Suchbegriff trimmen und in Kleinbuchstaben umwandeln für besseren Vergleich
|
|
||||||
$cleanKeyword = mb_strtolower(trim($keyword));
|
|
||||||
|
|
||||||
foreach ($articles as $article) {
|
foreach ($articles as $article) {
|
||||||
// Sicherstellen, dass die Felder existieren und Strings sind
|
$inTitle = isset($article['title']) && stripos($article['title'], $keyword) !== false;
|
||||||
$title = isset($article['title']) ? mb_strtolower((string)$article['title']) : '';
|
$inContent = isset($article['content']) && stripos($article['content'], $keyword) !== false;
|
||||||
$content = isset($article['content']) ? mb_strtolower((string)$article['content']) : '';
|
|
||||||
|
|
||||||
// Suche im Titel ODER im Inhalt
|
// Wenn das Keyword im Titel ODER im Inhalt vorkommt
|
||||||
if (($cleanKeyword !== '' && strpos($title, $cleanKeyword) !== false) ||
|
if ($inTitle || $inContent) {
|
||||||
($cleanKeyword !== '' && strpos($content, $cleanKeyword) !== false)) {
|
// Wir mappen die JSON-Daten auf ein echtes Article-Objekt (genau wie bei getArticlesByAuthor)
|
||||||
|
|
||||||
// Wir mappen die Daten auf das Article-Objekt (wie in deiner getArticlesByAuthor)
|
|
||||||
$filteredArticles[] = new Article(
|
$filteredArticles[] = new Article(
|
||||||
intval($article['id'] ?? 0),
|
intval($article['id']),
|
||||||
$article['title'] ?? '',
|
$article['title'],
|
||||||
$article['content'] ?? '',
|
$article['content'],
|
||||||
$article['author'] ?? '',
|
$article['author'],
|
||||||
$article['category'] ?? '',
|
$article['category'],
|
||||||
$article['tags'] ?? '',
|
$article['tags'],
|
||||||
$article['creationDate'] ?? ''
|
$article['creationDate']
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $filteredArticles;
|
return $filteredArticles;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
Reference in New Issue
Block a user