function FieldItemNormalizer::getAlternatives

Same name in other branches
  1. 10 core/modules/jsonapi/src/Normalizer/FieldItemNormalizer.php \Drupal\jsonapi\Normalizer\FieldItemNormalizer::getAlternatives()
  2. 11.x core/modules/jsonapi/src/Normalizer/FieldItemNormalizer.php \Drupal\jsonapi\Normalizer\FieldItemNormalizer::getAlternatives()

Provides alternatives for a given array and key.

Parameters

string $search_key: The search key to get alternatives for.

array $keys: The search space to search for alternatives in.

Return value

string[] An array of strings with suitable alternatives.

See also

\Drupal\Component\DependencyInjection\Container::getAlternatives()

1 call to FieldItemNormalizer::getAlternatives()
FieldItemNormalizer::denormalize in core/modules/jsonapi/src/Normalizer/FieldItemNormalizer.php

File

core/modules/jsonapi/src/Normalizer/FieldItemNormalizer.php, line 198

Class

FieldItemNormalizer
Converts the Drupal field item object to a JSON:API array structure.

Namespace

Drupal\jsonapi\Normalizer

Code

private static function getAlternatives(string $search_key, array $keys) : array {
    // $search_key is user input and could be longer than the 255 string length
    // limit of levenshtein().
    if (strlen($search_key) > 255) {
        return [];
    }
    $alternatives = [];
    foreach ($keys as $key) {
        $lev = levenshtein($search_key, $key);
        if ($lev <= strlen($search_key) / 3 || strpos($key, $search_key) !== FALSE) {
            $alternatives[] = $key;
        }
    }
    return $alternatives;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.