Skip to content

Commit

Permalink
add custom property convenience methods
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Sep 29, 2015
1 parent 455d554 commit 3e889ef
Show file tree
Hide file tree
Showing 5 changed files with 86 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

##3.3.0
- Added `hasCustomProperty`- and `getCustomProperty`-convenience-methods

##3.2.5
- Allow 0 for `x` and `y` parameters in `setRectangle`

Expand Down
2 changes: 1 addition & 1 deletion src/Conversion/Conversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public function setFit($fit)
public function setRectangle($width, $height, $x, $y)
{
foreach (compact('width', 'height', 'x', 'y') as $name => $value) {
if (! is_numeric($value)) {
if (!is_numeric($value)) {
throw new InvalidConversionParameter($name.' should be numeric');
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Helpers/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function getHumanReadableSize($sizeInBytes)
return '0 '.$units[1];
}

for ($i = 0; $sizeInBytes > 1024; $i++) {
for ($i = 0; $sizeInBytes > 1024; ++$i) {
$sizeInBytes /= 1024;
}

Expand Down
30 changes: 30 additions & 0 deletions src/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public function getExtensionAttribute()
return pathinfo($this->file_name, PATHINFO_EXTENSION);
}

/**
/**
* @return string
*/
Expand All @@ -115,8 +116,37 @@ public function getHumanReadableSizeAttribute()
return File::getHumanReadableSize($this->size);
}

/**
* @return string
*/
public function getDiskDriverName()
{
return config('filesystems.disks.'.$this->disk.'.driver');
}

/**
* Determine if the media item has a custom property with the given name.
*
* @return bool
*/
public function hasCustomProperty($propertyName)
{
return array_key_exists($propertyName, $this->custom_properties);
}

/**
* Get if the value of custom property with the given name.
*
* @param $propertyName
*
* @return mixed
*/
public function getCustomProperty($propertyName)
{
if (!$this->hasCustomProperty($propertyName)) {
return;
}

return $this->custom_properties[$propertyName];
}
}
51 changes: 51 additions & 0 deletions tests/Media/CustomPropertyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Spatie\MediaLibrary\Test\Media;

use Spatie\MediaLibrary\Test\TestCase;

class CustomPropertyTest extends TestCase
{
protected $mediaWithCustomProperty;
protected $mediaWithoutCustomProperty;

public function setUp()
{
parent::setUp();

$this->mediaWithCustomProperty = $this->testModel
->addMedia($this->getTestJpg())
->preservingOriginal()
->withCustomProperties(['customName' => 'customValue'])
->toMediaLibrary('images');

$this->mediaWithoutCustomProperty = $this->testModel
->addMedia($this->getTestJpg())
->preservingOriginal()
->toMediaLibrary('images');
}

/**
* @test
*/
public function it_can_determine_if_a_media_item_has_custom_property()
{
$this->assertTrue($this->mediaWithCustomProperty->hasCustomProperty('customName'));
$this->assertFalse($this->mediaWithCustomProperty->hasCustomProperty('nonExisting'));

$this->assertFalse($this->mediaWithoutCustomProperty->hasCustomProperty('customName'));
$this->assertFalse($this->mediaWithoutCustomProperty->hasCustomProperty('nonExisting'));
}

/**
* @test
*/
public function it_can_get_a_custom_property_of_a_media_item()
{
$this->assertEquals('customValue', $this->mediaWithCustomProperty->getCustomProperty('customName'));
$this->assertNull($this->mediaWithCustomProperty->getCustomProperty('nonExisting'));

$this->assertNull($this->mediaWithoutCustomProperty->getCustomProperty('customName'));
$this->assertNull($this->mediaWithoutCustomProperty->getCustomProperty('nonExisting'));
}
}

0 comments on commit 3e889ef

Please sign in to comment.