Skip to content

Commit

Permalink
Adding Find
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Jan 31, 2014
1 parent d3d7331 commit cea794f
Show file tree
Hide file tree
Showing 9 changed files with 344 additions and 11 deletions.
1 change: 1 addition & 0 deletions examples/phpunit
17 changes: 14 additions & 3 deletions lib/Tmdb/Api/Find.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,25 @@ class Find
extends AbstractApi
{
/**
* Get a list of valid jobs.
* The find method makes it easy to search for objects in our database by an external id. For instance, an IMDB ID. This will search all objects (movies, TV shows and people) and return the results in a single response. TV season and TV episode searches will be supported shortly.
*
* The supported external sources for each object are as follows:
*
* Movies: imdb_id
* People: imdb_id, freebase_mid, freebase_id, tvrage_id
* TV Series: imdb_id, freebase_mid, freebase_id, tvdb_id, tvrage_id
*
* @param string $id
* @param array $parameters
* @param array $headers
* @return mixed
*/
public function getJobs(array $parameters = array(), array $headers = array())
public function find($id, array $parameters = array(), array $headers = array())
{
return $this->get('job/list', $parameters, $headers);
return $this->get(
sprintf('find/%s', $id),
$parameters,
$headers
);
}
}
8 changes: 8 additions & 0 deletions lib/Tmdb/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ public function getCollectionsApi()
return new Api\Collections($this);
}

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

/**
* @return Api\Movies
*/
Expand Down
130 changes: 130 additions & 0 deletions lib/Tmdb/Factory/FindFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?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\Factory;

use Tmdb\Model\Common\GenericCollection;
use Tmdb\Model\Find;

class FindFactory extends AbstractFactory
{
/**
* @var MovieFactory
*/
private $movieFactory;

/**
* @var PeopleFactory
*/
private $peopleFactory;

/**
* @var TvFactory
*/
private $tvFactory;

/**
* Constructor
*/
public function __construct()
{
$this->movieFactory = new MovieFactory();
$this->peopleFactory = new PeopleFactory();
$this->tvFactory = new TvFactory();
}

/**
* {@inheritdoc}
*/
public function create(array $data = array())
{
$find = new Find();

if (array_key_exists('movie_results', $data)) {
$find->setMovieResults($this->getMovieFactory()->createCollection($data['movie_results']));
}

if (array_key_exists('person_results', $data)) {
$find->setPersonResults($this->getPeopleFactory()->createCollection($data['person_results']));
}

if (array_key_exists('tv_results', $data)) {
$find->setTvResults($this->getTvFactory()->createCollection($data['tv_results']));
}

return $find;
}

/**
* {@inheritdoc}
*/
public function createCollection(array $data = array())
{
return array();
}

/**
* @param \Tmdb\Factory\MovieFactory $movieFactory
* @return $this
*/
public function setMovieFactory($movieFactory)
{
$this->movieFactory = $movieFactory;
return $this;
}

/**
* @return \Tmdb\Factory\MovieFactory
*/
public function getMovieFactory()
{
return $this->movieFactory;
}

/**
* @param \Tmdb\Factory\PeopleFactory $peopleFactory
* @return $this
*/
public function setPeopleFactory($peopleFactory)
{
$this->peopleFactory = $peopleFactory;
return $this;
}

/**
* @return \Tmdb\Factory\PeopleFactory
*/
public function getPeopleFactory()
{
return $this->peopleFactory;
}

/**
* @param \Tmdb\Factory\TvFactory $tvFactory
* @return $this
*/
public function setTvFactory($tvFactory)
{
$this->tvFactory = $tvFactory;
return $this;
}

/**
* @return \Tmdb\Factory\TvFactory
*/
public function getTvFactory()
{
return $this->tvFactory;
}


}
90 changes: 90 additions & 0 deletions lib/Tmdb/Model/Find.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?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\Model\Collection\People;
use Tmdb\Model\Common\GenericCollection;

class Find extends AbstractModel {

/**
* @var GenericCollection
*/
private $movieResults;

/**
* @var People
*/
private $personResults;

/**
* @var GenericCollection
*/
private $tvResults;

/**
* @param \Tmdb\Model\Common\GenericCollection $movieResults
* @return $this
*/
public function setMovieResults($movieResults)
{
$this->movieResults = $movieResults;
return $this;
}

/**
* @return \Tmdb\Model\Common\GenericCollection
*/
public function getMovieResults()
{
return $this->movieResults;
}

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

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

/**
* @param \Tmdb\Model\Common\GenericCollection $tvResults
* @return $this
*/
public function setTvResults($tvResults)
{
$this->tvResults = $tvResults;
return $this;
}

/**
* @return \Tmdb\Model\Common\GenericCollection
*/
public function getTvResults()
{
return $this->tvResults;
}


}
27 changes: 27 additions & 0 deletions lib/Tmdb/Repository/FindRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,39 @@
namespace Tmdb\Repository;

// @todo
use Tmdb\Factory\FindFactory;
use Tmdb\Model\Find;

class FindRepository extends AbstractRepository {
/**
* Find something
*
* @param $id
* @param array $parameters
* @param array $headers
* @return Find
*/
public function find($id, array $parameters = array(), array $headers = array()) {
return $this->getFactory()->create(
$this->getApi()->find($id,$parameters, $headers)
);
}

/**
* Return the related API class
*
* @return \Tmdb\Api\Find
*/
public function getApi()
{
return $this->getClient()->getFindApi();
}

/**
* @return FindFactory
*/
public function getFactory()
{
return new FindFactory();
}
}
54 changes: 54 additions & 0 deletions test/Tmdb/Tests/Factory/FindFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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\Tests\Factory;

use Tmdb\Model\Find;

class FindFactoryTest extends TestCase
{
/**
* @test
*/
public function shouldConstructFind()
{
$factory = $this->getFactory();

/**
* @var Find $find
*/
$find = $factory->create(array(
'movie_results' => array(array('id' => 1)),
'person_results' => array(array('id' => 1)),
'tv_results' => array(array('id' => 1)),
));

$this->assertInstanceOf('Tmdb\Model\Find', $find);

foreach($find->getMovieResults() as $movie) {
$this->assertInstanceOf('Tmdb\Model\Movie', $movie);
}

foreach($find->getPersonResults() as $person) {
$this->assertInstanceOf('Tmdb\Model\Person', $person);
}

foreach($find->getTvResults() as $tv) {
$this->assertInstanceOf('Tmdb\Model\Tv', $tv);
}
}

protected function getFactoryClass()
{
return 'Tmdb\Factory\FindFactory';
}
}
16 changes: 16 additions & 0 deletions test/Tmdb/Tests/Factory/GenreFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ public function shouldConstructGenres()
}
}

/**
* @test
*/
public function shouldFilter()
{
$factory = $this->getFactory();
$data = $this->loadByFile('genre/result.json');

$collection = $factory->createCollection($data['genres']);
$filteredGenres = $collection->filterId(self::GENRE_ID);

foreach($filteredGenres as $filteredGenre) {
$this->assertEquals('Action', $filteredGenre->getName());
}
}

protected function getFactoryClass()
{
return 'Tmdb\Factory\GenreFactory';
Expand Down
Loading

0 comments on commit cea794f

Please sign in to comment.