Nette\Utils\ObjectMixin::getSuggestion PHP Method

getSuggestion() public static method

Finds the best suggestion (for 8-bit encoding).
public static getSuggestion ( array $possibilities, $value ) : string | null
$possibilities array
return string | null
    public static function getSuggestion(array $possibilities, $value)
    {
        $norm = preg_replace($re = '#^(get|set|has|is|add)(?=[A-Z])#', '', $value);
        $best = NULL;
        $min = (strlen($value) / 4 + 1) * 10 + 0.1;
        foreach (array_unique($possibilities, SORT_REGULAR) as $item) {
            $item = $item instanceof \Reflector ? $item->getName() : $item;
            if ($item !== $value && (($len = levenshtein($item, $value, 10, 11, 10)) < $min || ($len = levenshtein(preg_replace($re, '', $item), $norm, 10, 11, 10) + 20) < $min)) {
                $min = $len;
                $best = $item;
            }
        }
        return $best;
    }

Usage Example

Example #1
0
 /**
  * Checks whether $config contains only $expected items and returns combined array.
  * @return array
  * @throws Nette\InvalidStateException
  */
 public function validateConfig(array $expected, array $config = NULL, $name = NULL)
 {
     if (func_num_args() === 1) {
         return $this->config = $this->validateConfig($expected, $this->config);
     }
     if ($extra = array_diff_key((array) $config, $expected)) {
         $name = $name ?: $this->name;
         $hint = Nette\Utils\ObjectMixin::getSuggestion(array_keys($expected), key($extra));
         $extra = $hint ? key($extra) : implode(", {$name}.", array_keys($extra));
         throw new Nette\InvalidStateException("Unknown configuration option {$name}.{$extra}" . ($hint ? ", did you mean {$name}.{$hint}?" : '.'));
     }
     return Config\Helpers::merge($config, $expected);
 }
All Usage Examples Of Nette\Utils\ObjectMixin::getSuggestion