From 6f48d69b24a84c92bf4ef2179c0dc8647220c5a0 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 3 Dec 2015 02:32:27 +0100 Subject: [PATCH] ObjectMixin: added getSuggestion for forward compatibility --- src/Utils/ObjectMixin.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/Utils/ObjectMixin.php b/src/Utils/ObjectMixin.php index 148c5b23c..98b50ef05 100644 --- a/src/Utils/ObjectMixin.php +++ b/src/Utils/ObjectMixin.php @@ -395,4 +395,27 @@ public static function getExtensionMethod($class, $name) return $cache = FALSE; } + + /** + * Finds the best suggestion (for 8-bit encoding). + * @return string|NULL + * @internal + */ + public static function getSuggestion(array $items, $value) + { + $norm = preg_replace($re = '#^(get|set|has|is|add)(?=[A-Z])#', '', $value); + $best = NULL; + $min = (strlen($value) / 4 + 1) * 10 + .1; + foreach (array_unique($items) as $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; + } + }