Skip to content

Commit

Permalink
Extending the Movies model
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Nov 2, 2013
1 parent 02693b1 commit ae365f5
Show file tree
Hide file tree
Showing 16 changed files with 985 additions and 36 deletions.
46 changes: 45 additions & 1 deletion lib/Tmdb/Model/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
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\Person\CastMember;
use Tmdb\Model\Person\CrewMember;

class AbstractModel {
protected static $_properties;
Expand Down Expand Up @@ -142,7 +146,47 @@ protected function collectPeople($client, array $collection = array())
}

/**
* Collect all people from an array
* Collect cast
*
* @param $client
* @param array $collection
* @return People
*/
protected function collectCast($client, array $collection = array())
{
$people = new MovieCast();

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

$people->addPerson($person);
}

return $people;
}

/**
* Collect crew
*
* @param $client
* @param array $collection
* @return People
*/
protected function collectCrew($client, array $collection = array())
{
$people = new MovieCrew();

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

use Tmdb\Client;
use Tmdb\Model\Common\Collection;

abstract class Changes extends AbstractModel {

private $from = null;
private $to = null;
private $page = null;

public function __construct(Client $client)
{
$this->setClient($client);
}

/**
* Set the from parameter
*
* @param \DateTime $date
* @return $this
*/
public function from($date)
{
$this->from = $date->format('Y-m-d');

return $this;
}

/**
* Set the to parameter
*
* @param \DateTime $date
* @return $this
*/
public function to($date)
{
$this->to = $date->format('Y-m-d');

return $this;
}

/**
* Set the page parameter
*
* @param int $page
* @return $this
*/
public function page($page = 1) {
$this->page = (int) $page;

return $this;
}

public function execute()
{
$collection = new Collection();

$response = $this->getClient()->api('changes')->getMovieChanges();

if (!empty($response)) {
foreach($response['results'] as $change) {
$collection->add($change);
}
}

return $collection;
}

abstract public function getEntity();
}
93 changes: 93 additions & 0 deletions lib/Tmdb/Model/Changes/Change.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?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;

use Tmdb\Client;

class Change extends AbstractModel {

private $id;
private $name;

protected static $_properties = array(
'id',
'name',
);

/**
* Convert an array to an hydrated object
*
* @param Client $client
* @param array $data
* @return $this
*/
public static function fromArray(Client $client, array $data)
{
$genre = new Genre($data['id']);
//$genre->setClient($client);

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

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

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

/**
* @param mixed $id
* @return $this
*/
public function setId($id)
{
$this->id = (int) $id;
return $this;
}

/**
* @return mixed
*/
public function getId()
{
return $this->id;
}

/**
* @param mixed $name
* @return $this
*/
public function setName($name)
{
$this->name = $name;
return $this;
}

/**
* @return mixed
*/
public function getName()
{
return $this->name;
}


}
27 changes: 27 additions & 0 deletions lib/Tmdb/Model/Common/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,32 @@
use Guzzle\Common\Collection as GuzzleCollection;

class Collection extends GuzzleCollection {
/**
* Allow support for adding objects
*
* @param string $key
* @param mixed $value
* @return GuzzleCollection
*/
public function add($key, $value) {
if ($key === null && is_object($value)) {
$key = spl_object_hash($value);
}

return parent::add($key, $value);
}

/**
* Allow support for getting objects
*
* @param string $key
* @return mixed|null
*/
public function get($key) {
if (is_object($key)) {
$key = spl_object_hash($key);
}

return parent::get($key);
}
}
62 changes: 62 additions & 0 deletions lib/Tmdb/Model/Common/Country.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?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;

class Country extends AbstractModel {

private $iso31661;
private $name;

protected static $_properties = array(
'iso_3166_1',
'name',
);

/**
* @param mixed $iso31661
* @return $this
*/
public function setIso31661($iso31661)
{
$this->iso31661 = $iso31661;
return $this;
}

/**
* @return mixed
*/
public function getIso31661()
{
return $this->iso31661;
}

/**
* @param mixed $name
* @return $this
*/
public function setName($name)
{
$this->name = $name;
return $this;
}

/**
* @return mixed
*/
public function getName()
{
return $this->name;
}


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

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

class People extends Collection {

/**
* Returns all people
*
* @return array
* @return Person[]
*/
public function getPeople()
{
Expand All @@ -30,7 +31,7 @@ public function getPeople()
* Retrieve a person from the collection
*
* @param $id
* @return null
* @return Person
*/
public function getPerson($id) {
foreach($this->data as $person) {
Expand All @@ -45,9 +46,9 @@ public function getPerson($id) {
/**
* Add a person to the collection
*
* @param Person $person
* @param PersonInterface $person
*/
public function addPerson(Person $person)
public function addPerson(PersonInterface $person)
{
$this->data[] = $person;
}
Expand Down
Loading

0 comments on commit ae365f5

Please sign in to comment.