Skip to content

Commit

Permalink
- Refactoring collection locations to be more natural
Browse files Browse the repository at this point in the history
- Adding query parameter bags to provide a decent API to all possible query parameters
  • Loading branch information
wtfzdotnet committed Nov 3, 2013
1 parent 54128dc commit 0812794
Show file tree
Hide file tree
Showing 27 changed files with 558 additions and 97 deletions.
59 changes: 36 additions & 23 deletions examples/movies/model/get.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,49 @@
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
require_once('../../../vendor/autoload.php');
require_once('../../../apikey.php');
require_once('../../../vendor/autoload.php');
require_once('../../../apikey.php');

$token = new \Tmdb\ApiToken(TMDB_API_KEY);
$client = new \Tmdb\Client($token);
$token = new \Tmdb\ApiToken(TMDB_API_KEY);
$client = new \Tmdb\Client($token);

$movie = \Tmdb\Model\Movie::load($client, 87421, array(
'append_to_response' => 'casts,images'
));
$append = new \Tmdb\Model\Movie\QueryParameter\AppendToResponse(array(
\Tmdb\Model\Movie\QueryParameter\AppendToResponse::ALTERNATIVE_TITLES,
\Tmdb\Model\Movie\QueryParameter\AppendToResponse::CHANGES,
\Tmdb\Model\Movie\QueryParameter\AppendToResponse::CREDITS,
\Tmdb\Model\Movie\QueryParameter\AppendToResponse::IMAGES,
\Tmdb\Model\Movie\QueryParameter\AppendToResponse::KEYWORDS,
\Tmdb\Model\Movie\QueryParameter\AppendToResponse::LISTS,
\Tmdb\Model\Movie\QueryParameter\AppendToResponse::RELEASES,
\Tmdb\Model\Movie\QueryParameter\AppendToResponse::REVIEWS,
\Tmdb\Model\Movie\QueryParameter\AppendToResponse::SIMILAR_MOVIES,
\Tmdb\Model\Movie\QueryParameter\AppendToResponse::TRAILERS,
\Tmdb\Model\Movie\QueryParameter\AppendToResponse::TRANSLATIONS,
));

echo $movie->getTitle() . "\n";
$movie = \Tmdb\Model\Movie::load($client, 87421, array($append, $language));

echo "Cast\n";

foreach($movie->getCast() as $person) {
printf(" - %s as %s\n", $person->getName(), $person->getCharacter());
}
echo $movie->getTitle() . "\n";

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

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

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

echo "Genres\n";
echo "Images\n";

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

echo "Genres\n";

