Skip to content

Commit

Permalink
add hasMedia convenience method + psr2
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Jul 30, 2015
1 parent 51029ea commit 3b4006a
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All Notable changes to `laravel-medialibrary` will be documented in this file

##2.3.0
- Added: hasMedia convenience method

##2.2.3
- Fixed: when renaming file_name on a media object the orginal file gets renamed as well

Expand Down
2 changes: 1 addition & 1 deletion src/FileManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function createDerivedFiles(Media $media)
return;
}

if ($media->type == Media::TYPE_PDF && ! class_exists('Imagick')) {
if ($media->type == Media::TYPE_PDF && !class_exists('Imagick')) {
return;
}

Expand Down
12 changes: 12 additions & 0 deletions src/HasMediaTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ public function addMedia($file, $collectionName = 'default', $removeOriginal = t
return $media;
}

/**
* Determine if there is media in the given collection.
*
* @param $collectionName
*
* @return bool
*/
public function hasMedia($collectionName = 'default')
{
return count($this->getMedia($collectionName)) ? true : false;
}

/**
* Get media collection by its collectionName.
*
Expand Down
9 changes: 9 additions & 0 deletions src/HasMediaWithoutConversions.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ public function media();
*/
public function addMedia($file, $collectionName = 'default', $preserveOriginal = false, $addAsTemporary = false);

/**
* Determine if there is media in the given collection.
*
* @param $collectionMedia
*
* @return bool
*/
public function hasMedia($collectionMedia = 'default');

/**
* Get media collection by its collectionName.
*
Expand Down
45 changes: 45 additions & 0 deletions tests/HasMediaTrait/HasMediaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Spatie\MediaLibrary\Test\HasMediaTrait;

use Spatie\MediaLibrary\Test\TestCase;

class HasMediaTest extends TestCase
{
/**
* @test
*/
public function it_returns_false_for_an_empty_collection()
{
$this->assertFalse($this->testModel->hasMedia());
}

/**
* @test
*/
public function it_returns_true_for_a_non_empty_collection()
{
$this->testModel->addMedia($this->getTestFilesDirectory('test.jpg'));

$this->assertTrue($this->testModel->hasMedia());
}

/**
* @test
*/
public function it_returns_false_for_an_empty_named_collection()
{
$this->assertFalse($this->testModel->hasMedia('images'));
}

/**
* @test
*/
public function it_returns_true_for_a_non_empty_named_collection()
{
$this->testModel->addMedia($this->getTestFilesDirectory('test.jpg'), 'images');

$this->assertTrue($this->testModel->hasMedia('images'));
$this->assertFalse($this->testModel->hasMedia('downloads'));
}
}
2 changes: 1 addition & 1 deletion tests/Performance/PerformanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function it_will_can_use_eagerly_loaded_media()
DB::connection()->enableQueryLog();

$testModels = $this->testModelWithConversion->get();
$testModels->load("media");
$testModels->load('media');

foreach ($testModels as $testModel) {
$testModel->getFirstMediaUrl('images', 'thumb');
Expand Down
1 change: 1 addition & 0 deletions tests/TestModel.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Spatie\MediaLibrary\Test;

use Illuminate\Database\Eloquent\Model;
Expand Down
1 change: 1 addition & 0 deletions tests/TestModelWithConversion.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Spatie\MediaLibrary\Test;

class TestModelWithConversion extends TestModel
Expand Down
1 change: 1 addition & 0 deletions tests/TestModelWithoutMediaConversions.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Spatie\MediaLibrary\Test;

use Illuminate\Database\Eloquent\Model;
Expand Down

0 comments on commit 3b4006a

Please sign in to comment.