weitere anpassungen der such funktion
This commit is contained in:
@@ -172,31 +172,38 @@ 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 = [];
|
||||||
|
|
||||||
// 2. Artikel durchlaufen und filtern
|
if (!is_array($articles)) {
|
||||||
foreach ($articles as $article) {
|
return [];
|
||||||
$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
|
// Verwende strtolower statt mb_strtolower
|
||||||
if ($inTitle || $inContent) {
|
$cleanKeyword = strtolower(trim($keyword));
|
||||||
// Wir mappen die JSON-Daten auf ein echtes Article-Objekt (genau wie bei getArticlesByAuthor)
|
|
||||||
|
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(
|
$filteredArticles[] = new Article(
|
||||||
intval($article['id']),
|
intval($article['id'] ?? 0),
|
||||||
$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