Skip to content

Commit

Permalink
add filter param to hasMedia() (#2648)
Browse files Browse the repository at this point in the history
Co-authored-by: Frank Heider <[email protected]>
  • Loading branch information
fheider and Frank Heider authored Dec 9, 2021
1 parent 797d4c9 commit 9b0371e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/HasMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function addMediaFromDisk(string $key, string $disk = null): FileAdder;
*/
public function copyMedia($file): FileAdder;

public function hasMedia(string $collectionMedia = ''): bool;
public function hasMedia(string $collectionName = ''): bool;

/**
* Get media collection by its collectionName.
Expand Down
4 changes: 2 additions & 2 deletions src/InteractsWithMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,9 @@ public function copyMedia($file): FileAdder
/*
* Determine if there is media in the given collection.
*/
public function hasMedia(string $collectionName = 'default'): bool
public function hasMedia(string $collectionName = 'default', array $filters = []): bool
{
return count($this->getMedia($collectionName)) ? true : false;
return count($this->getMedia($collectionName, $filters)) ? true : false;
}

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/Feature/InteractsWithMedia/HasMediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,17 @@ public function it_returns_true_for_a_non_empty_named_collection()
$this->assertTrue($this->testModel->hasMedia('images'));
$this->assertFalse($this->testModel->hasMedia('downloads'));
}

/** @test */
public function it_returns_true_for_a_filtered_collection()
{
$this->testModel
->addMedia($this->getTestJpg())
->withCustomProperties(['test' => true])
->toMediaCollection();

$this->assertTrue($this->testModel->hasMedia('default'));
$this->assertTrue($this->testModel->hasMedia('default', ['test' => true]));
$this->assertFalse($this->testModel->hasMedia('default', ['test' => false]));
}
}

0 comments on commit 9b0371e

Please sign in to comment.