Skip to content

Commit

Permalink
Added support for episode groups
Browse files Browse the repository at this point in the history
  • Loading branch information
nouser committed Oct 6, 2021
1 parent a1fe153 commit 78e7c65
Show file tree
Hide file tree
Showing 15 changed files with 1,069 additions and 4 deletions.
11 changes: 11 additions & 0 deletions lib/Tmdb/Api/Tv.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,15 @@ public function getAlternativeTitles($id)
{
return $this->get('tv/' . $id . '/alternative_titles');
}

/**
* Get the alternative titles for a specific show ID.
*
* @param integer $id
* @return mixed
*/
public function getEpisodeGroups($id)
{
return $this->get('tv/' . $id . '/episode_groups');
}
}
47 changes: 47 additions & 0 deletions lib/Tmdb/Api/TvEpisodeGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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 sheriffmarley
* @copyright (c) 2013, Michael Roterman
* @version 4.0.0
*/

namespace Tmdb\Api;

/**
* Class TvEpisode
* @package Tmdb\Api
* @see http://docs.themoviedb.apiary.io/#tvepisodes
*/
class TvEpisodeGroup extends AbstractApi
{
/**
* Get the primary information about a TV episode group by it's id.
*
* @param $episode_group
* @param array $parameters
* @param array $headers
* @return mixed
*/
public function getEpisodeGroup(
$episode_group,
array $parameters = [],
array $headers = []
) {
return $this->get(
sprintf(
'tv/episode_group/%s',
$episode_group
),
$parameters,
$headers
);
}

}
8 changes: 8 additions & 0 deletions lib/Tmdb/ApiMethodsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,12 @@ public function getTvEpisodeApi()
{
return new Api\TvEpisode($this);
}

/**
* @return Api\TvEpisodeGroup
*/
public function getTvEpisodeGroupApi()
{
return new Api\TvEpisodeGroup($this);
}
}
85 changes: 85 additions & 0 deletions lib/Tmdb/Factory/TvEpisodeGroupFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?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 sheriffmarley
* @copyright (c) 2013, Michael Roterman
* @version 4.0.0
*/

namespace Tmdb\Factory;

use Tmdb\Model\Network;
use Tmdb\Model\AbstractModel;
use Tmdb\Model\Tv\EpisodeGroup;
use Tmdb\HttpClient\HttpClient;
use Tmdb\Model\Common\GenericCollection;

/**
* Class TvEpisodeGroupFactory
* @package Tmdb\Factory
*/
class TvEpisodeGroupFactory extends AbstractFactory
{

/**
* @var TvEpisodeGroupsFactory
*/
private $tvEpisodeGroupsFactory;

/**
* Constructor
*
* @param HttpClient $httpClient
*/
public function __construct(HttpClient $httpClient)
{
$this->tvEpisodeGroupsFactory = new TvEpisodeGroupsFactory($httpClient);

parent::__construct($httpClient);
}

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

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

return $collection;
}

/**
* {@inheritdoc}
*
* @return AbstractModel|null
*/
public function create(array $data = []): ?AbstractModel
{
if (!$data) {
return null;
}

$episodeGroup = new EpisodeGroup();

if(array_key_exists('network', $data) && !is_null($data['network'])){
$episodeGroup->setNetwork($this->hydrate(new Network(), $data['network']));
}

if (array_key_exists('groups', $data) && $data['groups'] !== null) {
$episodeGroup->setGroups($this->tvEpisodeGroupsFactory->createCollection($data['groups']));
}

return $this->hydrate($episodeGroup, $data);
}

}
99 changes: 99 additions & 0 deletions lib/Tmdb/Factory/TvEpisodeGroupsFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?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 sheriffmarley
* @copyright (c) 2013, Michael Roterman
* @version 4.0.0
*/

namespace Tmdb\Factory;

use Tmdb\Model\AbstractModel;
use Tmdb\HttpClient\HttpClient;
use Tmdb\Model\Tv\TvEpisodeGroup;
use Tmdb\Model\Common\GenericCollection;

/**
* Class TvEpisodeGroupsFactory
* @package Tmdb\Factory
*/
class TvEpisodeGroupsFactory extends AbstractFactory
{

/**
* @var TvEpisodeFactory
*/
private $tvEpisodeFactory;

/**
* Constructor
*
* @param HttpClient $httpClient
*/
public function __construct(HttpClient $httpClient)
{
$this->tvEpisodeFactory = new TvEpisodeFactory($httpClient);

parent::__construct($httpClient);
}

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

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

return $collection;
}

