From 851bad3ada29c04b4ac6e21f7582b917e5d898b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Goetz?= Date: Fri, 21 Sep 2018 23:41:49 +0200 Subject: [PATCH] Be more user friendly if the format is incorrect --- libs/Daux.php | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/libs/Daux.php b/libs/Daux.php index f4637cf..0f99e63 100644 --- a/libs/Daux.php +++ b/libs/Daux.php @@ -293,6 +293,20 @@ class Daux return $class; } + protected function findAlternatives($input, $words) { + $alternatives = []; + + foreach ($words as $word) { + $lev = levenshtein($input, $word); + + if ($lev <= \strlen($word) / 3) { + $alternatives[] = $word; + } + } + + return $alternatives; + } + /** * @return \Todaymade\Daux\Format\Base\Generator */ @@ -307,7 +321,19 @@ class Daux $format = $this->getParams()->getFormat(); if (!array_key_exists($format, $generators)) { - throw new \RuntimeException("The format '$format' doesn't exist, did you forget to set your processor ?"); + $message = "The format '$format' doesn't exist, did you forget to set your processor ?"; + + $alternatives = $this->findAlternatives($format, array_keys($generators)); + + if (0 == \count($alternatives)) { + $message .= "\n\nAvailable formats are \n " . implode("\n ", array_keys($generators)); + } elseif (1 == \count($alternatives)) { + $message .= "\n\nDid you mean this?\n " . implode("\n ", $alternatives); + } else { + $message .= "\n\nDid you mean one of these?\n " . implode("\n ", $alternatives); + } + + throw new \RuntimeException($message); } $class = $generators[$format];