SuggestionFactory.php in Search API Autocomplete 8
File
src/Suggestion/SuggestionFactory.php
View source
<?php
namespace Drupal\search_api_autocomplete\Suggestion;
use Drupal\Core\Url;
class SuggestionFactory {
protected $userInput;
public function __construct($user_input = NULL) {
$this->userInput = $user_input;
}
public function createFromSuggestedKeys($suggested_keys, $results_count = NULL) {
$suggestion = new Suggestion($suggested_keys);
$lowercase_input = mb_strtolower($this->userInput);
$lowercase_keys = mb_strtolower($suggested_keys);
$start_position = mb_strpos($lowercase_keys, $lowercase_input);
if ($start_position === FALSE) {
$suggestion
->setLabel($suggested_keys);
}
else {
if ($start_position) {
$prefix = mb_substr($suggested_keys, 0, $start_position);
$suggestion
->setSuggestionPrefix($prefix);
}
$input_length = mb_strlen($this->userInput);
$end_position = $start_position + $input_length;
if ($end_position < mb_strlen($suggested_keys)) {
$suffix = mb_substr($suggested_keys, $end_position);
$suggestion
->setSuggestionSuffix($suffix);
}
$suggestion
->setUserInput(mb_substr($suggested_keys, $start_position, $input_length));
}
if ($results_count !== NULL) {
$suggestion
->setResultsCount($results_count);
}
return $suggestion;
}
public function createFromSuggestionSuffix($suggestion_suffix, $results_count = NULL) {
$suggestion = new Suggestion();
$suggestion
->setUserInput($this->userInput);
$suggestion
->setSuggestionSuffix($suggestion_suffix);
if ($results_count !== NULL) {
$suggestion
->setResultsCount($results_count);
}
return $suggestion;
}
public function createUrlSuggestion(Url $url, $label = NULL, array $render = NULL) {
$suggestion = new Suggestion(NULL, $url);
if ($label !== NULL) {
$suggestion
->setLabel($label);
}
if ($render !== NULL) {
$suggestion
->setRender($render);
}
return $suggestion;
}
}
Classes
Name |
Description |
SuggestionFactory |
Provides factory methods for simpler creation of autocomplete suggestions. |