From 05ff2d90f34c3f4433cf0dfc3ad430af845c2f2b Mon Sep 17 00:00:00 2001 From: Jan-Niclas Loosen Date: Mon, 19 Oct 2026 17:48:30 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20FontSuggest=20=E2=80=93=20=C3=A4hnliche?= =?UTF-8?q?=20Schriftnamen=20f=C3=BCr=20unbekannte=20Schriften=20(W5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 5.5 --- src/Integrations/Divi/Design/FontSuggest.php | 48 +++++++++++++ .../Divi/Design/FontSuggestTest.php | 67 +++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 src/Integrations/Divi/Design/FontSuggest.php create mode 100644 tests/Unit/Integrations/Divi/Design/FontSuggestTest.php diff --git a/src/Integrations/Divi/Design/FontSuggest.php b/src/Integrations/Divi/Design/FontSuggest.php new file mode 100644 index 0000000..1744402 --- /dev/null +++ b/src/Integrations/Divi/Design/FontSuggest.php @@ -0,0 +1,48 @@ + $names + * @return list At most $limit names. + */ + public static function similar(string $name, array $names, int $limit = 5): array + { + $needle = mb_strtolower(trim($name)); + if ($needle === '' || $limit < 1) { + return []; + } + $maxDistance = max(3, intdiv(mb_strlen($needle), 3)); + $ranked = []; + foreach (array_unique($names) as $candidate) { + $lower = mb_strtolower($candidate); + $distance = levenshtein($needle, $lower); + $rank = match (true) { + $lower === $needle => 0, + str_starts_with($lower, $needle) => 1, + str_contains($lower, $needle) => 2, + $distance <= $maxDistance => 3, + default => null, + }; + if ($rank !== null) { + $ranked[] = [$rank, $distance, mb_strlen($candidate), $candidate]; + } + } + usort($ranked, static fn (array $a, array $b): int => $a[0] <=> $b[0] ?: $a[1] <=> $b[1] ?: $a[2] <=> $b[2] + ?: strcmp($a[3], $b[3])); + + return array_slice(array_column($ranked, 3), 0, $limit); + } +} diff --git a/tests/Unit/Integrations/Divi/Design/FontSuggestTest.php b/tests/Unit/Integrations/Divi/Design/FontSuggestTest.php new file mode 100644 index 0000000..0b01d16 --- /dev/null +++ b/tests/Unit/Integrations/Divi/Design/FontSuggestTest.php @@ -0,0 +1,67 @@ +