From 2dfc35ff49028b7249f316b4d983eddc3eb3679c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petar=20S=CC=8Cpanja?= Date: Fri, 17 Apr 2020 14:08:19 +0200 Subject: [PATCH] Implement getSuggestedSearchText() --- lib/API/Values/Content/Search/Suggestion.php | 25 ++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/API/Values/Content/Search/Suggestion.php b/lib/API/Values/Content/Search/Suggestion.php index e7bee136..9d383414 100644 --- a/lib/API/Values/Content/Search/Suggestion.php +++ b/lib/API/Values/Content/Search/Suggestion.php @@ -64,4 +64,29 @@ public function getSuggestionsByOriginalWord(string $originalWord) return $this->suggestionsByOriginalWords[$originalWord]; } + + /** + * Get suggested search text based on returned spell check suggestions. + * + * @param string $originalSearchText + * + * @return string|null + */ + public function getSuggestedSearchText(string $originalSearchText) + { + $originalWords = $this->getOriginalWords(); + $suggestedWords = []; + + foreach ($originalWords as $originalWord) { + $suggestedWords[] = $this->getSuggestionsByOriginalWord($originalWord)[0]->suggestedWord; + } + + $suggestedSearchText = \str_replace($originalWords, $suggestedWords, $originalSearchText); + + if ($originalSearchText === $suggestedSearchText) { + return null; + } + + return $suggestedSearchText; + } }