Skip to content

Commit

Permalink
Implemented Timezones first citizen, fixes php-tmdb#19.
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Apr 3, 2014
1 parent 93842bb commit d458133
Show file tree
Hide file tree
Showing 8 changed files with 1,744 additions and 4 deletions.
8 changes: 5 additions & 3 deletions examples/timezones/model/get.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
$token = new \Tmdb\ApiToken(TMDB_API_KEY);
$client = new \Tmdb\Client($token);

$repository = new \Tmdb\Repository\CollectionRepository($client);
$collection = $repository->load(10);
$repository = new \Tmdb\Repository\TimezoneRepository($client);
$timezones = $repository->getTimezones();

var_dump($collection);
var_dump($timezones->getCountry('NL')->supports('Europe/Amsterdam'));

var_dump($timezones);
56 changes: 56 additions & 0 deletions lib/Tmdb/Factory/TimezoneFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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\Collection\Timezones;
use Tmdb\Model\Timezone;

/**
* Class TimezoneFactory
* @package Tmdb\Factory
*/
class TimezoneFactory extends AbstractFactory
{
/**
* @param array $data
*
* @return Timezone\CountryTimezone
*/
public function create(array $data = array())
{
return $this->hydrate(new Timezone\CountryTimezone(), $data);
}

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

foreach ($data as $foobar_data) {
foreach ($foobar_data as $iso_3166_1 => $timezones) {
$country = new Timezone\CountryTimezone();
$country->setIso31661($iso_3166_1);

foreach ($timezones as $timezone) {
$country->getTimezones()->add(null, $timezone);
}

$collection->add(null, $country);
}
}

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

use Tmdb\Model\Common\GenericCollection;
use Tmdb\Model\Timezone\CountryTimezone;

/**
* Class Timezones
* @package Tmdb\Model\Collection
*/
class Timezones extends GenericCollection
{
/**
* Returns all countries with timezones
*
* @return array
*/
public function getCountries()
{
return $this->data;
}

/**
* Retrieve a country from the collection
*
* @param $id
* @return CountryTimezone|null
*/
public function getCountry($id)
{
foreach ($this->data as $country) {
if (strtoupper($id) == (string) $country) {
return $country;
}
}

return null;
}

/**
* Add a timezone to the collection
*
* @param CountryTimezone $country
*/
public function addCountry($country)
{
$this->data[] = $country;
}
}
4 changes: 3 additions & 1 deletion lib/Tmdb/Model/Common/GenericCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,10 @@ public function add($key, $value)
$key = spl_object_hash($value);
}

if (!array_key_exists($key, $this->data)) {
if (!array_key_exists($key, $this->data) && null !== $key) {
$this->data[$key] = $value;
} elseif (!array_key_exists($key, $this->data) && null == $key) {
$this->data[] = $value;
} elseif (is_array($this->data[$key])) {
$this->data[$key][] = $value;
} else {
Expand Down
96 changes: 96 additions & 0 deletions lib/Tmdb/Model/Timezone/CountryTimezone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?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\Timezone;

use Tmdb\Model\AbstractModel;
use Tmdb\Model\Collection\Timezones;
use Tmdb\Model\Common\GenericCollection;

/**
* Class Timezone
* @package Tmdb\Model\Certification
*/
class CountryTimezone extends AbstractModel
{
/**
* @var string
*/
private $iso31661;

/**
* @var Timezones
*/
private $timezones;

public function __construct()
{
$this->timezones = new GenericCollection();
}

/**
* @param \Tmdb\Model\Collection\Timezones $timezones
* @return $this
*/
public function setTimezones($timezones)
{
$this->timezones = $timezones;

return $this;
}

/**
* @return \Tmdb\Model\Collection\Timezones
*/
public function getTimezones()
{
return $this->timezones;
}

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

return $this;
}

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

/**
* Verify if a country supports a certain timezone
*
* @param $timezone
* @return mixed
*/
public function supports($timezone)
{
return false !== $this->timezones->hasValue($timezone);
}

/**
* @return string
*/
public function __toString()
{
return $this->iso31661;
}
}
56 changes: 56 additions & 0 deletions lib/Tmdb/Repository/TimezoneRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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\Repository;

use Tmdb\Factory\TimezoneFactory;
use Tmdb\Model\Collection\Timezones;

/**
* Class TimezoneRepository
* @package Tmdb\Repository
* @see http://docs.themoviedb.apiary.io/#timezones
*/
class TimezoneRepository extends AbstractRepository
{
/**
* Get the list of supported timezones for the API methods that support them.
*
* @param $parameters
* @param $headers
* @return Timezones
*/
public function getTimezones(array $parameters = array(), array $headers = array())
{
$data = $this->getApi()->getTimezones($this->parseQueryParameters($parameters), $headers);

return $this->getFactory()->createCollection($data);
}

/**
* Return the Collection API Class
*
* @return \Tmdb\Api\Timezones
*/
public function getApi()
{
return $this->getClient()->getTimezonesApi();
}

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

class TimezoneFactoryTest extends TestCase
{
private $data;

public function setUp()
{
$this->data = $this->loadByFile('timezones/get.json');
}

/**
* @test
*/
public function shouldConstructTimezones()
{
/**
* @var TimezoneFactory $factory
*/
$factory = $this->getFactory();
$timezonesCollection = $factory->createCollection($this->data);

$this->assertInstanceOf('Tmdb\Model\Collection\Timezones', $timezonesCollection);

/**
* @var CountryTimezone $countryTimezone
*/
foreach ($timezonesCollection as $countryTimezone) {
$this->assertInstanceOf('Tmdb\Model\Timezone\CountryTimezone', $countryTimezone);
$this->assertNotEmpty($countryTimezone->getIso31661());

foreach ($countryTimezone->getTimezones() as $timezone) {
$this->assertNotEmpty($timezone);
}
}
}

protected function getFactoryClass()
{
return 'Tmdb\Factory\TimezoneFactory';
}
}
Loading

0 comments on commit d458133

Please sign in to comment.