From 0f477d2a7cf72fa3fd491fc3604ee3b793aee4ca Mon Sep 17 00:00:00 2001 From: Jack Wilkinson Date: Thu, 15 Sep 2022 18:26:22 +0100 Subject: [PATCH 01/11] Added string helper functions --- src/Support/Str.php | 64 +++++++++++++++++++++++++++++++++++++++ tests/Support/StrTest.php | 30 ++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/src/Support/Str.php b/src/Support/Str.php index abe58f5ed..1edb34f63 100644 --- a/src/Support/Str.php +++ b/src/Support/Str.php @@ -85,6 +85,70 @@ public static function join(iterable $items, string $glue = ', ', string $lastGl return $result; } + /** + * Apply an index to a string, i.e. + * winter -> winter_1 + * winter_1 -> winter_2 + */ + public static function index(string $str, string $separator = '_', int $starting = 1, int $step = 1): string + { + if (!preg_match('/(.*?)' . $separator . '(\d*$)/', $str, $matches)) { + return $str . $separator . $starting; + } + + return $matches[1] . $separator . (((int) $matches[2]) + $step); + } + + /** + * Apply a unique index to a string from provided list i.e. + * winter, [winter_1, winter_2] -> winter_3 + * winter, [winter_1, winter_3] -> winter_4 + */ + public static function unique(string $str, array $list, string $separator = '_', int $starting = 1, int $step = 1): string + { + $indexes = []; + + foreach ($list as $item) { + if (!preg_match('/(.*?)' . $str . $separator . '(\d*$)/', $item, $matches)) { + continue; + } + + $indexes[] = (int) $matches[2]; + } + + return empty($indexes) + ? $str . $separator . $starting + : $str . $separator . (max($indexes) + $step); + } + + /** + * Apply a unique index to a filename from provided list i.e. + * winter.txt, [winter_1.txt, winter_2.txt] -> winter_3.txt + * winter.txt, [winter_1.txt, winter_3.txt] -> winter_4.txt + */ + public static function uniqueFile(string $str, array $list, string $separator = '_', int $starting = 1, int $step = 1): string + { + $indexes = []; + + $info = pathinfo($str); + + if (empty($info['filename']) || empty($info['extension'])) { + throw new \InvalidArgumentException('$str must be a file name'); + } + + foreach ($list as $item) { + if (!preg_match('/' . $info['filename'] . $separator . '(\d*)\.' . $info['extension'] . '/', $item, $matches)) { + continue; + } + + $indexes[] = (int) $matches[1]; + } + + return empty($indexes) + ? $info['filename'] . $separator . $starting . '.' . $info['extension'] + : $info['filename'] . $separator . (max($indexes) + $step) . '.' . $info['extension']; + } + /** * Converts line breaks to a standard \r\n pattern. */ diff --git a/tests/Support/StrTest.php b/tests/Support/StrTest.php index eba82d57a..632616bdf 100644 --- a/tests/Support/StrTest.php +++ b/tests/Support/StrTest.php @@ -15,4 +15,34 @@ public function testJoin() $this->assertSame('bob or joe', Str::join(['bob', 'joe'], ', ', ', or ', ' or ')); $this->assertSame('bob; joe; or sally', Str::join(['bob', 'joe', 'sally'], '; ', '; or ')); } + + public function testIndex() + { + $this->assertSame('winter_cms_1', Str::index('winter_cms')); + $this->assertSame('winter_cms_2', Str::index('winter_cms_1')); + $this->assertSame('winter_cms_43', Str::index('winter_cms_42')); + $this->assertSame('winter_cms 3', Str::index('winter_cms 2', separator: ' ')); + $this->assertSame('winter_cms 6', Str::index('winter_cms 2', separator: ' ', step: 4)); + $this->assertSame('winter_cms8', Str::index('winter_cms4', separator: '', step: 4)); + $this->assertSame('winter_cms4_1', Str::index('winter_cms4', step: 4)); + $this->assertSame('winter_cms-22', Str::index('winter_cms', separator: '-', starting: 22)); + $this->assertSame('winter cms 1', Str::index('winter cms', separator: ' ')); + $this->assertSame('winter cms 2', Str::index('winter cms 1', separator: ' ')); + } + + public function testUnique() + { + $this->assertSame('winter_cms_4', Str::unique('winter_cms', ['winter_cms_1', 'test_5', 'winter_cms_3'])); + $this->assertSame('winter_cms_98', Str::unique('winter_cms', ['winter_cms_97', 'test_5', 'winter_cms_3'])); + $this->assertSame('winter_cms 1', Str::unique('winter_cms', ['winter_cms_1', 'test_5', 'winter_cms_3'], ' ')); + $this->assertSame('winter_cms_1', Str::unique('winter_cms', ['test_5'])); + } + + public function testUniqueFile() + { + $this->assertSame('winter_4.cms', Str::uniqueFile('winter.cms', ['winter_1.cms', 'test_5', 'winter_2.cms'])); + $this->assertSame('winter_98.cms', Str::uniqueFile('winter.cms', ['winter_97.cms', 'test_5', 'winter_1.cms'])); + $this->assertSame('winter 1.cms', Str::uniqueFile('winter.cms', ['winter_1.cms', 'test_5', 'winter_3.cms'], ' ')); + $this->assertSame('winter_1.cms', Str::uniqueFile('winter.cms', ['test_5'])); + } } From bd5152ba616b10a60e5dc120a03b8b4ba21055e7 Mon Sep 17 00:00:00 2001 From: Jack Wilkinson <31214002+jaxwilko@users.noreply.github.com> Date: Fri, 16 Sep 2022 18:26:03 +0100 Subject: [PATCH 02/11] Fixed test --- tests/Support/StrTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Support/StrTest.php b/tests/Support/StrTest.php index 632616bdf..374b40df9 100644 --- a/tests/Support/StrTest.php +++ b/tests/Support/StrTest.php @@ -40,7 +40,7 @@ public function testUnique() public function testUniqueFile() { - $this->assertSame('winter_4.cms', Str::uniqueFile('winter.cms', ['winter_1.cms', 'test_5', 'winter_2.cms'])); + $this->assertSame('winter_4.cms', Str::uniqueFile('winter.cms', ['winter_1.cms', 'test_5', 'winter_3.cms'])); $this->assertSame('winter_98.cms', Str::uniqueFile('winter.cms', ['winter_97.cms', 'test_5', 'winter_1.cms'])); $this->assertSame('winter 1.cms', Str::uniqueFile('winter.cms', ['winter_1.cms', 'test_5', 'winter_3.cms'], ' ')); $this->assertSame('winter_1.cms', Str::uniqueFile('winter.cms', ['test_5'])); From 85c054a2efa14ffe5a0d91e298ed261f1723b08e Mon Sep 17 00:00:00 2001 From: Jack Wilkinson <31214002+jaxwilko@users.noreply.github.com> Date: Fri, 16 Sep 2022 18:28:04 +0100 Subject: [PATCH 03/11] Removed alignement because it did not conform --- tests/Support/StrTest.php | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/tests/Support/StrTest.php b/tests/Support/StrTest.php index 374b40df9..4b19d294a 100644 --- a/tests/Support/StrTest.php +++ b/tests/Support/StrTest.php @@ -18,31 +18,31 @@ public function testJoin() public function testIndex() { - $this->assertSame('winter_cms_1', Str::index('winter_cms')); - $this->assertSame('winter_cms_2', Str::index('winter_cms_1')); - $this->assertSame('winter_cms_43', Str::index('winter_cms_42')); - $this->assertSame('winter_cms 3', Str::index('winter_cms 2', separator: ' ')); - $this->assertSame('winter_cms 6', Str::index('winter_cms 2', separator: ' ', step: 4)); - $this->assertSame('winter_cms8', Str::index('winter_cms4', separator: '', step: 4)); - $this->assertSame('winter_cms4_1', Str::index('winter_cms4', step: 4)); - $this->assertSame('winter_cms-22', Str::index('winter_cms', separator: '-', starting: 22)); - $this->assertSame('winter cms 1', Str::index('winter cms', separator: ' ')); - $this->assertSame('winter cms 2', Str::index('winter cms 1', separator: ' ')); + $this->assertSame('winter_cms_1', Str::index('winter_cms')); + $this->assertSame('winter_cms_2', Str::index('winter_cms_1')); + $this->assertSame('winter_cms_43', Str::index('winter_cms_42')); + $this->assertSame('winter_cms 3', Str::index('winter_cms 2', separator: ' ')); + $this->assertSame('winter_cms 6', Str::index('winter_cms 2', separator: ' ', step: 4)); + $this->assertSame('winter_cms8', Str::index('winter_cms4', separator: '', step: 4)); + $this->assertSame('winter_cms4_1', Str::index('winter_cms4', step: 4)); + $this->assertSame('winter_cms-22', Str::index('winter_cms', separator: '-', starting: 22)); + $this->assertSame('winter cms 1', Str::index('winter cms', separator: ' ')); + $this->assertSame('winter cms 2', Str::index('winter cms 1', separator: ' ')); } public function testUnique() { - $this->assertSame('winter_cms_4', Str::unique('winter_cms', ['winter_cms_1', 'test_5', 'winter_cms_3'])); + $this->assertSame('winter_cms_4', Str::unique('winter_cms', ['winter_cms_1', 'test_5', 'winter_cms_3'])); $this->assertSame('winter_cms_98', Str::unique('winter_cms', ['winter_cms_97', 'test_5', 'winter_cms_3'])); - $this->assertSame('winter_cms 1', Str::unique('winter_cms', ['winter_cms_1', 'test_5', 'winter_cms_3'], ' ')); - $this->assertSame('winter_cms_1', Str::unique('winter_cms', ['test_5'])); + $this->assertSame('winter_cms 1', Str::unique('winter_cms', ['winter_cms_1', 'test_5', 'winter_cms_3'], ' ')); + $this->assertSame('winter_cms_1', Str::unique('winter_cms', ['test_5'])); } public function testUniqueFile() { - $this->assertSame('winter_4.cms', Str::uniqueFile('winter.cms', ['winter_1.cms', 'test_5', 'winter_3.cms'])); + $this->assertSame('winter_4.cms', Str::uniqueFile('winter.cms', ['winter_1.cms', 'test_5', 'winter_3.cms'])); $this->assertSame('winter_98.cms', Str::uniqueFile('winter.cms', ['winter_97.cms', 'test_5', 'winter_1.cms'])); - $this->assertSame('winter 1.cms', Str::uniqueFile('winter.cms', ['winter_1.cms', 'test_5', 'winter_3.cms'], ' ')); - $this->assertSame('winter_1.cms', Str::uniqueFile('winter.cms', ['test_5'])); + $this->assertSame('winter 1.cms', Str::uniqueFile('winter.cms', ['winter_1.cms', 'test_5', 'winter_3.cms'], ' ')); + $this->assertSame('winter_1.cms', Str::uniqueFile('winter.cms', ['test_5'])); } } From ad96e3b0e5dd65638105c8a88c50c9c30ffb7dbd Mon Sep 17 00:00:00 2001 From: Jack Wilkinson Date: Mon, 19 Sep 2022 17:51:41 +0100 Subject: [PATCH 04/11] Moved uniqueFile method to filesystem class --- src/Filesystem/Filesystem.php | 28 ++++++++++++++++++++++++++++ src/Support/Str.php | 28 ---------------------------- tests/Filesystem/FilesystemTest.php | 16 ++++++++++++++++ tests/Support/StrTest.php | 8 -------- 4 files changed, 44 insertions(+), 36 deletions(-) create mode 100644 tests/Filesystem/FilesystemTest.php diff --git a/src/Filesystem/Filesystem.php b/src/Filesystem/Filesystem.php index ba8c3df9c..f1102a530 100644 --- a/src/Filesystem/Filesystem.php +++ b/src/Filesystem/Filesystem.php @@ -151,6 +151,34 @@ public function isLocalDisk(\Illuminate\Filesystem\FilesystemAdapter $disk): boo return ($disk->getAdapter() instanceof \League\Flysystem\Local\LocalFilesystemAdapter); } + /** + * Apply a unique index to a filename from provided list i.e. + * winter.txt, [winter_1.txt, winter_2.txt] -> winter_3.txt + * winter.txt, [winter_1.txt, winter_3.txt] -> winter_4.txt + */ + public function unique(string $str, array $list, string $separator = '_', int $starting = 1, int $step = 1): string + { + $indexes = []; + + $info = pathinfo($str); + + if (empty($info['filename']) || empty($info['extension'])) { + throw new \InvalidArgumentException('$str must be a file name'); + } + + foreach ($list as $item) { + if (!preg_match('/' . $info['filename'] . $separator . '(\d*)\.' . $info['extension'] . '/', $item, $matches)) { + continue; + } + + $indexes[] = (int) $matches[1]; + } + + return empty($indexes) + ? $info['filename'] . $separator . $starting . '.' . $info['extension'] + : $info['filename'] . $separator . (max($indexes) + $step) . '.' . $info['extension']; + } + /** * Finds the path of a given class. * diff --git a/src/Support/Str.php b/src/Support/Str.php index 1edb34f63..93a7a2668 100644 --- a/src/Support/Str.php +++ b/src/Support/Str.php @@ -121,34 +121,6 @@ public static function unique(string $str, array $list, string $separator = '_', : $str . $separator . (max($indexes) + $step); } - /** - * Apply a unique index to a filename from provided list i.e. - * winter.txt, [winter_1.txt, winter_2.txt] -> winter_3.txt - * winter.txt, [winter_1.txt, winter_3.txt] -> winter_4.txt - */ - public static function uniqueFile(string $str, array $list, string $separator = '_', int $starting = 1, int $step = 1): string - { - $indexes = []; - - $info = pathinfo($str); - - if (empty($info['filename']) || empty($info['extension'])) { - throw new \InvalidArgumentException('$str must be a file name'); - } - - foreach ($list as $item) { - if (!preg_match('/' . $info['filename'] . $separator . '(\d*)\.' . $info['extension'] . '/', $item, $matches)) { - continue; - } - - $indexes[] = (int) $matches[1]; - } - - return empty($indexes) - ? $info['filename'] . $separator . $starting . '.' . $info['extension'] - : $info['filename'] . $separator . (max($indexes) + $step) . '.' . $info['extension']; - } - /** * Converts line breaks to a standard \r\n pattern. */ diff --git a/tests/Filesystem/FilesystemTest.php b/tests/Filesystem/FilesystemTest.php new file mode 100644 index 000000000..6b6741219 --- /dev/null +++ b/tests/Filesystem/FilesystemTest.php @@ -0,0 +1,16 @@ +assertSame('winter_4.cms', $filesystem->unique('winter.cms', ['winter_1.cms', 'test_5', 'winter_2.cms'])); + $this->assertSame('winter_98.cms', $filesystem->unique('winter.cms', ['winter_97.cms', 'test_5', 'winter_1.cms'])); + $this->assertSame('winter 1.cms', $filesystem->unique('winter.cms', ['winter_1.cms', 'test_5', 'winter_3.cms'], ' ')); + $this->assertSame('winter_1.cms', $filesystem->unique('winter.cms', ['test_5'])); + } +} diff --git a/tests/Support/StrTest.php b/tests/Support/StrTest.php index 632616bdf..1f2685aee 100644 --- a/tests/Support/StrTest.php +++ b/tests/Support/StrTest.php @@ -37,12 +37,4 @@ public function testUnique() $this->assertSame('winter_cms 1', Str::unique('winter_cms', ['winter_cms_1', 'test_5', 'winter_cms_3'], ' ')); $this->assertSame('winter_cms_1', Str::unique('winter_cms', ['test_5'])); } - - public function testUniqueFile() - { - $this->assertSame('winter_4.cms', Str::uniqueFile('winter.cms', ['winter_1.cms', 'test_5', 'winter_2.cms'])); - $this->assertSame('winter_98.cms', Str::uniqueFile('winter.cms', ['winter_97.cms', 'test_5', 'winter_1.cms'])); - $this->assertSame('winter 1.cms', Str::uniqueFile('winter.cms', ['winter_1.cms', 'test_5', 'winter_3.cms'], ' ')); - $this->assertSame('winter_1.cms', Str::uniqueFile('winter.cms', ['test_5'])); - } } From ebc6c4b2ac24ee4246ab60d67f8b2ab876a85b84 Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Tue, 20 Sep 2022 16:02:31 -0600 Subject: [PATCH 05/11] Add Arr::moveKeyToIndex() helper Adds the Arr::moveKeyToIndex($array, $targetKey, $index) helper for moving --- src/Support/Arr.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/Support/Arr.php b/src/Support/Arr.php index ea94efd45..23d2a4773 100644 --- a/src/Support/Arr.php +++ b/src/Support/Arr.php @@ -1,6 +1,7 @@ Date: Tue, 20 Sep 2022 16:04:01 -0600 Subject: [PATCH 06/11] Cleanup docblocks --- src/Support/Arr.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Support/Arr.php b/src/Support/Arr.php index 23d2a4773..3583fc673 100644 --- a/src/Support/Arr.php +++ b/src/Support/Arr.php @@ -6,18 +6,14 @@ /** * Array helper * - * @author Alexey Bobkov, Samuel Georges + * @author Winter CMS */ class Arr extends ArrHelper { /** * Build a new array using a callback. - * - * @param array $array - * @param callable $callback - * @return array */ - public static function build(array $array, callable $callback) + public static function build(array $array, callable $callback): array { $results = []; @@ -41,7 +37,7 @@ public static function moveKeyToIndex(array $array, string|int $targetKey, int $ } if (!array_key_exists($targetKey, $array)) { - throw new \InvalidArgumentException(sprintf('Key "%s" does not exist in the array', $targetKey)); + throw new InvalidArgumentException(sprintf('Key "%s" does not exist in the array', $targetKey)); } $keys = array_diff(array_keys($array), [$targetKey]); From e4a28c3816268e3d426be93ca8f5a3770415bb99 Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Tue, 20 Sep 2022 21:05:48 -0600 Subject: [PATCH 07/11] Add test cases for Arr::moveKeyToIndex() --- tests/Support/ArrTest.php | 46 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tests/Support/ArrTest.php b/tests/Support/ArrTest.php index b01a37bdb..29d6b737b 100644 --- a/tests/Support/ArrTest.php +++ b/tests/Support/ArrTest.php @@ -4,6 +4,52 @@ class ArrTest extends TestCase { + public function testMoveKeyToIndex() + { + $array = [ + 'one' => 'a', + 'two' => 'b', + 'three' => 'c', + ]; + + // 0 based index means that index 1 is the second element + $this->assertSame([ + 'one' => 'a', + 'three' => 'c', + 'two' => 'b', + ], Arr::moveKeyToIndex($array, 'three', 1)); + + // 0 index inserts at start + $this->assertSame([ + 'two' => 'b', + 'one' => 'a', + 'three' => 'c', + ], Arr::moveKeyToIndex($array, 'two', 0)); + + // Index out of range inserts at end + $this->assertSame([ + 'one' => 'a', + 'three' => 'c', + 'two' => 'b', + ], Arr::moveKeyToIndex($array, 'two', 10)); + + // Negative index inserting as first element + $this->assertSame([ + 'two' => 'b', + 'one' => 'a', + 'three' => 'c', + ], Arr::moveKeyToIndex($array, 'two', -10)); + + // Elements with null values are correctly able to be sorted + $nullValueArray = $array; + $nullValueArray['two'] = null; + $this->assertSame([ + 'one' => 'a', + 'three' => 'c', + 'two' => null, + ], Arr::moveKeyToIndex($nullValueArray, 'two', 2)); + } + public function testArrClass() { $array = [ From 68bb7acc6c0a1377b375d3dc9052310a9d8d02d8 Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Tue, 20 Sep 2022 22:25:25 -0600 Subject: [PATCH 08/11] Remove File::unique() and Str::index() helpers for now Also modified Str::unique() to allow for the unindexed string to be used if it is unique. --- src/Filesystem/Filesystem.php | 28 -------------- src/Support/Str.php | 69 ++++++++++++++++------------------- tests/Support/StrTest.php | 31 +++++++--------- 3 files changed, 44 insertions(+), 84 deletions(-) diff --git a/src/Filesystem/Filesystem.php b/src/Filesystem/Filesystem.php index f1102a530..ba8c3df9c 100644 --- a/src/Filesystem/Filesystem.php +++ b/src/Filesystem/Filesystem.php @@ -151,34 +151,6 @@ public function isLocalDisk(\Illuminate\Filesystem\FilesystemAdapter $disk): boo return ($disk->getAdapter() instanceof \League\Flysystem\Local\LocalFilesystemAdapter); } - /** - * Apply a unique index to a filename from provided list i.e. - * winter.txt, [winter_1.txt, winter_2.txt] -> winter_3.txt - * winter.txt, [winter_1.txt, winter_3.txt] -> winter_4.txt - */ - public function unique(string $str, array $list, string $separator = '_', int $starting = 1, int $step = 1): string - { - $indexes = []; - - $info = pathinfo($str); - - if (empty($info['filename']) || empty($info['extension'])) { - throw new \InvalidArgumentException('$str must be a file name'); - } - - foreach ($list as $item) { - if (!preg_match('/' . $info['filename'] . $separator . '(\d*)\.' . $info['extension'] . '/', $item, $matches)) { - continue; - } - - $indexes[] = (int) $matches[1]; - } - - return empty($indexes) - ? $info['filename'] . $separator . $starting . '.' . $info['extension'] - : $info['filename'] . $separator . (max($indexes) + $step) . '.' . $info['extension']; - } - /** * Finds the path of a given class. * diff --git a/src/Support/Str.php b/src/Support/Str.php index 93a7a2668..4785ff416 100644 --- a/src/Support/Str.php +++ b/src/Support/Str.php @@ -4,8 +4,6 @@ /** * String helper - * - * @author Alexey Bobkov, Samuel Georges */ class Str extends StrHelper { @@ -85,42 +83,6 @@ public static function join(iterable $items, string $glue = ', ', string $lastGl return $result; } - /** - * Apply an index to a string, i.e. - * winter -> winter_1 - * winter_1 -> winter_2 - */ - public static function index(string $str, string $separator = '_', int $starting = 1, int $step = 1): string - { - if (!preg_match('/(.*?)' . $separator . '(\d*$)/', $str, $matches)) { - return $str . $separator . $starting; - } - - return $matches[1] . $separator . (((int) $matches[2]) + $step); - } - - /** - * Apply a unique index to a string from provided list i.e. - * winter, [winter_1, winter_2] -> winter_3 - * winter, [winter_1, winter_3] -> winter_4 - */ - public static function unique(string $str, array $list, string $separator = '_', int $starting = 1, int $step = 1): string - { - $indexes = []; - - foreach ($list as $item) { - if (!preg_match('/(.*?)' . $str . $separator . '(\d*$)/', $item, $matches)) { - continue; - } - - $indexes[] = (int) $matches[2]; - } - - return empty($indexes) - ? $str . $separator . $starting - : $str . $separator . (max($indexes) + $step); - } - /** * Converts line breaks to a standard \r\n pattern. */ @@ -167,4 +129,35 @@ public static function ordinal($number) return $number.'th'; } } + + /** + * Ensures that the provide string will be unique within the provided array, + * adjusts it with the separator & step as necessary if not + * + * Examples: + * winter, [winter, winter_1, winter_2] -> winter_3 + * winter, [winter_1, winter_3] -> winter + */ + public static function unique(string $str, array $items, string $separator = '_', int $step = 1): string + { + $indexes = []; + + if (!in_array($str, $items)) { + return $str; + } else { + $indexes[] = 0; + } + + foreach ($items as $item) { + if (!preg_match('/(.*?)' . $str . $separator . '(\d*$)/', $item, $matches)) { + continue; + } + + $indexes[] = (int) $matches[2]; + } + + return empty($indexes) + ? $str + : $str . $separator . (max($indexes) + $step); + } } diff --git a/tests/Support/StrTest.php b/tests/Support/StrTest.php index 5dc514454..3abd67793 100644 --- a/tests/Support/StrTest.php +++ b/tests/Support/StrTest.php @@ -16,25 +16,20 @@ public function testJoin() $this->assertSame('bob; joe; or sally', Str::join(['bob', 'joe', 'sally'], '; ', '; or ')); } - public function testIndex() - { - $this->assertSame('winter_cms_1', Str::index('winter_cms')); - $this->assertSame('winter_cms_2', Str::index('winter_cms_1')); - $this->assertSame('winter_cms_43', Str::index('winter_cms_42')); - $this->assertSame('winter_cms 3', Str::index('winter_cms 2', separator: ' ')); - $this->assertSame('winter_cms 6', Str::index('winter_cms 2', separator: ' ', step: 4)); - $this->assertSame('winter_cms8', Str::index('winter_cms4', separator: '', step: 4)); - $this->assertSame('winter_cms4_1', Str::index('winter_cms4', step: 4)); - $this->assertSame('winter_cms-22', Str::index('winter_cms', separator: '-', starting: 22)); - $this->assertSame('winter cms 1', Str::index('winter cms', separator: ' ')); - $this->assertSame('winter cms 2', Str::index('winter cms 1', separator: ' ')); - } - public function testUnique() { - $this->assertSame('winter_cms_4', Str::unique('winter_cms', ['winter_cms_1', 'test_5', 'winter_cms_3'])); - $this->assertSame('winter_cms_98', Str::unique('winter_cms', ['winter_cms_97', 'test_5', 'winter_cms_3'])); - $this->assertSame('winter_cms 1', Str::unique('winter_cms', ['winter_cms_1', 'test_5', 'winter_cms_3'], ' ')); - $this->assertSame('winter_cms_1', Str::unique('winter_cms', ['test_5'])); + // Original returned unmodified when already unique + $this->assertSame('winter_cms', Str::unique('winter_cms', [])); + $this->assertSame('winter_cms', Str::unique('winter_cms', ['winter_cms_1', 'winter_cms_2'])); + + // // String modified to be the default step higher than the highest index identified + $this->assertSame('winter_cms_1', Str::unique('winter_cms', ['winter_cms'])); + $this->assertSame('winter_cms_4', Str::unique('winter_cms', ['winter_cms', 'winter_cms_1', 'test_5', 'winter_cms_3'])); + + // String modified to be the default step higher than the highest index identified with reversed order of items + $this->assertSame('winter_cms_98', Str::unique('winter_cms', ['winter_cms', 'winter_cms_97', 'test_5', 'winter_cms_3'])); + + // String modified to be the provided step higher than the highest index identified with the provided separator + $this->assertSame('winter_cms 5', Str::unique('winter_cms', ['winter_cms', 'winter_cms 1', 'test_5', 'winter_cms 3'], ' ', 2)); } } From 76a4302553cf68a3c955a320a392c8b61bf0e3ba Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Tue, 20 Sep 2022 22:26:04 -0600 Subject: [PATCH 09/11] Remove Filesystem test for now --- tests/Filesystem/FilesystemTest.php | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 tests/Filesystem/FilesystemTest.php diff --git a/tests/Filesystem/FilesystemTest.php b/tests/Filesystem/FilesystemTest.php deleted file mode 100644 index f462f559b..000000000 --- a/tests/Filesystem/FilesystemTest.php +++ /dev/null @@ -1,16 +0,0 @@ -assertSame('winter_4.cms', $filesystem->unique('winter.cms', ['winter_1.cms', 'test_5', 'winter_3.cms'])); - $this->assertSame('winter_98.cms', $filesystem->unique('winter.cms', ['winter_97.cms', 'test_5', 'winter_1.cms'])); - $this->assertSame('winter 1.cms', $filesystem->unique('winter.cms', ['winter_1.cms', 'test_5', 'winter_3.cms'], ' ')); - $this->assertSame('winter_1.cms', $filesystem->unique('winter.cms', ['test_5'])); - } -} From 15e0be2f1cbaee98304c721cd030aef48ff7fb7c Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Wed, 21 Sep 2022 10:33:02 -0600 Subject: [PATCH 10/11] Support negative index working backwards from end of array for Arr::moveKeyToIndex() --- src/Support/Arr.php | 4 ---- tests/Support/ArrTest.php | 9 ++++++++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Support/Arr.php b/src/Support/Arr.php index 3583fc673..877879926 100644 --- a/src/Support/Arr.php +++ b/src/Support/Arr.php @@ -32,10 +32,6 @@ public static function build(array $array, callable $callback): array */ public static function moveKeyToIndex(array $array, string|int $targetKey, int $index): array { - if ($index < 0) { - $index = 0; - } - if (!array_key_exists($targetKey, $array)) { throw new InvalidArgumentException(sprintf('Key "%s" does not exist in the array', $targetKey)); } diff --git a/tests/Support/ArrTest.php b/tests/Support/ArrTest.php index 29d6b737b..2acde7bf1 100644 --- a/tests/Support/ArrTest.php +++ b/tests/Support/ArrTest.php @@ -33,7 +33,14 @@ public function testMoveKeyToIndex() 'two' => 'b', ], Arr::moveKeyToIndex($array, 'two', 10)); - // Negative index inserting as first element + // Negative index works backwards + $this->assertSame([ + 'two' => 'b', + 'one' => 'a', + 'three' => 'c', + ], Arr::moveKeyToIndex($array, 'two', -2)); + + // Negative index beyond bounds inserting as first element $this->assertSame([ 'two' => 'b', 'one' => 'a', From c69d65cba1349a8d25c2c84be96c35c513e57066 Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Wed, 21 Sep 2022 10:33:54 -0600 Subject: [PATCH 11/11] Docblock update --- src/Support/Arr.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Support/Arr.php b/src/Support/Arr.php index 877879926..29d05ed71 100644 --- a/src/Support/Arr.php +++ b/src/Support/Arr.php @@ -27,7 +27,7 @@ public static function build(array $array, callable $callback): array } /** - * Moves the key to the index within the array + * Moves the key to the index within the array, negative index will work backwards from the end of the array * @throws InvalidArgumentException if the key does not exist in the array */ public static function moveKeyToIndex(array $array, string|int $targetKey, int $index): array