Skip to content

Commit

Permalink
Extending more models and creating some more "logical" approaches to …
Browse files Browse the repository at this point in the history
…different type of people (person).

- Updated Genres, added a filter capability.
  • Loading branch information
wtfzdotnet committed Nov 2, 2013
1 parent 14a13d3 commit 02693b1
Show file tree
Hide file tree
Showing 15 changed files with 1,685 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/composer.lock
/*.php
/.idea/
/examples/
20 changes: 20 additions & 0 deletions lib/Tmdb/Api/Genres.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@
class Genres
extends AbstractApi
{
/**
* Get the list of genres, and return one by id
*
* @param integer $id
* @param array $options
* @return mixed
*/
public function getGenre($id, array $options = array())
{
$response = $this->getGenres();

foreach($response['genres'] as $genre) {
if ($id == $genre['id']) {
return $genre;
}
}

return null;
}

/**
* Get the list of genres.
*
Expand Down
114 changes: 113 additions & 1 deletion lib/Tmdb/Model/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

use Tmdb\Client;
use Tmdb\Exception\RuntimeException;
use Tmdb\Model\Common\Genres;
use Tmdb\Model\Common\Images;
use Tmdb\Model\Common\People;

class AbstractModel {
protected static $_properties;
Expand Down Expand Up @@ -69,7 +72,10 @@ public function hydrate(array $data = array())
if (!empty($data)) {
foreach ($data as $k => $v) {
if (in_array($k, static::$_properties)) {
$method = sprintf('set%s', ucfirst($k));

$method = $this->camelize(
sprintf('set_%s', $k)
);

if (!method_exists($this, $method)) {
throw new RuntimeException(sprintf(
Expand All @@ -86,4 +92,110 @@ public function hydrate(array $data = array())

return $this;
}

/**
* Collect all images from an `image` array ( containing e.g. posters / profiles etc. )
*
* @param $client
* @param array $collection
* @return Images
*/
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 People
*/
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 all people from an array
*
* @param $client
* @param array $collection
* @return People
*/
protected function collectGenres($client, array $collection = array())
{
$genres = new Genres();

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

return $genres;
}

/**
* Transforms an under_scored_string to a camelCasedOne
*
* @see https://gist.github.com/troelskn/751517
*
* @param $candidate
* @return string
*/
private function camelize($candidate)
{
return lcfirst(
implode('',
array_map('ucfirst',
array_map('strtolower',
explode('_', $candidate
)
)
)
)
);
}

/**
* Transforms a camelCasedString to an under_scored_one
*
* @see https://gist.github.com/troelskn/751517
*
* @param $camelized
* @return string
*/
private function uncamelize($camelized) {
return implode('_',
array_map('strtolower',
preg_split('/([A-Z]{1}[^A-Z]*)/', $camelized, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY)
)
);
}

}
17 changes: 17 additions & 0 deletions lib/Tmdb/Model/Common/Changes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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 Images extends Collection {

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

use Guzzle\Common\Collection as GuzzleCollection;

class Collection extends GuzzleCollection {

}
54 changes: 54 additions & 0 deletions lib/Tmdb/Model/Common/Genres.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\Model\Common;

use Tmdb\Model\Genre;

class Genres extends Collection {

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

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

return null;
}

/**
* Add a genre to the collection
*
* @param Genre $genre
*/
public function addGenre(Genre $genre)
{
$this->data[] = $genre;
}
}
54 changes: 54 additions & 0 deletions lib/Tmdb/Model/Common/Images.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\Model\Common;

use Tmdb\Model\Image;

class Images extends Collection {

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

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

return null;
}

/**
* Add a image to the collection
*
* @param Image $image
*/
public function addImage(Image $image)
{
$this->data[] = $image;
}
}
54 changes: 54 additions & 0 deletions lib/Tmdb/Model/Common/People.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\Model\Common;

use Tmdb\Model\Person;

class People extends Collection {

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

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

return null;
}

/**
* Add a person to the collection
*
* @param Person $person
*/
public function addPerson(Person $person)
{
$this->data[] = $person;
}
}
Loading

0 comments on commit 02693b1

Please sign in to comment.