Skip to content

Commit

Permalink
Removing static methods to allow for easier testing later on.
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Jan 27, 2014
1 parent 2d5a111 commit 2f6da48
Show file tree
Hide file tree
Showing 29 changed files with 906 additions and 186 deletions.
33 changes: 28 additions & 5 deletions lib/Tmdb/Factory/AbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,46 @@
use Tmdb\Common\ObjectHydrator;
use Tmdb\Factory\Common\GenericCollectionFactory;
use Tmdb\Model\AbstractModel;
use Tmdb\Model\Common\GenericCollection;

abstract class AbstractFactory {
abstract class AbstractFactory implements FactoryInterface {
/**
* Convert an array to an hydrated object
*
* @param array $data
* @return AbstractModel
*/
abstract public static function create(array $data = array());
abstract public function create(array $data = array());

/**
* Convert an array with an collection of items to an hydrated object collection
*
* @param array $data
* @return GenericCollectionFactory
*/
abstract public static function createCollection(array $data = array());
abstract public function createCollection(array $data = array());

/**
* Create a generic collection of data and map it on the class by it's static parameter $_properties
*
* @param array $data
* @param $class
* @return GenericCollection
*/
protected function createGenericCollection(array $data = array(), $class)
{
if (is_object($class)) {
$class = get_class($class);
}

$collection = new GenericCollection();

foreach($data as $item) {
$collection->add(null, $this->hydrate(new $class(), $item));
}

return $collection;
}

/**
* Hydrate the object with data
Expand All @@ -40,8 +63,8 @@ abstract public static function createCollection(array $data = array());
* @param array $data
* @return AbstractModel
*/
public function hydrate(AbstractModel $object, $data = array())
protected function hydrate(AbstractModel $object, $data = array())
{
return ObjectHydrator::hydrate($object, $data);
}
}
}
77 changes: 67 additions & 10 deletions lib/Tmdb/Factory/CollectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,105 @@
*/
namespace Tmdb\Factory;

use Tmdb\Model\Common\GenericCollection;

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

/**
* @var ImageFactory
*/
private $imageFactory;

/**
* Constructor
*/
public function __construct()
{
$this->movieFactory = new MovieFactory();
$this->imageFactory = new ImageFactory();
}

/**
* {@inheritdoc}
* @return \Tmdb\Model\Collection
*/
public static function create(array $data = array())
public function create(array $data = array())
{
$collection = new \Tmdb\Model\Collection();

if (array_key_exists('parts', $data)) {
$collection->setParts(
MovieFactory::createCollection($data['parts'])
$this->getMovieFactory()->createCollection($data['parts'])
);
}

if (array_key_exists('backdrop_path', $data)) {
$collection->setBackdrop(ImageFactory::createFromPath($data['backdrop_path'], 'backdrop_path'));
$collection->setBackdrop($this->getImageFactory()->createFromPath($data['backdrop_path'], 'backdrop_path'));
}

if (array_key_exists('images', $data)) {
$collection->setImages(ImageFactory::createCollectionFromMovie($data['images']));
$collection->setImages($this->getImageFactory()->createCollectionFromMovie($data['images']));
}

if (array_key_exists('poster_path', $data)) {
$collection->setPoster(ImageFactory::createFromPath($data['poster_path'], 'poster_path'));
$collection->setPoster($this->getImageFactory()->createFromPath($data['poster_path'], 'poster_path'));
}

return parent::hydrate($collection, $data);
return $this->hydrate($collection, $data);
}

/**
* {@inheritdoc}
*/
public static function createCollection(array $data = array())
public function createCollection(array $data = array())
{
$collection = new \Tmdb\Model\Common\Collection();
$collection = new GenericCollection();

foreach($data as $item) {
$collection->add(null, self::create($item));
$collection->add(null, $this->create($item));
}

return $collection;
}
}

/**
* @param \Tmdb\Factory\ImageFactory $imageFactory
* @return $this
*/
public function setImageFactory($imageFactory)
{
$this->imageFactory = $imageFactory;
return $this;
}

/**
* @return \Tmdb\Factory\ImageFactory
*/
public function getImageFactory()
{
return $this->imageFactory;
}

/**
* @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;
}
}
14 changes: 10 additions & 4 deletions lib/Tmdb/Factory/Common/GenericCollectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,29 @@
use Tmdb\Common\ObjectHydrator;
use Tmdb\Model\Common\GenericCollection;

/**
* @deprecated
*
* Class GenericCollectionFactory
* @package Tmdb\Factory\Common
*/
class GenericCollectionFactory {
/**
* @param array $data
* @param $class
* @return GenericCollection
*/
public static function create(array $data = array(), $class)
public function create(array $data = array(), $class)
{
return self::createCollection($data, $class);
return $this->createCollection($data, $class);
}

/**
* @param array $data
* @param $class
* @return GenericCollection
*/
public static function createCollection(array $data = array(), $class)
public function createCollection(array $data = array(), $class)
{
if (is_object($class)) {
$class = get_class($class);
Expand All @@ -45,4 +51,4 @@ public static function createCollection(array $data = array(), $class)

return $collection;
}
}
}
10 changes: 5 additions & 5 deletions lib/Tmdb/Factory/CompanyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ class CompanyFactory extends AbstractFactory
/**
* {@inheritdoc}
*/
public static function create(array $data = array())
public function create(array $data = array())
{
return parent::hydrate(new Company(), $data);
return $this->hydrate(new Company(), $data);
}

/**
* {@inheritdoc}
*/
public static function createCollection(array $data = array())
public function createCollection(array $data = array())
{
$collection = new GenericCollection();

foreach($data as $item) {
$collection->add(null, self::create($item));
$collection->add(null, $this->create($item));
}

return $collection;
}
}
}
18 changes: 5 additions & 13 deletions lib/Tmdb/Factory/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,25 @@
*/
namespace Tmdb\Factory;

use Tmdb\Model\Common\GenericCollection;
use Tmdb\Model\Configuration;

class ConfigurationFactory extends AbstractFactory
{
/**
* {@inheritdoc}
*/
public static function create(array $data = array())
public function create(array $data = array())
{
$config = new Configuration();

return parent::hydrate($config, $data);
return $this->hydrate($config, $data);
}

/**
* {@inheritdoc}
*/
public static function createCollection(array $data = array())
public function createCollection(array $data = array())
{
$collection = new GenericCollection();

foreach($data as $item) {
$collection->add(null, self::create($item));
}

return $collection;
return null;
}

}
}
17 changes: 17 additions & 0 deletions lib/Tmdb/Factory/FactoryInterface.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\Factory;

interface FactoryInterface {
function create();
}
10 changes: 5 additions & 5 deletions lib/Tmdb/Factory/GenreFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ class GenreFactory extends AbstractFactory
/**
* {@inheritdoc}
*/
public static function create(array $data = array())
public function create(array $data = array())
{
return parent::hydrate(new Genre(), $data);
return $this->hydrate(new Genre(), $data);
}

/**
* {@inheritdoc}
*/
public static function createCollection(array $data = array())
public function createCollection(array $data = array())
{
$collection = new Genres();

foreach($data as $item) {
$collection->add(null, self::create($item));
$collection->add(null, $this->create($item));
}

return $collection;
}
}
}
Loading

0 comments on commit 2f6da48

Please sign in to comment.