You are here

protected function Database::convert in Search API 8

Converts a value between two search types.

Parameters

mixed $value: The value to convert.

string $type: The Search API type to convert to. (Has to be a type supported by this backend.)

string $original_type: The value's original type.

\Drupal\search_api\IndexInterface $index: The index for which this conversion takes place.

Return value

mixed The converted value.

Throws

\Drupal\search_api\SearchApiException Thrown if $type is unknown.

1 call to Database::convert()
Database::indexItem in modules/search_api_db/src/Plugin/search_api/backend/Database.php
Indexes a single item on the specified index.

File

modules/search_api_db/src/Plugin/search_api/backend/Database.php, line 1462

Class

Database
Indexes and searches items using the database.

Namespace

Drupal\search_api_db\Plugin\search_api\backend

Code

protected function convert($value, $type, $original_type, IndexInterface $index) {
  if (!isset($value)) {

    // For text fields, we have to return an array even if the value is NULL.
    return $this
      ->getDataTypeHelper()
      ->isTextType($type) ? [] : NULL;
  }
  switch ($type) {
    case 'text':

      /** @var \Drupal\search_api\Plugin\search_api\data_type\value\TextValueInterface $value */
      $tokens = $value
        ->getTokens();
      if ($tokens === NULL) {
        $tokens = [];
        $text = $value
          ->getText();

        // For dates, splitting the timestamp makes no sense.
        if ($original_type == 'date') {
          $text = $this
            ->getDateFormatter()
            ->format($text, 'custom', 'Y y F M n m j d l D');
        }
        foreach (static::splitIntoWords($text) as $word) {
          if ($word) {
            if (mb_strlen($word) > 50) {
              $this
                ->getLogger()
                ->warning('An overlong word (more than 50 characters) was encountered while indexing: %word.<br />Since database search servers currently cannot index words of more than 50 characters, the word was truncated for indexing. If this should not be a single word, please make sure the "Tokenizer" processor is enabled and configured correctly for index %index.', [
                '%word' => $word,
                '%index' => $index
                  ->label(),
              ]);
              $word = mb_substr($word, 0, 50);
            }
            $tokens[] = new TextToken($word);
          }
        }
      }
      else {
        while (TRUE) {
          foreach ($tokens as $i => $token) {

            // Check for over-long tokens.
            $score = $token
              ->getBoost();
            $word = $token
              ->getText();
            if (mb_strlen($word) > 50) {
              $new_tokens = [];
              foreach (static::splitIntoWords($word) as $word) {
                if (mb_strlen($word) > 50) {
                  $this
                    ->getLogger()
                    ->warning('An overlong word (more than 50 characters) was encountered while indexing: %word.<br />Since database search servers currently cannot index words of more than 50 characters, the word was truncated for indexing. If this should not be a single word, please make sure the "Tokenizer" processor is enabled and configured correctly for index %index.', [
                    '%word' => $word,
                    '%index' => $index
                      ->label(),
                  ]);
                  $word = mb_substr($word, 0, 50);
                }
                $new_tokens[] = new TextToken($word, $score);
              }
              array_splice($tokens, $i, 1, $new_tokens);

              // Restart the loop looking through all the tokens.
              continue 2;
            }
          }
          break;
        }
      }
      return $tokens;
    case 'string':

      // For non-dates, PHP can handle this well enough.
      if ($original_type == 'date') {
        return date('c', $value);
      }
      if (mb_strlen($value) > 255) {
        $value = mb_substr($value, 0, 255);
        $this
          ->getLogger()
          ->warning('An overlong value (more than 255 characters) was encountered while indexing: %value.<br />Database search servers currently cannot index such values correctly – the value was therefore trimmed to the allowed length.', [
          '%value' => $value,
        ]);
      }
      return $value;
    case 'integer':
    case 'date':
      return (int) $value;
    case 'decimal':
      return (double) $value;
    case 'boolean':
      return $value ? 1 : 0;
    default:
      throw new SearchApiException("Unknown field type '{$type}'.");
  }
}