From fabac5b895d8be533fa3d11c98815cc94eab829c Mon Sep 17 00:00:00 2001 From: kocoten1992 Date: Sat, 26 Nov 2022 21:52:15 +0700 Subject: [PATCH] feat+fix: add test param min_length + change it position --- src/StrUtil.php | 4 ++-- tests/StrUtilTest.php | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/StrUtil.php b/src/StrUtil.php index a5e02ee..ee0167c 100644 --- a/src/StrUtil.php +++ b/src/StrUtil.php @@ -8,9 +8,9 @@ class StrUtil { public static function toSearchablePhrases( string $string, + int $min_length = 1, string $separator = ' ', int $limit = PHP_INT_MAX, - int $min_length = 1, ): array { $string = mb_strtolower($string); @@ -35,7 +35,7 @@ public static function toSearchablePhrases( break; } - $length = 1; + $length = $min_length; while ($pointer + $length < $sub_string_length + 1) { $result[implode('', array_slice($mb_str_split, $pointer, $length))] = true; diff --git a/tests/StrUtilTest.php b/tests/StrUtilTest.php index 40b8e69..75ed9eb 100644 --- a/tests/StrUtilTest.php +++ b/tests/StrUtilTest.php @@ -14,12 +14,32 @@ public function test_to_searchable_phrases() $this->assertEquals(16, count(StrUtil::toSearchablePhrases('léon'))); + $this->assertEquals(16, count(StrUtil::toSearchablePhrases('léon'))); + $this->assertEquals( StrUtil::toSearchablePhrases('amélie'), StrUtil::toSearchablePhrases('AméLie') ); $this->assertEquals(32, count(StrUtil::toSearchablePhrases('amélie'))); + + $this->assertEquals( + StrUtil::toSearchablePhrases('amélie', 4), + [ + "amél", + "amel", + "améli", + "ameli", + "amélie", + "amelie", + "méli", + "meli", + "mélie", + "melie", + "élie", + "elie", + ] + ); } public function test_replace_once()