Skip to content

Commit

Permalink
Add support for watch providers to tv show objects
Browse files Browse the repository at this point in the history
  • Loading branch information
neildaniels committed Aug 27, 2021
1 parent d47bbfd commit de40074
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/Tmdb/Factory/TvFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Tmdb\Model\Person\CastMember;
use Tmdb\Model\Person\CrewMember;
use Tmdb\Model\Tv;
use Tmdb\Model\Watch;

/**
* Class TvFactory
Expand Down Expand Up @@ -244,6 +245,26 @@ public function create(array $data = []): ?AbstractModel
$tvShow->setNetworks($this->getNetworkFactory()->createCollection($data['networks']));
}

if (array_key_exists('watch/providers', $data) && array_key_exists('results', $data['watch/providers'])) {
$watchProviders = new GenericCollection();
foreach ($data['watch/providers']['results'] as $iso_31661 => $country_watch_data) {
$country_watch_data['iso_3166_1'] = $iso_31661;

foreach (['flatrate', 'rent', 'buy'] as $providerType) {
$typeProviders = new GenericCollection();
foreach ($country_watch_data[$providerType] ?? [] as $providerData) {
$providerData['iso_3166_1'] = $iso_31661;
$providerData['type'] = $providerType;
$typeProviders->add(null, $this->hydrate(new Watch\Provider(), $providerData));
}
$country_watch_data[$providerType] = $typeProviders;
}

$watchProviders->add($iso_31661, $this->hydrate(new Watch\Providers(), $country_watch_data));
}
$tvShow->setWatchProviders($watchProviders);
}

if (array_key_exists('videos', $data) && $data['videos'] !== null) {
$tvShow->setVideos($this->getVideoFactory()->createCollection($data['videos']));
}
Expand Down
24 changes: 24 additions & 0 deletions lib/Tmdb/Model/Tv.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ class Tv extends AbstractModel
* @var int
*/
private $voteCount;
/**
* @var GenericCollection
*/
private $watchProviders;

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

/**
Expand Down Expand Up @@ -1045,4 +1050,23 @@ public function setType($type)

return $this;
}

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

/**
* @param GenericCollection $watchProviders
* @return $this
*/
public function setWatchProviders($watchProviders)
{
$this->watchProviders = $watchProviders;

return $this;
}
}
1 change: 1 addition & 0 deletions lib/Tmdb/Model/Tv/QueryParameter/AppendToResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ class AppendToResponse extends BaseAppendToResponse
public const RECOMMENDATIONS = 'recommendations';
public const CONTENT_RATINGS = 'content_ratings';
public const ALTERNATIVE_TITLES = 'alternative_titles';
public const WATCH_PROVIDERS = 'watch/providers';
}
1 change: 1 addition & 0 deletions lib/Tmdb/Repository/TvRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function load($id, array $parameters = [], array $headers = [])
AppendToResponse::CONTENT_RATINGS,
AppendToResponse::ALTERNATIVE_TITLES,
AppendToResponse::VIDEOS,
AppendToResponse::WATCH_PROVIDERS,
])
]);
}
Expand Down

0 comments on commit de40074

Please sign in to comment.