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; + } }