Skip to content

Commit

Permalink
Refactoring the models big-time
Browse files Browse the repository at this point in the history
- Removed any knowledge of collaborators from the models.
- Added factories to create the models by the retrieved data from the API.
- Added a Movie repository that is able to load an movie providing the client and id ( by calling the API segment and passing the data on to the factory ).
- Starting to refactor some bits of the API segment, removing the ->api() function call in the client. ( For now I just added getMovieApi(), but the whole api() method will vanish ).
- Added a GenericCollectionFactory to create collections of objects we don't care much about (yet) to filter or ...?
- Created some extra submodels for Movies
  • Loading branch information
wtfzdotnet committed Nov 18, 2013
1 parent 9859dbd commit f067911
Show file tree
Hide file tree
Showing 25 changed files with 837 additions and 369 deletions.
91 changes: 91 additions & 0 deletions examples/movies/model/all.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?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\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,
));

$repository = new \Tmdb\Repository\MovieRepository($client);
$movie = $repository->load(87421, array($append));

echo $movie->getTitle() . "\n";

echo "Alternative Titles\n";

foreach($movie->getAlternativeTitles() as $title) {
printf(" - %s [%s]\n", $title->getTitle(), $title->getIso31661());
}

echo "Cast\n";

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

echo "Crew\n";

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

echo "Images\n";

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

echo "Genres\n";

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

echo "Keywords\n";

foreach($movie->getKeywords() as $keyword) {
printf(" - %s [%s]\n", $keyword->getName(), $keyword->getId());
}

echo "Releases\n";

foreach($movie->getReleases() as $release) {
printf(" - %s on %s\n", $release->getIso31661(), $release->getReleaseDate());
}

echo "Translations\n";

foreach($movie->getTranslations() as $translation) {
printf(" - %s\n", $translation->getName());
}

echo "Trailers\n";

foreach($movie->getTrailers() as $trailer) {
printf(" - %s\n", $trailer->getUrl());
}
41 changes: 4 additions & 37 deletions examples/movies/model/get.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,9 @@
$token = new \Tmdb\ApiToken(TMDB_API_KEY);
$client = new \Tmdb\Client($token);

$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,
));
$repository = new \Tmdb\Repository\MovieRepository($client);
$movie = $repository->load(87421);

$movie = \Tmdb\Factory\MovieFactory::load($client, 87421, array($append));
printf('<h1>%s</h1>', $movie->getTitle());

echo $movie->getTitle() . "\n";

echo "Cast\n";

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

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

echo "Images\n";

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

echo "Genres\n";

foreach($movie->getGenres() as $genre) {
printf(" - %s\n", $genre->getName());
}
printf('<p>%s</p>', $movie->getOverview());
8 changes: 8 additions & 0 deletions lib/Tmdb/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ public function setToken(Token $token)
return $this;
}

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

/**
* Return the relevant API object
*
Expand Down
90 changes: 90 additions & 0 deletions lib/Tmdb/Common/ObjectHydrator.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\Common;

use Tmdb\Exception\RuntimeException;
use Tmdb\Model\AbstractModel;

class ObjectHydrator {
/**
* Hydrate the object with data
*
* @param AbstractModel $object
* @param array $data
* @return $this
* @throws RuntimeException
*/
public static function hydrate(AbstractModel $object, $data = array())
{
if (!empty($data)) {
foreach ($data as $k => $v) {
if (in_array($k, $object::$_properties)) {

$method = self::camelize(
sprintf('set_%s', $k)
);

if (!method_exists($object, $method)) {
throw new RuntimeException(sprintf(
'Trying to call method "%s" on "%s" but it does not exist or is private.',
$method,
get_class($object)
));
}

$object->$method($v);
}
}
}

return $object;
}

/**
* 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)
)
);
}
}
88 changes: 4 additions & 84 deletions lib/Tmdb/Factory/AbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
*/
namespace Tmdb\Factory;

use Tmdb\Exception\RuntimeException;
use Tmdb\Common\ObjectHydrator;
use Tmdb\Model\AbstractModel;
use Tmdb\Model\Common\QueryParameter\QueryParameterInterface;

abstract class AbstractFactory {
/**
Expand All @@ -33,94 +32,15 @@ abstract public static function create(array $data = array());
*/
abstract public static function createCollection(array $data = array());

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

/**
* Hydrate the object with data
*
* @param AbstractModel $object
* @param array $data
* @return $this
* @throws RuntimeException
*/
public function hydrate(AbstractModel $object, array $data = array())
{
if (!empty($data)) {
foreach ($data as $k => $v) {
if (in_array($k, $object::$_properties)) {

$method = self::camelize(
sprintf('set_%s', $k)
);

if (!method_exists($object, $method)) {
throw new RuntimeException(sprintf(
'Trying to call method "%s" on "%s" but it does not exist or is private.',
$method,
get_class($object)
));
}

$object->$method($v);
}
}
}

return $object;
}

/**
* Transforms an under_scored_string to a camelCasedOne
*
* @see https://gist.github.com/troelskn/751517
*
* @param $candidate
* @return string
* @return AbstractModel
*/
private function camelize($candidate)
public function hydrate(AbstractModel $object, $data = array())
{
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)
)
);
return ObjectHydrator::hydrate($object, $data);
}
}
Loading

0 comments on commit f067911

Please sign in to comment.