Skip to content

Commit

Permalink
Expanding the Factories to better support Image generation and adding…
Browse files Browse the repository at this point in the history
… properties to the relative objects to provide this functionality ( e.g. posterPath has a cousin poster now which holds a true Image object ).
  • Loading branch information
wtfzdotnet committed Jan 14, 2014
1 parent 850977d commit 27ef5db
Show file tree
Hide file tree
Showing 12 changed files with 263 additions and 16 deletions.
4 changes: 1 addition & 3 deletions examples/movies/model/get.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,4 @@
$repository = new \Tmdb\Repository\MovieRepository($client);
$movie = $repository->load(87421);

printf('<h1>%s</h1>', $movie->getTitle());

printf('<p>%s</p>', $movie->getOverview());
var_dump($movie);
2 changes: 0 additions & 2 deletions examples/tv/model/show.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,4 @@
$repository = new \Tmdb\Repository\TvRepository($client);
$tvShow = $repository->load(1396);

printf('<h1>%s</h1>', $tvShow->getName());

var_dump($tvShow);
106 changes: 98 additions & 8 deletions lib/Tmdb/Factory/ImageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,110 @@ class ImageFactory extends AbstractFactory
/**
* {@inheritdoc}
*/
public static function create(array $data = array())
public static function create(array $data = array(), $key = null)
{
return parent::hydrate(new Image(), $data);
$type = self::resolveImageType($key);

if (is_string($data)) {
$data = array(
'file_path' => $data
);
}

return parent::hydrate($type, $data);
}

/**
* Create an image instance based on the path and type, e.g.
*
* '/xkQ5yWnMjpC2bGmu7GsD66AAoKO.jpg', 'backdrop_path'
*
* @param $path
* @param $key
* @return \Tmdb\Model\Image
*/
public static function createFromPath($path, $key)
{
return parent::hydrate(
self::resolveImageType($key),
array('file_path' => $path)
);
}

/**
* Return possible image type keys
*
* @return array
*/
public static function getPossibleKeys()
{
return array(
'poster',
'posters',
'poster_path',
'backdrop',
'backdrops',
'backdrop_path',
'profile',
'profiles',
'profile_path',
'logo',
'logos',
'logo_path',
);
}

private function resolveImageType($key = null)
{
switch($key) {
case 'poster':
case 'posters':
case 'poster_path':
$object = new Image\PosterImage();
break;

case 'backdrop':
case 'backdrops':
case 'backdrop_path':
$object = new Image\BackdropImage();
break;

case 'profile':
case 'profiles':
case 'profile_path':
$object = new Image\ProfileImage();
break;

case 'logo':
case 'logos':
case 'logo_path':
$object = new Image\LogoImage();
break;

case 'still':
case 'stills':
case 'still_path':
$object = new Image\StillImage();
break;

case 'file_path':
default:
$object = new Image();
break;
}

return $object;
}