foreach($movie->getGenres() as $genre) {
printf(" - %s\n", $genre->getName());
}
8 changes: 4 additions & 4 deletions lib/Tmdb/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,22 @@ public function api($name)
{
switch($name) {
case 'configuration':
/** @return Api\Configuration */
/** @var Api\Configuration */
$api = new Api\Configuration($this);
break;

case 'authentication':
/** @return Api\Authentication */
/** @var Api\Authentication */
$api = new Api\Authentication($this);
break;

case 'account':
/** @return Api\Account */
/** @var Api\Account */
$api = new Api\Account($this);
break;

case 'movies':
/** @return Api\Movies */
/** @var Api\Movies */
$api = new Api\Movies($this);
break;

Expand Down
36 changes: 29 additions & 7 deletions lib/Tmdb/Model/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@

use Tmdb\Client;
use Tmdb\Exception\RuntimeException;
use Tmdb\Model\Common\Genres;
use Tmdb\Model\Common\Images;
use Tmdb\Model\Common\People;
use Tmdb\Model\Movie\Cast as MovieCast;
use Tmdb\Model\Movie\Crew as MovieCrew;

use Tmdb\Model\Common\Collection\Credits\Cast;
use Tmdb\Model\Common\Collection\Credits\Crew;
use Tmdb\Model\Common\Collection\Genres;
use Tmdb\Model\Common\Collection\Images;
use Tmdb\Model\Common\Collection\People;

use Tmdb\Model\Common\QueryParameter\QueryParameterInterface;
use Tmdb\Model\Person\CastMember;
use Tmdb\Model\Person\CrewMember;

Expand Down Expand Up @@ -97,6 +100,25 @@ public function hydrate(array $data = array())
return $this;
}

/**
* 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;
}

/**
* Collect all images from an `image` array ( containing e.g. posters / profiles etc. )
*
Expand Down Expand Up @@ -154,7 +176,7 @@ protected function collectPeople($client, array $collection = array())
*/
protected function collectCast($client, array $collection = array())
{
$people = new MovieCast();
$people = new Cast();

foreach($collection as $item) {
$person = CastMember::fromArray($client, $item);
Expand All @@ -174,7 +196,7 @@ protected function collectCast($client, array $collection = array())
*/
protected function collectCrew($client, array $collection = array())
{
$people = new MovieCrew();
$people = new Crew();

foreach($collection as $item) {
$person = CrewMember::fromArray($client, $item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Model\Common;
namespace Tmdb\Model\Common\Collection;

use Tmdb\Model\Common\Collection;

class Images extends Collection {

Expand Down
66 changes: 66 additions & 0 deletions lib/Tmdb/Model/Collection/Credits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?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\Collection;

use Tmdb\Model\Collection\People\Cast;
use Tmdb\Model\Collection\People\Crew;

class Credits {
/**
* @var Cast
*/
private $cast;

/**
* @var Crew
*/
private $crew;

/**
* @param \Tmdb\Model\Collection\People\Cast $cast
* @return $this
*/
public function setCast($cast)
{
$this->cast = $cast;
return $this;
}

/**
* @return \Tmdb\Model\Collection\People\Cast
*/
public function getCast()
{
return $this->cast;
}

/**
* @param \Tmdb\Model\Collection\People\Crew $crew
* @return $this
*/
public function setCrew($crew)
{
$this->crew = $crew;
return $this;
}

/**
* @return \Tmdb\Model\Collection\People\Crew
*/
public function getCrew()
{
return $this->crew;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Model\Movie;
namespace Tmdb\Model\Collection\Credits;

use Tmdb\Model\Common\People\Cast as BaseCast;
use Tmdb\Model\Collection\People\Cast as BaseCast;

class Cast extends BaseCast {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Model\Movie;
namespace Tmdb\Model\Collection\Credits;

use Tmdb\Model\Common\People\Crew as BaseCrew;
use Tmdb\Model\Collection\People\Crew as BaseCrew;

class Crew extends BaseCrew {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Model\Common;
namespace Tmdb\Model\Collection;

use Tmdb\Model\Common\Collection;
use Tmdb\Model\Genre;

class Genres extends Collection {

/**
* Returns all genres
*
* @return array
* @return Genre[]
*/
public function getGenres()
{
Expand Down
56 changes: 56 additions & 0 deletions lib/Tmdb/Model/Collection/Keywords.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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\Common\Collection;

use Tmdb\Model\Common\Collection;

use Tmdb\Model\Keyword;

class Keywords extends Collection {

/**
* Returns all keywords
*
* @return array
*/
public function getKeywords()
{
return $this->data;
}

/**
* Retrieve a keyword from the collection
*
* @param $id
* @return null
*/
public function getKeyword($id) {
foreach($this->data as $keyword) {
if ($id === $keyword->getId()) {
return $keyword;
}
}

return null;
}

/**
* Add a keyword to the collection
*
* @param Keyword $keyword
*/
public function addKeyword(Keyword $keyword)
{
$this->data[] = $keyword;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Model\Common;
namespace Tmdb\Model\Collection;

use Tmdb\Model\Common\People\PersonInterface;
use Tmdb\Model\Common\Collection;

use Tmdb\Model\Collection\People\PersonInterface;
use Tmdb\Model\Person;

class People extends Collection {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Model\Common\People;
namespace Tmdb\Model\Collection\People;

use Tmdb\Model\Common\People;
use Tmdb\Model\Collection\People;
use Tmdb\Model\Person;

class Cast extends People {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Model\Common\People;
namespace Tmdb\Model\Collection\People;

use Tmdb\Model\Common\People;
use Tmdb\Model\Collection\People;
use Tmdb\Model\Person;

class Crew extends People {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Model\Common\People;
namespace Tmdb\Model\Collection\People;

interface PersonInterface {
function getName();
Expand Down
Loading

0 comments on commit 0812794

Please sign in to comment.