anpassung von der search function
This commit is contained in:
@@ -170,36 +170,28 @@ class LocalArticleManager implements ArticleManagerDAO {
|
||||
}
|
||||
|
||||
// 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();
|
||||
$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));
|
||||
|
||||
// 2. Artikel durchlaufen und filtern
|
||||
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']) : '';
|
||||
$inTitle = isset($article['title']) && stripos($article['title'], $keyword) !== false;
|
||||
$inContent = isset($article['content']) && stripos($article['content'], $keyword) !== false;
|
||||
|
||||
// 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)
|
||||
// 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'] ?? 0),
|
||||
$article['title'] ?? '',
|
||||
$article['content'] ?? '',
|
||||
$article['author'] ?? '',
|
||||
$article['category'] ?? '',
|
||||
$article['tags'] ?? '',
|
||||
$article['creationDate'] ?? ''
|
||||
intval($article['id']),
|
||||
$article['title'],
|
||||
$article['content'],
|
||||
$article['author'],
|
||||
$article['category'],
|
||||
$article['tags'],
|
||||
$article['creationDate']
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user