You are here

protected function Database::prepareKeys in Search API 8

Removes nested expressions and phrase groupings from the search keys.

Used as a helper method in createDbQuery() and createDbCondition().

Parameters

array|string|null $keys: The keys which should be preprocessed.

bool $tokenizer_active: (optional) TRUE if we can rely on the "Tokenizer" processor already having preprocessed the keywords.

Return value

array|string|null The preprocessed keys.

2 calls to Database::prepareKeys()
Database::createDbCondition in modules/search_api_db/src/Plugin/search_api/backend/Database.php
Creates a database query condition for a given search filter.
Database::createDbQuery in modules/search_api_db/src/Plugin/search_api/backend/Database.php
Creates a database query for a search.

File

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

Class

Database
Indexes and searches items using the database.

Namespace

Drupal\search_api_db\Plugin\search_api\backend

Code

protected function prepareKeys($keys, bool $tokenizer_active = FALSE) {
  if (is_scalar($keys)) {
    $keys = $this
      ->splitKeys($keys, $tokenizer_active);
    return is_array($keys) ? $this
      ->eliminateDuplicates($keys) : $keys;
  }
  elseif (!$keys) {
    return NULL;
  }
  $keys = $this
    ->splitKeys($keys, $tokenizer_active);
  $keys = $this
    ->eliminateDuplicates($keys);
  $conj = $keys['#conjunction'];
  $neg = !empty($keys['#negation']);
  foreach ($keys as $i => &$nested) {
    if (is_array($nested)) {
      $nested = $this
        ->prepareKeys($nested, $tokenizer_active);
      if (is_array($nested) && $neg == !empty($nested['#negation'])) {
        if ($nested['#conjunction'] == $conj) {
          unset($nested['#conjunction'], $nested['#negation']);
          foreach ($nested as $renested) {
            $keys[] = $renested;
          }
          unset($keys[$i]);
        }
      }
    }
  }
  $keys = array_filter($keys);
  if (($count = count($keys)) <= 2) {
    if ($count < 2 || isset($keys['#negation'])) {
      $keys = NULL;
    }
    else {
      unset($keys['#conjunction']);
      $keys = reset($keys);
    }
  }
  return $keys;
}