Skip to content

Commit

Permalink
Making a start of moving the ugly fromArray methods to dedicated Fact…
Browse files Browse the repository at this point in the history
…ories
  • Loading branch information
wtfzdotnet committed Nov 6, 2013
1 parent 3a07da7 commit 5c90abe
Show file tree
Hide file tree
Showing 8 changed files with 328 additions and 230 deletions.
34 changes: 17 additions & 17 deletions examples/movies/model/get.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@

echo $movie->getTitle() . "\n";

echo "Cast\n";

foreach($movie->credits->cast as $person) {
printf(" - %s as %s\n", $person->getName(), $person->getCharacter());
}

foreach($movie->getCredits()->getCrew() as $person) {
printf(" - %s as %s\n", $person->getName(), $person->getJob());
}

echo "Images\n";

foreach($movie->getImages() as $image) {
printf(" - %s\n", $image->getFilePath());
}

echo "Genres\n";
//echo "Cast\n";
//
//foreach($movie->credits->cast as $person) {
// printf(" - %s as %s\n", $person->getName(), $person->getCharacter());
//}
//
//foreach($movie->getCredits()->getCrew() as $person) {
// printf(" - %s as %s\n", $person->getName(), $person->getJob());
//}
//
//echo "Images\n";
//
//foreach($movie->getImages() as $image) {
// printf(" - %s\n", $image->getFilePath());
//}
//
//echo "Genres\n";

foreach($movie->getGenres() as $genre) {
printf(" - %s\n", $genre->getName());
Expand Down
14 changes: 9 additions & 5 deletions lib/Tmdb/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Guzzle\Http\ClientInterface;
use Guzzle\Http\Message\RequestInterface;

use Tmdb\Api\ApiInterface;
use Tmdb\Exception\InvalidArgumentException;
use Tmdb\HttpClient\HttpClient;
use Tmdb\HttpClient\HttpClientInterface;
Expand Down Expand Up @@ -54,11 +55,13 @@ public function __construct(Token $token, ClientInterface $httpClient = null)
{
$httpClient = $httpClient ?: new GuzzleClient(self::TMDB_URI);

$apiQueryTokenPlugin = new ApiTokenPlugin($token);
$httpClient->addSubscriber($apiQueryTokenPlugin);
if ($httpClient instanceof \Guzzle\Common\HasDispatcherInterface) {
$apiQueryTokenPlugin = new ApiTokenPlugin($token);
$httpClient->addSubscriber($apiQueryTokenPlugin);

$acceptJsonHeaderPlugin = new AcceptJsonHeader();
$httpClient->addSubscriber($acceptJsonHeaderPlugin);
$acceptJsonHeaderPlugin = new AcceptJsonHeader();
$httpClient->addSubscriber($acceptJsonHeaderPlugin);
}

$this->httpClient = new HttpClient(self::TMDB_URI, array(), $httpClient);
$this->setToken($token);
Expand All @@ -79,10 +82,11 @@ public function setToken(Token $token)
/**
* Return the relevant API object
*
* Pick your poison for the matter of your preference of plural / non-plural.
* @todo we should either register these by DI, or hardcode the return values through the constructor, and provide getApiConfiguration() type of methods
*
* @param $name
* @throws Exception\InvalidArgumentException
* @return ApiInterface
*/
public function api($name)
{
Expand Down
50 changes: 50 additions & 0 deletions lib/Tmdb/Factory/GenreFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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;

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

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

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

/**
* Convert an array to an hydrated object
*
* @param array $data
* @return Genre[]
*/
public static function createCollection(array $data = array())
{
$collection = new Genres();

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

return $collection;
}

}
97 changes: 97 additions & 0 deletions lib/Tmdb/Factory/MovieFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?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;

use Tmdb\Model\Movie;

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

$movie = new Movie($data['id']);

// 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)) {
$movie->setGenres(GenreFactory::createCollection($data['genres']));
}
//
// /** Images */
// if (array_key_exists('images', $data)) {
// $movie->setImages(parent::collectImages($client, $data['images']));
// }
//
// /** Keywords */
// if (array_key_exists('keywords', $data)) {
// }
//
// if (array_key_exists('releases', $data)) {
// }
//
// if (array_key_exists('trailers', $data)) {
// }
//
// if (array_key_exists('translations', $data)) {
// }
//
// if (array_key_exists('similar_movies', $data)) {
// }
//
// if (array_key_exists('reviews', $data)) {
// }
//
// if (array_key_exists('lists', $data)) {
// }
//
// if (array_key_exists('changes', $data)) {
// }

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

}
1 change: 1 addition & 0 deletions lib/Tmdb/HttpClient/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ public function request(RequestInterface $request)
}
catch(\Exception $e)
{
// @TODO catch any API errors / timeouts / other specific information from Guzzle?
throw $e;
}

Expand Down
129 changes: 0 additions & 129 deletions lib/Tmdb/Model/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,135 +121,6 @@ protected function parseQueryParameters(array $parameters = array())
return $parameters;
}

/**
* Collect all images from an `image` array ( containing e.g. posters / profiles etc. )
*
* @param $client
* @param array $collection
* @return Image[]
*/
protected function collectImages($client, array $collection = array())
{
$images = new Images();

foreach($collection as $collectionName => $itemCollection) {
foreach($itemCollection as $item) {
if (!is_array($item)) {
continue;
}

$image = Image::fromArray($client, $item);

$image->setType(Image::getTypeFromCollectionName($collectionName));

$images->addImage($image);
}
}

return $images;
}

/**
* Collect all people from an array
*
* @param $client
* @param array $collection
* @return Person[]
*/
protected function collectPeople($client, array $collection = array())
{
$people = new People();

foreach($collection as $item) {
$person = Person::fromArray($client, $item);

$people->addPerson($person);
}

return $people;
}

/**
* Collect cast
*
* @param $client
* @param array $collection
* @return CastMember[]
*/
protected function collectCast($client, array $collection = array())
{
$people = new Cast();

foreach($collection as $item) {
$person = CastMember::fromArray($client, $item);

$people->addPerson($person);
}

return $people;
}

/**
* Collect crew
*
* @param $client
* @param array $collection
* @return CrewMember[]
*/
protected function collectCrew($client, array $collection = array())
{
$people = new Crew();

foreach($collection as $item) {
$person = CrewMember::fromArray($client, $item);

$people->addPerson($person);
}

return $people;
}

/**
* Collect all genres from an array
*
* @param $client
* @param array $collection
* @return Genre[]
*/
protected function collectGenres($client, array $collection = array())
{
$genres = new Genres();

foreach($collection as $item) {
$genre = Genre::fromArray($client, $item);
$genres->addGenre($genre);
}

return $genres;
}

/**
* Collect all genres from an array
*
* @param $client
* @param array $collection
* @param object $object
* @return Collection
*/
protected function collectGenericCollection($client, array $collection = array(), $object)
{
$collectionObject = new Collection();

foreach($collection as $item) {
$class = get_class($object);
$model = $class::fromArray($client, $item);

$collectionObject->addObject($model);
}

return $collectionObject;
}

/**
* Transforms an under_scored_string to a camelCasedOne
*
Expand Down
Loading

0 comments on commit 5c90abe

Please sign in to comment.