Skip to content

Commit

Permalink
Updating Model structure
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Jan 8, 2014
1 parent f26bb3e commit de17a90
Show file tree
Hide file tree
Showing 12 changed files with 320 additions and 20 deletions.
2 changes: 2 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Add event subscribers to supply language and adult filters
- Figure out a way to slim down the seemingly dependency on Guzzle
10 changes: 10 additions & 0 deletions examples/movies/model/all.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
header('Content-Type: text/html; charset=utf-8');

require_once('../../../vendor/autoload.php');
require_once('../../../apikey.php');

Expand Down Expand Up @@ -88,4 +90,12 @@

foreach($movie->getTrailers() as $trailer) {
printf(" - %s\n", $trailer->getUrl());
}

$popular = $repository->getPopular();

echo "Popular titles\n";

foreach($popular as $p) {
printf(" - %s\n", $p->getTitle());
}
33 changes: 33 additions & 0 deletions examples/people/model/all.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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
*/
require_once('../../../vendor/autoload.php');
require_once('../../../apikey.php');

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

// This is optional, but if you want lots of data this is the way.
$append = new \Tmdb\Model\Person\QueryParameter\AppendToResponse(array(
\Tmdb\Model\Person\QueryParameter\AppendToResponse::IMAGES,
\Tmdb\Model\Person\QueryParameter\AppendToResponse::CHANGES,
\Tmdb\Model\Person\QueryParameter\AppendToResponse::COMBINED_CREDITS,
\Tmdb\Model\Person\QueryParameter\AppendToResponse::LATEST,
\Tmdb\Model\Person\QueryParameter\AppendToResponse::MOVIE_CREDITS,
\Tmdb\Model\Person\QueryParameter\AppendToResponse::TV_CREDITS,
\Tmdb\Model\Person\QueryParameter\AppendToResponse::POPULAR
));

$repository = new \Tmdb\Repository\PersonRepository($client);
$person = $repository->load(33, array($append));

var_dump($person);
8 changes: 8 additions & 0 deletions lib/Tmdb/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ public function getMovieApi()
return new Api\Movies($this);
}

/**
* @return Api\People
*/
public function getPersonApi()
{
return new Api\People($this);
}

/**
* Return the relevant API object
*
Expand Down
18 changes: 17 additions & 1 deletion lib/Tmdb/Factory/Common/ImageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static function createCollection(array $data = array())
/**
* {@inheritdoc}
*/
public static function createCollectionFromMovie(array $data = array())
public static function createImageCollection(array $data = array())
{
$collection = array();

Expand All @@ -60,4 +60,20 @@ public static function createCollectionFromMovie(array $data = array())
return self::createCollection($collection);
}

/**
* {@inheritdoc}
*/
public static function createCollectionFromMovie(array $data = array())
{
return self::createImageCollection($data);
}

/**
* {@inheritdoc}
*/
public static function createCollectionFromPeople(array $data = array())
{
return self::createImageCollection($data);
}

}
14 changes: 9 additions & 5 deletions lib/Tmdb/Factory/People/PeopleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
*/
namespace Tmdb\Factory\People;

use Symfony\Component\Yaml\Exception\RuntimeException;
use Tmdb\Client;
use Tmdb\Factory\AbstractFactory;
use Tmdb\Model\Common\Collection;
use Tmdb\Model\Person\CastMember;
use Tmdb\Model\Person\CrewMember;
use Tmdb\Model\Person;

use Tmdb\Factory\Common\ImageFactory;

class PeopleFactory extends AbstractFactory {
/**
Expand All @@ -36,9 +37,12 @@ public static function create(array $data = array())
}

if (null === $person) {
throw new RuntimeException(sprintf(
'Was unable to determine the type of person by the data provided for #%d', $data['id']
));
$person = new Person();
}

/** Images */
if (array_key_exists('images', $data)) {
$person->setImages(ImageFactory::createCollectionFromPeople($data['images']));
}

return parent::hydrate($person, $data);
Expand Down
10 changes: 5 additions & 5 deletions lib/Tmdb/HttpClient/HttpClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
interface HttpClientInterface
{
/**
* Send a GET request
* Compose a GET request
*
* @param string $path Request path
* @param array $parameters GET Parameters
Expand All @@ -29,7 +29,7 @@ interface HttpClientInterface
public function get($path, array $parameters = array(), array $headers = array());

/**
* Send a POST request
* Compose a POST request
*
* @param string $path Request path
* @param string $postBody The post BODY
Expand All @@ -41,7 +41,7 @@ public function get($path, array $parameters = array(), array $headers = array()
public function post($path, $postBody, array $parameters = array(), array $headers = array());

/**
* Send a PATCH request
* Compose a PATCH request
*
* @param string $path Request path
* @param string $body The body
Expand All @@ -53,7 +53,7 @@ public function post($path, $postBody, array $parameters = array(), array $heade
public function patch($path, $body = null, array $parameters = array(), array $headers = array());

/**
* Send a PUT request
* Compose a PUT request
*
* @param string $path Request path
* @param string $body The body
Expand All @@ -65,7 +65,7 @@ public function patch($path, $body = null, array $parameters = array(), array $h
public function put($path, $body = null, array $parameters = array(), array $headers = array());

/**
* Send a DELETE request
* Compose a DELETE request
*
* @param string $path Request path
* @param string $body The body
Expand Down
20 changes: 19 additions & 1 deletion lib/Tmdb/Model/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
namespace Tmdb\Model;

use Tmdb\Client;

use Tmdb\Model\Collection\Credits;

use Tmdb\Model\Common\Collection;
use Tmdb\Model\Common\Collection\Images;

use Tmdb\Model\Collection\People\PersonInterface;

class Person extends AbstractModel implements PersonInterface {
Expand All @@ -34,7 +40,7 @@ class Person extends AbstractModel implements PersonInterface {
protected $images;
protected $changes;

protected static $_properties = array(
public static $_properties = array(
'adult',
'also_known_as',
'biography',
Expand All @@ -47,6 +53,18 @@ class Person extends AbstractModel implements PersonInterface {
'profile_path',
);

/**
* Constructor
*
* Set all default collections
*/
public function __construct()
{
$this->credits = new Credits();
$this->images = new Images();
$this->changes = new Collection();
}

/**
* Convert an array to an hydrated object
*
Expand Down
23 changes: 23 additions & 0 deletions lib/Tmdb/Model/Person/QueryParameter/AppendToResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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\Person\QueryParameter;

use Tmdb\Model\Common\QueryParameter\AppendToResponse as BaseAppendToResponse;

final class AppendToResponse extends BaseAppendToResponse {
const MOVIE_CREDITS = 'movie_credits';
const TV_CREDITS = 'tv_credits';
const COMBINED_CREDITS = 'combined_credits';
const IMAGES = 'images';
const CHANGES = 'changes';
}
19 changes: 19 additions & 0 deletions lib/Tmdb/Repository/AbstractRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
*/
namespace Tmdb\Repository;

use Tmdb\Api\ApiInterface;
use Tmdb\Client;
use Tmdb\Model\Common\QueryParameter\QueryParameterInterface;

abstract class AbstractRepository {

private static $client = null;

protected $api = null;

/**
* Constructor
*
Expand Down Expand Up @@ -57,4 +60,20 @@ protected function parseQueryParameters(array $parameters = array())

return $parameters;
}

/**
* Load the given identifier
*
* @param $id
* @param array $parameters
* @return mixed
*/
abstract public function load($id, array $parameters = array());

/**
* Return the API Class
*
* @return ApiInterface
*/
abstract public function getApi();
}
Loading

0 comments on commit de17a90

Please sign in to comment.