/**
* {@inheritdoc}
*
* @return AbstractModel|null
*/
public function create(array $data = []): ?AbstractModel
{
if (!$data) {
return null;
}

$tvEpisodeGroup = new TvEpisodeGroup();

/** Episodes */
if (array_key_exists('episodes', $data) && $data['episodes'] !== null) {
$tvEpisodeGroup->setEpisodes($this->getTvEpisodeFactory()->createCollection($data['episodes']));
}

return $this->hydrate($tvEpisodeGroup, $data);
}

/**
* @return TvEpisodeFactory
*/
public function getTvEpisodeFactory()
{
return $this->tvEpisodeFactory;
}

/**
* @param TvEpisodeFactory $tvEpisodeFactory
* @return $this
*/
public function setTvEpisodeFactory($tvEpisodeFactory)
{
$this->tvEpisodeFactory = $tvEpisodeFactory;

return $this;
}
}
22 changes: 19 additions & 3 deletions lib/Tmdb/Factory/TvFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Tmdb\Model\Common\SpokenLanguage;
use Tmdb\Model\Common\Translation;
use Tmdb\Model\Company;
use Tmdb\Model\Network;
use Tmdb\Model\Person\CastMember;
use Tmdb\Model\Person\CrewMember;
use Tmdb\Model\Tv;
Expand Down Expand Up @@ -249,7 +250,7 @@ public function create(array $data = []): ?AbstractModel
$watchProviders = new GenericCollection();
foreach ($data['watch/providers']['results'] as $iso31661 => $countryWatchData) {
$countryWatchData['iso_3166_1'] = $iso31661;

foreach (['flatrate', 'rent', 'buy'] as $providerType) {
$typeProviders = new GenericCollection();
foreach ($countryWatchData[$providerType] ?? [] as $providerData) {
Expand All @@ -259,14 +260,14 @@ public function create(array $data = []): ?AbstractModel
if (isset($providerData['provider_name'])) {
$providerData['name'] = $providerData['provider_name'];
}

$providerData['iso_3166_1'] = $iso31661;
$providerData['type'] = $providerType;
$typeProviders->add(null, $this->hydrate(new Watch\Provider(), $providerData));
}
$countryWatchData[$providerType] = $typeProviders;
}

$watchProviders->add($iso31661, $this->hydrate(new Watch\Providers(), $countryWatchData));
}
$tvShow->setWatchProviders($watchProviders);
Expand Down Expand Up @@ -343,6 +344,21 @@ public function create(array $data = []): ?AbstractModel
);
}

if (array_key_exists('episode_groups', $data) && array_key_exists('results', $data['episode_groups'])) {
$episodeGroupCollection = new GenericCollection();

foreach ($data['episode_groups']['results'] as $episodeGroup) {


if(!is_null($episodeGroup['network'])){
$episodeGroup['network'] = $this->hydrate(new Network(), $episodeGroup['network']);
}

$episodeGroupCollection->add(null, $this->hydrate(new Tv\EpisodeGroups(), $episodeGroup));
}
$tvShow->setEpisodeGroups($episodeGroupCollection);
}

return $this->hydrate($tvShow, $data);
}

Expand Down
25 changes: 25 additions & 0 deletions lib/Tmdb/Model/Tv.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ class Tv extends AbstractModel
* @var GenericCollection
*/
private $watchProviders;
/**
* @var GenericCollection
*/
protected $episodeGroups;

/**
* Constructor
Expand All @@ -263,6 +267,7 @@ public function __construct()
$this->alternativeTitles = new GenericCollection();
$this->languages = new GenericCollection();
$this->watchProviders = new GenericCollection();
$this->episodeGroups = new GenericCollection();
}

/**
Expand Down Expand Up @@ -1069,4 +1074,24 @@ public function setWatchProviders($watchProviders)

return $this;
}

/**
* @return GenericCollection
*/
public function getEpisodeGroups(): GenericCollection
{
return $this->episodeGroups;
}

/**
* @param GenericCollection $episodeGroups
* @return Tv
*/
public function setEpisodeGroups(GenericCollection $episodeGroups): Tv
{
$this->episodeGroups = $episodeGroups;

return $this;
}

}

0 comments on commit 78e7c65

Please sign in to comment.