Skip to content

Commit

Permalink
Refactoring API, moving stuff up to factories
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Nov 16, 2013
1 parent 5c90abe commit d3dcb9e
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 41 deletions.
32 changes: 32 additions & 0 deletions lib/Tmdb/Factory/AbstractFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* This file is part of the Wrike PHP API created by B-Found IM&S.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Wrike
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, B-Found Internet Marketing & Services
* @version 0.0.1
*/

namespace Tmdb\Factory;

abstract class AbstractFactory {
/**
* Convert an array to an hydrated object
*
* @param array $data
* @return $this
*/
abstract public static function create(array $data = array());

/**
* Convert an array with an collection of items to an hydrated object collection
*
* @param array $data
* @return $this
*/
abstract public static function createCollection(array $data = array());
}
10 changes: 2 additions & 8 deletions lib/Tmdb/Factory/GenreFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@

class GenreFactory {
/**
* Convert an array to an hydrated object
*
* @param array $data
* @return $this
* {@inheritdoc}
*/
public static function create(array $data = array())
{
Expand All @@ -31,10 +28,7 @@ public static function create(array $data = array())
}

/**
* Convert an array to an hydrated object
*
* @param array $data
* @return Genre[]
* {@inheritdoc}
*/
public static function createCollection(array $data = array())
{
Expand Down
45 changes: 45 additions & 0 deletions lib/Tmdb/Factory/Movie/AlternativeTitleFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* This file is part of the Wrike PHP API created by B-Found IM&S.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Wrike
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, B-Found Internet Marketing & Services
* @version 0.0.1
*/

namespace Tmdb\Factory\Movie;

use Tmdb\Factory\AbstractFactory;
use Tmdb\Model\Common\Collection;
use Tmdb\Model\Movie\AlternativeTitle;

class AlternativeTitleFactory extends AbstractFactory
{
/**
* {@inheritdoc}
*/
public static function create(array $data = array())
{
$title = new AlternativeTitle();

return $title->hydrate($data);
}

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

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

return $collection;
}
}
74 changes: 43 additions & 31 deletions lib/Tmdb/Factory/MovieFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,47 @@

namespace Tmdb\Factory;

use Tmdb\Api\Movies;
use Tmdb\Model\Common\Collection;
use Tmdb\Model\Movie;

class MovieFactory {
class MovieFactory extends AbstractFactory {
/**
* Convert an array to an hydrated object
*
* @param array $data
* @return $this
* {@inheritdoc}
*/
public static function create(array $data = array())
{
if (!$data) {
return null;
}

$movie = new Movie($data['id']);
$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()));
// }
//
// $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('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);
//
/** Genres */
if (array_key_exists('genres', $data)) {
Expand Down Expand Up @@ -94,4 +93,17 @@ public static function create(array $data = array())
return $movie->hydrate($data);
}

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

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

return $collection;
}
}
45 changes: 45 additions & 0 deletions lib/Tmdb/Factory/People/CastFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* This file is part of the Wrike PHP API created by B-Found IM&S.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Wrike
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, B-Found Internet Marketing & Services
* @version 0.0.1
*/

namespace Tmdb\Factory\People;

use Tmdb\Factory\AbstractFactory;
use Tmdb\Model\Collection\People\Cast;
use Tmdb\Model\Collection\People;

class CastFactory extends AbstractFactory
{
/**
* {@inheritdoc}
*/
public static function create(array $data = array())
{
$cast = new Cast();

return $cast->hydrate($data);
}

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

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

return $collection;
}
}
45 changes: 45 additions & 0 deletions lib/Tmdb/Factory/People/CrewFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* This file is part of the Wrike PHP API created by B-Found IM&S.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Wrike
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, B-Found Internet Marketing & Services
* @version 0.0.1
*/

namespace Tmdb\Factory\People;

use Tmdb\Factory\AbstractFactory;
use Tmdb\Model\Collection\People\Crew;
use Tmdb\Model\Collection\People;

class CrewFactory extends AbstractFactory
{
/**
* {@inheritdoc}
*/
public static function create(array $data = array())
{
$crew = new Crew();

return $crew->hydrate($data);
}

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

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

return $collection;
}
}
2 changes: 1 addition & 1 deletion lib/Tmdb/Model/Collection/Genres.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function getGenres()
* Retrieve a genre from the collection
*
* @param $id
* @return null
* @return Genre|null
*/
public function getGenre($id) {
foreach($this->data as $genre) {
Expand Down
3 changes: 2 additions & 1 deletion lib/Tmdb/Model/Genre.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Tmdb\Model;

use Tmdb\Client;
use Tmdb\Factory\GenreFactory;

class Genre extends AbstractModel {

Expand Down Expand Up @@ -50,7 +51,7 @@ public static function fromArray(Client $client, array $data)
public static function load(Client $client, $id, array $parameters = array()) {
$data = $client->api('genres')->getGenre($id, parent::parseQueryParameters($parameters));

return Genre::fromArray($client, $data);
return GenreFactory::create($data);
}

/**
Expand Down

0 comments on commit d3dcb9e

Please sign in to comment.