Skip to content

Commit 4b7a508

Browse files
committed
feat(assets): add unique argument to Asset::move()
Remove moveUnique() in favor of a $unique parameter on move(), consistent with how rename() already works. Update rename() to delegate to move() with the $unique flag.
1 parent 6f7d937 commit 4b7a508

2 files changed

Lines changed: 16 additions & 27 deletions

File tree

src/Assets/Asset.php

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,15 @@ class Asset implements Arrayable, ArrayAccess, AssetContract, Augmentable, Conta
6767
}
6868

6969
protected $container;
70+
7071
protected $path;
72+
7173
protected $meta;
74+
7275
protected $withEvents = true;
76+
7377
protected $shouldHydrate = true;
78+
7479
protected $removedData = [];
7580

7681
public function syncOriginal()
@@ -745,23 +750,21 @@ public function containerHandle()
745750
*/
746751
public function rename($filename, $unique = false)
747752
{
748-
if ($unique) {
749-
return $this->moveUnique($this->folder(), $filename);
750-
}
751-
752-
return $this->move($this->folder(), $filename);
753+
return $this->move($this->folder(), $filename, $unique);
753754
}
754755

755756
/**
756757
* Move the asset to a different location.
757758
*
758759
* @param string $folder The folder relative to the container.
759760
* @param string|null $filename The new filename, if renaming.
761+
* @param bool $unique Whether to ensure the filename is unique.
760762
* @return $this
761763
*/
762-
public function move($folder, $filename = null)
764+
public function move($folder, $filename = null, $unique = false)
763765
{
764766
$filename = Uploader::getSafeFilename($filename ?: $this->filename());
767+
$filename = $unique ? $this->ensureUniqueFilename($folder, $filename) : $filename;
765768
$oldPath = $this->path();
766769
$oldMetaPath = $this->metaPath();
767770
$newPath = Str::removeLeft(Path::tidy($folder.'/'.$filename.'.'.pathinfo($oldPath, PATHINFO_EXTENSION)), '/');
@@ -780,21 +783,6 @@ public function move($folder, $filename = null)
780783
return $this;
781784
}
782785

783-
/**
784-
* Move the asset to a different location with a unique filename.
785-
*
786-
* @param string $folder The folder relative to the container.
787-
* @param string|null $filename The new filename, if renaming.
788-
* @return $this
789-
*/
790-
public function moveUnique($folder, $filename = null)
791-
{
792-
$filename = Uploader::getSafeFilename($filename ?: $this->filename());
793-
$filename = $this->ensureUniqueFilename($folder, $filename);
794-
795-
return $this->move($folder, $filename);
796-
}
797-
798786
public function moveQuietly($folder, $filename = null)
799787
{
800788
$this->withEvents = false;

tests/Assets/AssetTest.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class AssetTest extends TestCase
5050

5151
private $container;
5252

53-
public function setUp(): void
53+
protected function setUp(): void
5454
{
5555
parent::setUp();
5656

@@ -939,7 +939,7 @@ public function it_saves_quietly()
939939
}
940940

941941
#[Test]
942-
public function when_saving_quietly_the_cached_assets_withEvents_flag_will_be_set_back_to_true()
942+
public function when_saving_quietly_the_cached_assets_with_events_flag_will_be_set_back_to_true()
943943
{
944944
Event::fake();
945945
Storage::fake('test');
@@ -1261,7 +1261,7 @@ public function it_doesnt_lowercase_moved_files_when_configured()
12611261
}
12621262

12631263
#[Test]
1264-
public function it_can_be_moved_uniquely_to_another_folder_when_conflict_exists()
1264+
public function it_can_be_moved_to_another_folder_with_a_unique_filename_when_conflict_exists()
12651265
{
12661266
Storage::fake('local');
12671267
$disk = Storage::disk('local');
@@ -1274,7 +1274,7 @@ public function it_can_be_moved_uniquely_to_another_folder_when_conflict_exists(
12741274
$asset = $container->makeAsset('old/asset.txt')->data(['foo' => 'bar']);
12751275
$asset->save();
12761276

1277-
$return = $asset->moveUnique('new');
1277+
$return = $asset->move('new', null, true);
12781278

12791279
$this->assertEquals($asset, $return);
12801280
$disk->assertMissing('old/asset.txt');
@@ -1283,7 +1283,7 @@ public function it_can_be_moved_uniquely_to_another_folder_when_conflict_exists(
12831283
}
12841284

12851285
#[Test]
1286-
public function it_can_be_moved_uniquely_to_another_folder_without_renaming_when_no_conflict()
1286+
public function it_can_be_moved_to_another_folder_with_a_unique_filename_without_renaming_when_no_conflict()
12871287
{
12881288
Storage::fake('local');
12891289
$disk = Storage::disk('local');
@@ -1294,7 +1294,7 @@ public function it_can_be_moved_uniquely_to_another_folder_without_renaming_when
12941294
$asset = $container->makeAsset('old/asset.txt')->data(['foo' => 'bar']);
12951295
$asset->save();
12961296

1297-
$return = $asset->moveUnique('new');
1297+
$return = $asset->move('new', null, true);
12981298

12991299
$this->assertEquals($asset, $return);
13001300
$disk->assertMissing('old/asset.txt');
@@ -2479,6 +2479,7 @@ public function augment($value)
24792479
$relationshipFieldtype = new class extends Fieldtype
24802480
{
24812481
protected static $handle = 'relationship';
2482+
24822483
protected $relationship = true;
24832484

24842485
public function augment($values)

0 commit comments

Comments
 (0)