Implementierung von neuem such algorithmus und entsprechenden anpassungen in anderen dateien
This commit is contained in:
@@ -169,24 +169,34 @@ class LocalArticleManager implements ArticleManagerDAO {
|
||||
return $filteredArticles;
|
||||
}
|
||||
|
||||
public function search(string $keyword): array
|
||||
// getarticlesbyauthor nutzen um auch nach kategorien zu suchen
|
||||
public function search(string $keyword): array
|
||||
{
|
||||
$results = [];
|
||||
$contentFolder = __DIR__ . '/../content/';
|
||||
$files = glob($contentFolder . "*.php");
|
||||
// 1. Alle Artikel aus der JSON-Datei laden
|
||||
$articles = $this->getAllArticles();
|
||||
$filteredArticles = [];
|
||||
|
||||
foreach ($files as $file) {
|
||||
$content = file_get_contents($file);
|
||||
if (stripos($content, $keyword) !== false) {
|
||||
$filename = basename($file, ".php");
|
||||
$results[] = [
|
||||
'title' => ucfirst($filename),
|
||||
'link' => "index.php?pfad=" . $filename
|
||||
];
|
||||
// 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 $results;
|
||||
|
||||
return $filteredArticles;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user