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\TvEpisodeFactory; 17use Tmdb\Model\Collection\Videos; 18use Tmdb\Model\Common\AccountStates; 19use Tmdb\Model\Common\Video; 20use Tmdb\Model\Lists\Result; 21use Tmdb\Model\Tv\Episode\QueryParameter\AppendToResponse; 22use Tmdb\Model\Tv; 23use Tmdb\Model\Tv\Season; 24use Tmdb\Model\Tv\Episode; 25 26/** 27 * Class TvEpisodeRepository 28 * @package Tmdb\Repository 29 * @see http://docs.themoviedb.apiary.io/#tvepisodes 30 */ 31class TvEpisodeRepository extends AbstractRepository 32{ 33 /** 34 * Load a tv season with the given identifier 35 * 36 * If you want to optimize the result set/bandwidth you should 37 * define the AppendToResponse parameter 38 * 39 * @param $tvShow Tv|integer 40 * @param $season Season|integer 41 * @param $episode Episode|integer 42 * @param $parameters 43 * @param $headers 44 * @throws RuntimeException 45 * @return null|\Tmdb\Model\AbstractModel 46 */ 47 public function load($tvShow, $season, $episode, array $parameters = [], array $headers = []) 48 { 49 if ($tvShow instanceof Tv) { 50 $tvShow = $tvShow->getId(); 51 } 52 53 if ($season instanceof Season) { 54 $season = $season->getSeasonNumber(); 55 } 56 57 if ($episode instanceof Tv\Episode) { 58 $episode = $episode->getEpisodeNumber(); 59 } 60 61 if (is_null($tvShow) || is_null($season) || is_null($episode) ) { 62 throw new RuntimeException('Not all required parameters to load an tv episode are present.'); 63 } 64 65 if (!isset($parameters['append_to_response'])) { 66 $parameters = array_merge($parameters, [ 67 new AppendToResponse([ 68 AppendToResponse::CREDITS, 69 AppendToResponse::EXTERNAL_IDS, 70 AppendToResponse::IMAGES, 71 AppendToResponse::CHANGES, 72 AppendToResponse::VIDEOS 73 ]) 74 ]); 75 } 76 77 $data = $this->getApi()->getEpisode( 78 $tvShow, 79 $season, 80 $episode, 81 $this->parseQueryParameters($parameters), 82 $headers 83 ); 84 85 return $this->getFactory()->create($data); 86 } 87 88 /** 89 * Get the cast & crew information about a TV series. 90 * 91 * Just like the website, we pull this information from the last season of the series. 92 * 93 * @param $tvShow 94 * @param $season 95 * @param $episode 96 * @param $parameters 97 * @param $headers 98 * @return null|\Tmdb\Model\AbstractModel 99 */ 100 public function getCredits($tvShow, $season, $episode, array $parameters = [], array $headers = []) 101 { 102 if ($tvShow instanceof Tv) { 103 $tvShow = $tvShow->getId(); 104 } 105 106 if ($season instanceof Season) { 107 $season = $season->getSeasonNumber(); 108 } 109 110 if ($episode instanceof Tv\Episode) { 111 $episode = $episode->getEpisodeNumber(); 112 } 113 114 $data = $this->getApi()->getCredits( 115 $tvShow, 116 $season, 117 $episode, 118 $this->parseQueryParameters($parameters), 119 $headers 120 ); 121 122 $episode = $this->getFactory()->create(['credits' => $data]); 123 124 return $episode->getCredits(); 125 } 126 127 /** 128 * Get the external ids that we have stored for a TV series. 129 * 130 * @param $tvShow 131 * @param $season 132 * @param $episode 133 * @param $parameters 134 * @param $headers 135 * @return null|\Tmdb\Model\AbstractModel 136 */ 137 public function getExternalIds($tvShow, $season, $episode, array $parameters = [], array $headers = []) 138 { 139 if ($tvShow instanceof Tv) { 140 $tvShow = $tvShow->getId(); 141 } 142 143 if ($season instanceof Season) { 144 $season = $season->getSeasonNumber(); 145 } 146 147 if ($episode instanceof Tv\Episode) { 148 $episode = $episode->getEpisodeNumber(); 149 } 150 151 $data = $this->getApi()->getExternalIds( 152 $tvShow, 153 $season, 154 $episode, 155 $this->parseQueryParameters($parameters), 156 $headers 157 ); 158 159 $episode = $this->getFactory()->create(['external_ids' => $data]); 160 161 return $episode->getExternalIds(); 162 } 163 164 /** 165 * Get the images (posters and backdrops) for a TV series. 166 * 167 * @param $tvShow 168 * @param $season 169 * @param $episode 170 * @param $parameters 171 * @param $headers 172 * @return null|\Tmdb\Model\AbstractModel 173 */ 174 public function getImages($tvShow, $season, $episode, array $parameters = [], array $headers = []) 175 { 176 if ($tvShow instanceof Tv) { 177 $tvShow = $tvShow->getId(); 178 } 179 180 if ($season instanceof Season) { 181 $season = $season->getSeasonNumber(); 182 } 183 184 if ($episode instanceof Tv\Episode) { 185 $episode = $episode->getEpisodeNumber(); 186 } 187 188 $data = $this->getApi()->getImages( 189 $tvShow, 190 $season, 191 $episode, 192 $this->parseQueryParameters($parameters), 193 $headers 194 ); 195 196 $episode = $this->getFactory()->create(['images' => $data]); 197 198 return $episode->getImages(); 199 } 200 201 /** 202 * Get the videos that have been added to a TV episode (teasers, clips, etc...) 203 * 204 * @param $tvShow 205 * @param $season 206 * @param $episode 207 * @param $parameters 208 * @param $headers 209 * @return Videos|Video[] 210 */ 211 public function getVideos($tvShow, $season, $episode, array $parameters = [], array $headers = []) 212 { 213 if ($tvShow instanceof Tv) { 214 $tvShow = $tvShow->getId(); 215 } 216 217 if ($season instanceof Season) { 218 $season = $season->getSeasonNumber(); 219 } 220 221 if ($episode instanceof Tv\Episode) { 222 $episode = $episode->getEpisodeNumber(); 223 } 224 225 $data = $this->getApi()->getVideos( 226 $tvShow, 227 $season, 228 $episode, 229 $this->parseQueryParameters($parameters), 230 $headers 231 ); 232 233 $episode = $this->getFactory()->create(['videos' => $data]); 234 235 return $episode->getVideos(); 236 } 237 238 /** 239 * This method lets users get the status of whether or not the TV show has been rated 240 * or added to their favourite or watch lists. 241 * 242 * A valid session id is required. 243 * 244 * @param mixed $tvShow 245 * @param mixed $season 246 * @param mixed $episode 247 * @return AccountStates 248 */ 249 public function getAccountStates($tvShow, $season, $episode) 250 { 251 if ($tvShow instanceof Tv) { 252 $tvShow = $tvShow->getId(); 253 } 254 255 if ($season instanceof Season) { 256 $season = $season->getSeasonNumber(); 257 } 258 259 if ($episode instanceof Tv\Episode) { 260 $episode = $episode->getEpisodeNumber(); 261 } 262 263 return $this->getFactory()->createAccountStates( 264 $this->getApi()->getAccountStates($tvShow, $season, $episode) 265 ); 266 } 267 268 /** 269 * This method lets users rate a TV show. 270 * 271 * A valid session id or guest session id is required. 272 * 273 * @param mixed $tvShow 274 * @param mixed $season 275 * @param mixed $episode 276 * @param double $rating 277 * @return Result 278 */ 279 public function rate($tvShow, $season, $episode, $rating) 280 { 281 if ($tvShow instanceof Tv) { 282 $tvShow = $tvShow->getId(); 283 } 284 285 if ($season instanceof Season) { 286 $season = $season->getSeasonNumber(); 287 } 288 289 if ($episode instanceof Tv\Episode) { 290 $episode = $episode->getEpisodeNumber(); 291 } 292 293 return $this->getFactory()->createResult( 294 $this->getApi()->rateTvEpisode($tvShow, $season, $episode, $rating) 295 ); 296 } 297 298 /** 299 * Return the Seasons API Class 300 * 301 * @return \Tmdb\Api\TvEpisode 302 */ 303 public function getApi() 304 { 305 return $this->getClient()->getTvEpisodeApi(); 306 } 307 308 /** 309 * @return TvEpisodeFactory 310 */ 311 public function getFactory() 312 { 313 return new TvEpisodeFactory($this->getClient()->getHttpClient()); 314 } 315} 316