You are here

public function StreamingExpressionBuilder::_escaped_value in Search API Solr 4.x

Same name and namespace in other branches
  1. 8.3 src/Utility/StreamingExpressionBuilder.php \Drupal\search_api_solr\Utility\StreamingExpressionBuilder::_escaped_value()
  2. 8.2 src/Utility/StreamingExpressionBuilder.php \Drupal\search_api_solr\Utility\StreamingExpressionBuilder::_escaped_value()

Escapes a value to be used in a Solr streaming expression.

Parameters

string $value: The string to escape.

bool $single_term: (optional) Escapes the value as single term if TRUE, otherwise as phrase. Defaults to TRUE.

string $search_api_field_name: (optional) If provided the method will use it to check for each processor whether it is supposed to be run on the value. If the the name is not provided no processor will act on the value.

Return value

string The escaped value.

2 calls to StreamingExpressionBuilder::_escaped_value()
StreamingExpressionBuilder::_escape_and_implode in src/Utility/StreamingExpressionBuilder.php
Calls _escaped_value on each array element and returns the imploded result.
StreamingExpressionBuilder::_field_escaped_value in src/Utility/StreamingExpressionBuilder.php
Formats a field and its escaped value for a Solr streaming expression.

File

src/Utility/StreamingExpressionBuilder.php, line 346

Class

StreamingExpressionBuilder
Provides methods for creating streaming expressions targeting a given index.

Namespace

Drupal\search_api_solr\Utility

Code

public function _escaped_value(string $value, bool $single_term = TRUE, string $search_api_field_name = NULL) {
  if (is_string($value) && $search_api_field_name) {
    foreach ($this->index
      ->getProcessorsByStage(ProcessorInterface::STAGE_PREPROCESS_QUERY) as $processor) {
      if ($processor instanceof SolrProcessorInterface) {
        $configuration = $processor
          ->getConfiguration();
        if (in_array($search_api_field_name, $configuration['fields'])) {
          $value = $processor
            ->encodeStreamingExpressionValue($value) ?: $value;
        }
      }
    }
  }
  $escaped_string = $single_term ? $this->queryHelper
    ->escapeTerm($value) : $this->queryHelper
    ->escapePhrase($value);

  // If the escaped strings are to be used inside a streaming expression
  // double quotes need to be escaped once more.
  // (e.g. q="field:\"word1 word2\"").
  // See also https://issues.apache.org/jira/browse/SOLR-8409
  $escaped_string = str_replace('"', '\\"', $escaped_string);
  return $escaped_string;
}