Skip to content

Commit

Permalink
Remove File::unique() and Str::index() helpers for now
Browse files Browse the repository at this point in the history
Also modified Str::unique() to allow for the unindexed string to be used if it is unique.
  • Loading branch information
LukeTowers committed Sep 21, 2022
1 parent e4a28c3 commit 68bb7ac
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 84 deletions.
28 changes: 0 additions & 28 deletions src/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
69 changes: 31 additions & 38 deletions src/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

/**
* String helper
*
* @author Alexey Bobkov, Samuel Georges
*/
class Str extends StrHelper
{
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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);
}
}
31 changes: 13 additions & 18 deletions tests/Support/StrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 68bb7ac

Please sign in to comment.