1<?php
2/**
3 * This file is part of the Tmdb PHP API created by Michael Roterman.
4 *
5 * For the full copyright and license information, please view the LICENSE
6 * file that was distributed with this source code.
7 *
8 * @package Tmdb
9 * @author Michael Roterman <michael@wtfz.net>
10 * @copyright (c) 2013, Michael Roterman
11 * @version 0.0.1
12 */
13namespace Tmdb\Repository;
14
15use Tmdb\Exception\RuntimeException;
16use Tmdb\Factory\TvSeasonFactory;
17use Tmdb\Model\Collection\Videos;
18use Tmdb\Model\Common\Video;
19use \Tmdb\Model\Tv\Season\QueryParameter\AppendToResponse;
20use Tmdb\Model\Tv\Season;
21use Tmdb\Model\Tv;
22
23/**
24 * Class TvSeasonRepository
25 * @package Tmdb\Repository
26 * @see http://docs.themoviedb.apiary.io/#tvseasons
27 */
28class TvSeasonRepository extends AbstractRepository
29{
30    /**
31     * Load a tv season with the given identifier
32     *
33     * If you want to optimize the result set/bandwidth you should define the AppendToResponse parameter
34     *
35     * @param int|Tv $tvShow
36     * @param int|Season $season
37     * @param array $parameters
38     * @param array $headers
39     * @throws RuntimeException
40     * @return null|\Tmdb\Model\AbstractModel
41     */
42    public function load($tvShow, $season, array $parameters = [], array $headers = [])
43    {
44        if ($tvShow instanceof Tv) {
45            $tvShow = $tvShow->getId();
46        }
47
48        if ($season instanceof Season) {
49            $season = $season->getSeasonNumber();
50        }
51
52        if (null === $tvShow || null === $season) {
53            throw new RuntimeException('Not all required parameters to load an tv season are present.');
54        }
55
56        if (!isset($parameters['append_to_response'])) {
57            $parameters = array_merge($parameters, [
58                new AppendToResponse([
59                    AppendToResponse::CREDITS,
60                    AppendToResponse::EXTERNAL_IDS,
61                    AppendToResponse::IMAGES,
62                    AppendToResponse::CHANGES,
63                    AppendToResponse::VIDEOS
64                ])
65            ]);
66        }
67
68        $data = $this->getApi()->getSeason($tvShow, $season, $this->parseQueryParameters($parameters), $headers);
69
70        return $this->getFactory()->create($data);
71    }
72
73    /**
74     * Get the cast & crew information about a TV series.
75     *
76     * Just like the website, we pull this information from the last season of the series.
77     *
78     * @param $tvShow
79     * @param $season
80     * @param $parameters
81     * @param $headers
82     * @return null|\Tmdb\Model\AbstractModel
83     */
84    public function getCredits($tvShow, $season, array $parameters = [], array $headers = [])
85    {
86        if ($tvShow instanceof Tv) {
87            $tvShow = $tvShow->getId();
88        }
89
90        if ($season instanceof Season) {
91            $season = $season->getSeasonNumber();
92        }
93
94        $data   = $this->getApi()->getCredits($tvShow, $season, $this->parseQueryParameters($parameters), $headers);
95        $season = $this->getFactory()->create(['credits' => $data]);
96
97        return $season->getCredits();
98    }
99
100    /**
101     * Get the external ids that we have stored for a TV series.
102     *
103     * @param $tvShow
104     * @param $season
105     * @param $parameters
106     * @param $headers
107     * @return null|\Tmdb\Model\AbstractModel
108     */
109    public function getExternalIds($tvShow, $season, array $parameters = [], array $headers = [])
110    {
111        if ($tvShow instanceof Tv) {
112            $tvShow = $tvShow->getId();
113        }
114
115        if ($season instanceof Season) {
116            $season = $season->getSeasonNumber();
117        }
118
119        $data   = $this->getApi()->getExternalIds($tvShow, $season, $this->parseQueryParameters($parameters), $headers);
120        $season = $this->getFactory()->create(['external_ids' => $data]);
121
122        return $season->getExternalIds();
123    }
124
125    /**
126     * Get the images (posters and backdrops) for a TV series.
127     *
128     * @param $tvShow
129     * @param $season
130     * @param $parameters
131     * @param $headers
132     * @return null|\Tmdb\Model\AbstractModel
133     */
134    public function getImages($tvShow, $season, array $parameters = [], array $headers = [])
135    {
136        if ($tvShow instanceof Tv) {
137            $tvShow = $tvShow->getId();
138        }
139
140        if ($season instanceof Season) {
141            $season = $season->getSeasonNumber();
142        }
143
144        $data   = $this->getApi()->getImages($tvShow, $season, $this->parseQueryParameters($parameters), $headers);
145        $season = $this->getFactory()->create(['images' => $data]);
146
147        return $season->getImages();
148    }
149
150    /**
151     * Get the videos that have been added to a TV season (trailers, teasers, etc...)
152     *
153     * @param $tvShow
154     * @param $season
155     * @param $parameters
156     * @param $headers
157     * @return Videos|Video[]
158     */
159    public function getVideos($tvShow, $season, array $parameters = [], array $headers = [])
160    {
161        if ($tvShow instanceof Tv) {
162            $tvShow = $tvShow->getId();
163        }
164
165        if ($season instanceof Season) {
166            $season = $season->getSeasonNumber();
167        }
168
169        $data   = $this->getApi()->getVideos($tvShow, $season, $this->parseQueryParameters($parameters), $headers);
170        $season = $this->getFactory()->create(['videos' => $data]);
171
172        return $season->getVideos();
173    }
174
175    /**
176     * Return the Seasons API Class
177     *
178     * @return \Tmdb\Api\TvSeason
179     */
180    public function getApi()
181    {
182        return $this->getClient()->getTvSeasonApi();
183    }
184
185    /**
186     * @return TvSeasonFactory
187     */
188    public function getFactory()
189    {
190        return new TvSeasonFactory($this->getClient()->getHttpClient());
191    }
192}
193