Skip to content

Commit

Permalink
Modify behaviour of the ImageHelper, which now supports string input,…
Browse files Browse the repository at this point in the history
… and adding missing credit_id parameters in cast and crewmember objects.
  • Loading branch information
wtfzdotnet committed Mar 6, 2014
2 parents c647533 + 7a21f05 commit 2b28284
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
/phpunit
/public_html
/cache.properties
/tmdb.log
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"monolog/monolog": ">=1.7.0"
},
"suggest": {
"doctrine/cache": "This library is required if you want to make use of caching features.",
"monolog/monolog": "This library is required if you want to make use of logging features."
"doctrine/cache": "Required if you want to make use of caching features.",
"monolog/monolog": "Required if you want to make use of logging features."
},
"autoload": {
"psr-0": { "Tmdb\\": "lib/" }
Expand Down
50 changes: 26 additions & 24 deletions lib/Tmdb/Helper/ImageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,48 +41,50 @@ public function getImageConfiguration()
/**
* Get the url for the image resource
*
* @param Image $image
* @param string $size
* @param Image|string $image Either an instance of Image or the file_path
* @param string $size
* @return string
*/
public function getUrl(Image $image, $size = 'original')
public function getUrl($image, $size = 'original')
{
$config = $this->getImageConfiguration();

return $config['base_url'] . $size . $image->getFilePath();
return $config['base_url'] . $size . $image;
}

/**
* Get an img html tag for the image in the specified size
*
* @param Image $image
* @param string $size
* @param int|null $width
* @param int|null $height
* @param Image|string $image Either an instance of Image or the file_path
* @param string $size
* @param int|null $width
* @param int|null $height
* @return string
*/
public function getHtml(Image $image, $size = 'original', $width = null, $height = null)
public function getHtml($image, $size = 'original', $width = null, $height = null)
{
if (null == $image->getFilePath()) {
return '';
}
if ($image instanceof Image) {
if (null == $image->getFilePath()) {
return '';
}

$aspectRatio = $image->getAspectRatio();
$aspectRatio = $image->getAspectRatio();

if (null !== $width && null == $height && $aspectRatio !== null) {
$height = round($width / $aspectRatio);
}
if (null !== $width && null == $height && $aspectRatio !== null) {
$height = round($width / $aspectRatio);
}

if (null !== $height && null == $width && $aspectRatio !== null) {
$width = round($height * $aspectRatio);
}
if (null !== $height && null == $width && $aspectRatio !== null) {
$width = round($height * $aspectRatio);
}

if (null == $width) {
$width = $image->getWidth();
}
if (null == $width) {
$width = $image->getWidth();
}

if (null == $height) {
$height = $image->getHeight();
if (null == $height) {
$height = $image->getHeight();
}
}

return sprintf(
Expand Down
10 changes: 10 additions & 0 deletions lib/Tmdb/Model/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,14 @@ public function getWidth()
{
return $this->width;
}

/**
* Return the file path when casted to string
*
* @return mixed
*/
public function __toString()
{
return $this->getFilePath();
}
}
25 changes: 25 additions & 0 deletions lib/Tmdb/Model/Person/CastMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ class CastMember extends AbstractMember implements PersonInterface
*/
private $castId;

/**
* @var mixed
*/
private $creditId;

public static $properties = array(
'id',
'credit_id',
'cast_id',
'name',
'character',
Expand Down Expand Up @@ -100,4 +106,23 @@ public function getCastId()
{
return $this->castId;
}

/**
* @param mixed $creditId
* @return $this
*/
public function setCreditId($creditId)
{
$this->creditId = $creditId;

return $this;
}

/**
* @return mixed
*/
public function getCreditId()
{
return $this->creditId;
}
}
25 changes: 25 additions & 0 deletions lib/Tmdb/Model/Person/CrewMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@ class CrewMember extends AbstractMember implements PersonInterface
*/
private $job;

/**
* @var mixed
*/
private $creditId;

public static $properties = array(
'id',
'credit_id',
'name',
'department',
'job',
Expand Down Expand Up @@ -75,4 +81,23 @@ public function getJob()
{
return $this->job;
}

/**
* @param mixed $creditId
* @return $this
*/
public function setCreditId($creditId)
{
$this->creditId = $creditId;

return $this;
}

/**
* @return mixed
*/
public function getCreditId()
{
return $this->creditId;
}
}
23 changes: 23 additions & 0 deletions test/Tmdb/Tests/Helper/ImageHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,27 @@ public function shouldBeEmptyIfFilePathIsNotGiven()

$this->assertEquals('', $this->helper->getHtml($image));
}

/**
* @test
*/
public function shouldGetImageUrlByString()
{
$imageUrl = $this->helper->getUrl('/1NfhdnQAEqcBRCulEhOFSkRrrLv.jpg');

$this->assertEquals('http://image.tmdb.org/t/p/original/1NfhdnQAEqcBRCulEhOFSkRrrLv.jpg', $imageUrl);
}

/**
* @test
*/
public function shouldGetImageElementByString()
{
$imageUrl = $this->helper->getHtml('/1NfhdnQAEqcBRCulEhOFSkRrrLv.jpg');

$this->assertEquals(
'<img src="http://image.tmdb.org/t/p/original/1NfhdnQAEqcBRCulEhOFSkRrrLv.jpg" width="" height="" />',
$imageUrl
);
}
}

0 comments on commit 2b28284

Please sign in to comment.