Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TV Season: add translation #270

Open
wants to merge 6 commits into
base: 4.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/Tmdb/Api/TvSeason.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,21 @@ public function getChanges($tvshow_id, $season_number, array $parameters = [], a
{
return $this->get(sprintf('tv/%s/season/%s/changes', $tvshow_id, $season_number), $parameters, $headers);
}

/**
* Look up a TV season's changes by season ID.
* This method is used in conjunction with the /tv/{id}/changes method.
*
* This method uses the season_id value found in the change entries.
*
* @param integer $tvshow_id
* @param integer $season_number
* @param array $parameters
* @param array $headers
* @return mixed
*/
public function getTranslations($tvshow_id, $season_number, array $parameters = [], array $headers = [])
{
return $this->get(sprintf('tv/%s/season/%s/translations', $tvshow_id, $season_number), $parameters, $headers);
}
}
11 changes: 10 additions & 1 deletion lib/Tmdb/Factory/TvSeasonFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
use Tmdb\Factory\People\CastFactory;
use Tmdb\Factory\People\CrewFactory;
use Tmdb\HttpClient\HttpClient;
use Tmdb\Model\AbstractModel;
use Tmdb\Model\Common\ExternalIds;
use Tmdb\Model\Common\GenericCollection;
use Tmdb\Model\Common\Translation;
use Tmdb\Model\Person\CastMember;
use Tmdb\Model\Person\CrewMember;
use Tmdb\Model\Tv\Season;
Expand Down Expand Up @@ -157,6 +157,15 @@ public function create(array $data = []): ?Season
$tvSeason->setChanges($this->getChangesFactory()->createCollection($data['changes']));
}

if (array_key_exists('translations', $data) && $data['translations'] !== null) {
$tvSeason->setTranslations(
$this->createGenericCollection(
$data['translations']['translations'] ?? [],
new Translation()
)
);
}

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

Expand Down
27 changes: 26 additions & 1 deletion lib/Tmdb/Model/Tv/Season.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Tmdb\Model\Collection\Videos;
use Tmdb\Model\Common\ExternalIds;
use Tmdb\Model\Common\GenericCollection;
use Tmdb\Model\Common\Translation;
use Tmdb\Model\Image\PosterImage;

/**
Expand Down Expand Up @@ -75,6 +76,10 @@ class Season extends AbstractModel
* @var Changes
*/
protected $changes;
/**
* @var GenericCollection|Translation[]
*/
protected $translations;
/**
* @var DateTime
*/
Expand Down Expand Up @@ -115,6 +120,7 @@ public function __construct()
$this->episodes = new GenericCollection();
$this->videos = new Videos();
$this->changes = new Changes();
$this->translations = new GenericCollection();
}

/**
Expand Down Expand Up @@ -319,7 +325,7 @@ public function setPosterImage($poster)
}

/**
* @return ?PosterImage
* @return PosterImage
*/
public function getPosterImage()
{
Expand Down Expand Up @@ -363,4 +369,23 @@ public function setChanges($changes)

return $this;
}

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

/**
* @param GenericCollection $changes
* @return self
*/
public function setTranslations($changes)
{
$this->translations = $changes;

return $this;
}
}
25 changes: 25 additions & 0 deletions lib/Tmdb/Repository/TvSeasonRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,29 @@ public function getVideos($tvShow, $season, array $parameters = [], array $heade

return $season->getVideos();
}

/**
* Get the videos that have been added to a TV season (trailers, teasers, etc...)
*
* @param $tvShow
* @param $season
* @param $parameters
* @param $headers
* @return Videos|Video[]
*/
public function getTranslations($tvShow, $season, array $parameters = [], array $headers = [])
{
if ($tvShow instanceof Tv) {
$tvShow = $tvShow->getId();
}

if ($season instanceof Season) {
$season = $season->getSeasonNumber();
}

$data = $this->getApi()->getTranslations($tvShow, $season, $this->parseQueryParameters($parameters), $headers);
$season = $this->getFactory()->create(['translations' => $data]);

return $season->getTranslations();
}
}
11 changes: 11 additions & 0 deletions test/Tmdb/Tests/Api/TvSeasonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ public function shouldGetSeasonChanges()
$this->assertLastRequestIsWithPathAndMethod('/3/tv/' . self::TV_ID . '/season/' . self::SEASON_ID . '/changes');
}

/**
* @test
*/
public function shouldGetSeasonTranslations()
{
$api = $this->getApiWithMockedHttpAdapter();

$api->getTranslations(self::TV_ID, self::SEASON_ID);
$this->assertLastRequestIsWithPathAndMethod('/3/tv/' . self::TV_ID . '/season/' . self::SEASON_ID . '/translations');
}

protected function getApiClass()
{
return 'Tmdb\Api\TvSeason';
Expand Down
8 changes: 6 additions & 2 deletions test/Tmdb/Tests/Factory/TvSeasonFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function setUp(): void
* @var TvSeasonFactory $factory
*/
$factory = $this->getFactory();
$data = $this->loadByFile('tv/season/all.json');
$data = $this->loadByFile('tv/season/all.json');

/**
* @var Season $this->season
Expand All @@ -52,6 +52,7 @@ public function shouldConstructTvSeason()
$this->assertInstanceOf('Tmdb\Model\Collection\Images', $this->season->getImages());
$this->assertInstanceOf('Tmdb\Model\Common\GenericCollection', $this->season->getEpisodes());
$this->assertInstanceOf('Tmdb\Model\Image\PosterImage', $this->season->getPosterImage());
$this->assertInstanceOf('Tmdb\Model\Common\GenericCollection', $this->season->getTranslations());
}

/**
Expand Down Expand Up @@ -88,7 +89,10 @@ public function shouldBeFunctional()
$this->assertEquals(3573, $this->season->getId());
$this->assertEquals('/rCdISteF1GPvPsy0a5L0LDffjtP.jpg', $this->season->getPosterPath());
$this->assertEquals(2, $this->season->getSeasonNumber());
}
$this->assertEquals("Sein Ruf eilt dem zum Meth-Dealer gewordenen Lehrer Walter voraus – das erregt die Aufmerksamkeit rivalisierender Kartelle und gefährdet seinen Schwager, einen Drogencop.",
$this->season->getTranslations()->filterLanguage('de')->getIterator()->current()->getData()['overview']
);
}

protected function getFactoryClass()
{
Expand Down
1 change: 1 addition & 0 deletions test/Tmdb/Tests/Model/Tv/SeasonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function shouldConstructTvSeason()
'getImages' => 'Tmdb\Model\Collection\Images',
'getEpisodes' => 'Tmdb\Model\Common\GenericCollection',
'getVideos' => 'Tmdb\Model\Collection\Videos',
'getTranslations' => 'Tmdb\Model\Common\GenericCollection',
]
);
}
Expand Down
17 changes: 17 additions & 0 deletions test/Tmdb/Tests/Repository/TvSeasonRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ public function shouldGetCredits()
$this->assertLastRequestIsWithPathAndMethod('/3/tv/' . self::TV_ID . '/season/' . self::SEASON_NUMBER . '/credits');
}

/**
* @test
*/
public function shouldGetTranslations()
{
$repository = $this->getRepositoryWithMockedHttpAdapter();

$tv = new Tv();
$tv->setId(self::TV_ID);

$season = new Tv\Season();
$season->setSeasonNumber(self::SEASON_NUMBER);

$repository->getTranslations($tv, $season);
$this->assertLastRequestIsWithPathAndMethod('/3/tv/' . self::TV_ID . '/season/' . self::SEASON_NUMBER . '/translations');
}

/**
* @test
*/
Expand Down
Loading