/**
* {@inheritdoc}
*/
public static function createCollection(array $data = array())
{
$collection = new Images();
$collection = new Images();

foreach($data as $item) {
$collection->add(null, self::create($item));
$collection->addObject(self::create($item));
}

return $collection;
Expand All @@ -44,19 +134,19 @@ public static function createCollection(array $data = array())
*/
public static function createImageCollection(array $data = array())
{
$collection = array();
$collection = new Images();

foreach($data as $format => $formatCollection) {
foreach($formatCollection as $item) {
if (array_key_exists($format, Image::$_formats)) {
$item['format'] = Image::$_formats[$format];
$item = self::create($item, $format);

$collection[] = $item;
$collection->addObject($item);
}
}
}

return self::createCollection($collection);
return $collection;
}

/**
Expand Down
10 changes: 9 additions & 1 deletion lib/Tmdb/Factory/MovieFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,18 @@ public static function create(array $data = array())
}

/** Images */
if (array_key_exists('backdrop_path', $data)) {
$movie->setBackdrop(ImageFactory::createFromPath($data['backdrop_path'], 'backdrop_path'));
}

if (array_key_exists('images', $data)) {
$movie->setImages(ImageFactory::createCollectionFromMovie($data['images']));
}

if (array_key_exists('poster_path', $data)) {
$movie->setPoster(ImageFactory::createFromPath($data['poster_path'], 'poster_path'));
}

/** Keywords */
if (array_key_exists('keywords', $data)) {
$movie->setKeywords(GenericCollectionFactory::createCollection($data['keywords']['keywords'], new Movie\Keyword()));
Expand All @@ -79,7 +87,7 @@ public static function create(array $data = array())
}

if (array_key_exists('similar_movies', $data)) {
$movie->setSimilarMovies(GenericCollectionFactory::createCollection($data['similar_movies']['results'], new Movie()));
$movie->setSimilarMovies(self::createCollection($data['similar_movies']['results']));
}

// if (array_key_exists('reviews', $data)) {
Expand Down
4 changes: 4 additions & 0 deletions lib/Tmdb/Factory/People/PeopleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public static function create(array $data = array(), Person\AbstractMember $pers
$person->setImages(ImageFactory::createCollectionFromPeople($data['images']));
}

if (array_key_exists('profile_path', $data)) {
$person->setProfile(ImageFactory::createFromPath($data['profile_path'], 'profile_path'));
}

return parent::hydrate($person, $data);
}

Expand Down
17 changes: 17 additions & 0 deletions lib/Tmdb/Model/Image/BackdropImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Model\Image;

use Tmdb\Model\Image;

class BackdropImage extends Image {}
17 changes: 17 additions & 0 deletions lib/Tmdb/Model/Image/LogoImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Model\Image;

use Tmdb\Model\Image;

class LogoImage extends Image {}
17 changes: 17 additions & 0 deletions lib/Tmdb/Model/Image/PosterImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Model\Image;

use Tmdb\Model\Image;

class PosterImage extends Image {}
17 changes: 17 additions & 0 deletions lib/Tmdb/Model/Image/ProfileImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Model\Image;

use Tmdb\Model\Image;

class ProfileImage extends Image {}
17 changes: 17 additions & 0 deletions lib/Tmdb/Model/Image/StillImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Model\Image;

use Tmdb\Model\Image;

class StillImage extends Image {}
49 changes: 47 additions & 2 deletions lib/Tmdb/Model/Movie.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ class Movie extends AbstractModel {
private $adult = false;

/**
* @var Image
* @var string
*/
private $backdropPath;

/**
* @var Image
*/
private $backdrop;

/**
* @var Collection
*/
Expand Down Expand Up @@ -89,6 +94,11 @@ class Movie extends AbstractModel {
/**
* @var Image
*/
private $poster;

/**
* @var string
*/
private $posterPath;

/**
Expand Down Expand Up @@ -238,7 +248,6 @@ public function __construct()
$this->genres = new Genres();
$this->productionCompanies = new Collection();
$this->productionCountries = new Collection();
$this->releaseDate = new Collection();
$this->spokenLanguages = new Collection();
$this->alternativeTitles = new Collection();
$this->changes = new Collection();
Expand Down Expand Up @@ -883,4 +892,40 @@ public function getTranslations()
{
return $this->translations;
}

/**
* @param \Tmdb\Model\Image $backdrop
* @return $this
*/
public function setBackdrop($backdrop)
{
$this->backdrop = $backdrop;
return $this;
}

/**
* @return \Tmdb\Model\Image
*/
public function getBackdrop()
{
return $this->backdrop;
}

/**
* @param \Tmdb\Model\Image $poster
* @return $this
*/
public function setPoster($poster)
{
$this->poster = $poster;
return $this;
}

/**
* @return \Tmdb\Model\Image
*/
public function getPoster()
{
return $this->poster;
}
}
Loading

0 comments on commit 27ef5db

Please sign in to comment.