Skip to content

Commit

Permalink
Refactoring more factory stuff, more or less completed the ImageFacto…
Browse files Browse the repository at this point in the history
…ry from a Movie point of view
  • Loading branch information
wtfzdotnet committed Nov 16, 2013
1 parent c02ce9d commit 9859dbd
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 172 deletions.
95 changes: 95 additions & 0 deletions lib/Tmdb/Factory/AbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
*/
namespace Tmdb\Factory;

use Tmdb\Exception\RuntimeException;
use Tmdb\Model\AbstractModel;
use Tmdb\Model\Common\QueryParameter\QueryParameterInterface;

abstract class AbstractFactory {
/**
* Convert an array to an hydrated object
Expand All @@ -28,4 +32,95 @@ abstract public static function create(array $data = array());
* @return $this
*/
abstract public static function createCollection(array $data = array());

/**
* Process query parameters
*
* @param array $parameters
* @return array
*/
protected function parseQueryParameters(array $parameters = array())
{
foreach($parameters as $key => $candidate) {
if ($candidate instanceof QueryParameterInterface) {
unset($parameters[$key]);

$parameters[$candidate->getKey()] = $candidate->getValue();
}
}

return $parameters;
}

/**
* Hydrate the object with data
*
* @param AbstractModel $object
* @param array $data
* @return $this
* @throws RuntimeException
*/
public function hydrate(AbstractModel $object, array $data = array())
{
if (!empty($data)) {
foreach ($data as $k => $v) {
if (in_array($k, $object::$_properties)) {

$method = self::camelize(
sprintf('set_%s', $k)
);

if (!method_exists($object, $method)) {
throw new RuntimeException(sprintf(
'Trying to call method "%s" on "%s" but it does not exist or is private.',
$method,
get_class($object)
));
}

$object->$method($v);
}
}
}

return $object;
}

/**
* Transforms an under_scored_string to a camelCasedOne
*
* @see https://gist.github.com/troelskn/751517
*
* @param $candidate
* @return string
*/
private function camelize($candidate)
{
return lcfirst(
implode('',
array_map('ucfirst',
array_map('strtolower',
explode('_', $candidate
)
)
)
)
);
}

/**
* Transforms a camelCasedString to an under_scored_one
*
* @see https://gist.github.com/troelskn/751517
*
* @param $camelized
* @return string
*/
private function uncamelize($camelized) {
return implode('_',
array_map('strtolower',
preg_split('/([A-Z]{1}[^A-Z]*)/', $camelized, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY)
)
);
}
}
63 changes: 63 additions & 0 deletions lib/Tmdb/Factory/Common/ImageFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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\Factory\Common;

use Tmdb\Factory\AbstractFactory;
use Tmdb\Model\Common\Collection\Images;
use Tmdb\Model\Image;

class ImageFactory extends AbstractFactory
{
/**
* {@inheritdoc}
*/
public static function create(array $data = array())
{
return parent::hydrate(new Image(), $data);
}

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

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

return $collection;
}

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

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

$collection[] = $item;
}
}
}

return self::createCollection($collection);
}

}
22 changes: 18 additions & 4 deletions lib/Tmdb/Factory/GenreFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
*/
namespace Tmdb\Factory;

use Tmdb\Client;
use Tmdb\Model\Collection\Genres;
use Tmdb\Model\Genre;

class GenreFactory {
class GenreFactory extends AbstractFactory
{
/**
* {@inheritdoc}
*/
public static function create(array $data = array())
{
$genre = new Genre();

return $genre->hydrate($data);
return parent::hydrate(new Genre(), $data);
}

/**
Expand All @@ -40,4 +40,18 @@ public static function createCollection(array $data = array())
return $collection;
}

/**
* Load a genre with the given identifier
*
* @param Client $client
* @param $id
* @param $parameters
* @return $this
*/
public static function load(Client $client, $id, array $parameters = array()) {
$data = $client->api('genres')->getGenre($id, parent::parseQueryParameters($parameters));

return self::create($data);
}

}
70 changes: 44 additions & 26 deletions lib/Tmdb/Factory/MovieFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
namespace Tmdb\Factory;

use Tmdb\Api\Movies;
use Tmdb\Client;
use Tmdb\Factory\Common\ImageFactory;
use Tmdb\Model\Common\Collection;
use Tmdb\Model\Movie;

Expand All @@ -28,41 +30,41 @@ public static function create(array $data = array())

$movie = new Movie();

if (array_key_exists('alternative_titles', $data) && array_key_exists('titles', $data['alternative_titles'])) {
$movie->setAlternativeTitles(Movie::collectGenericCollection($client, $data['alternative_titles']['titles'], new AlternativeTitle()));
}
// if (array_key_exists('alternative_titles', $data) && array_key_exists('titles', $data['alternative_titles'])) {
// $movie->setAlternativeTitles(Movie::collectGenericCollection($client, $data['alternative_titles']['titles'], new AlternativeTitle()));
// }

$casts = array();
$credits = $movie->getCredits();

/** Credits */
if (array_key_exists('credits', $data)) {
$casts = $data['credits'];
}

if (array_key_exists('casts', $data)) {
$casts = $data['casts'];
}

if (array_key_exists('cast', $casts)) {
$credits->setCast(parent::collectCast($client, $casts['cast']));
}

if (array_key_exists('crew', $casts)) {
$credits->setCrew(parent::collectCrew($client, $casts['crew']));
}

$movie->setCredits($credits);
// if (array_key_exists('credits', $data)) {
// $casts = $data['credits'];
// }
//
// if (array_key_exists('casts', $data)) {
// $casts = $data['casts'];
// }
//
// if (array_key_exists('cast', $casts)) {
// $credits->setCast($casts['cast']);
// }
//
// if (array_key_exists('crew', $casts)) {
// $credits->setCrew($casts['crew']);
// }
//
// $movie->setCredits($credits);

/** Genres */
if (array_key_exists('genres', $data)) {
$movie->setGenres(GenreFactory::createCollection($data['genres']));
}
//
// /** Images */
// if (array_key_exists('images', $data)) {
// $movie->setImages(parent::collectImages($client, $data['images']));
// }

/** Images */
if (array_key_exists('images', $data)) {
$movie->setImages(ImageFactory::createCollectionFromMovie($data['images']));
}
//
// /** Keywords */
// if (array_key_exists('keywords', $data)) {
Expand All @@ -89,7 +91,7 @@ public static function create(array $data = array())
// if (array_key_exists('changes', $data)) {
// }

return $movie->hydrate($data);
return parent::hydrate($movie, $data);
}

/**
Expand All @@ -105,4 +107,20 @@ public static function createCollection(array $data = array())

return $collection;
}


/**
* Load a movie with the given identifier
*
* @param Client $client
* @param $id
* @param $parameters
* @return $this
*/
public static function load(Client $client, $id, array $parameters = array()) {
$data = $client->api('movies')->getMovie($id, parent::parseQueryParameters($parameters));

return self::create($data);
}

}
Loading

0 comments on commit 9859dbd

Please sign in to comment.