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\Factory\ImageFactory;
16use Tmdb\Factory\Movie\AlternativeTitleFactory;
17use Tmdb\Factory\MovieFactory;
18use Tmdb\Factory\PeopleFactory;
19use Tmdb\Model\Collection\Videos;
20use Tmdb\Model\Common\GenericCollection;
21use Tmdb\Model\Common\Video;
22use Tmdb\Model\Movie;
23use Tmdb\Model\Movie\QueryParameter\AppendToResponse;
24
25/**
26 * Class MovieRepository
27 * @package Tmdb\Repository
28 * @see http://docs.themoviedb.apiary.io/#movies
29 */
30class MovieRepository extends AbstractRepository
31{
32    /**
33     * @var ImageFactory
34     */
35    private $imageFactory;
36
37    /**
38     * @var AlternativeTitleFactory
39     */
40    private $alternativeTitleFactory;
41
42    /**
43     * @var PeopleFactory
44     */
45    private $peopleFactory;
46
47    /**
48     * Load a movie with the given identifier
49     *
50     * If you want to optimize the result set/bandwidth you
51     * should define the AppendToResponse parameter
52     *
53     * @param $id
54     * @param $parameters
55     * @param $headers
56     * @return null|\Tmdb\Model\AbstractModel
57     */
58    public function load($id, array $parameters = [], array $headers = [])
59    {
60        if (!isset($parameters['append_to_response'])) {
61            $parameters = array_merge($parameters, [
62                new AppendToResponse([
63                    AppendToResponse::ALTERNATIVE_TITLES,
64                    AppendToResponse::CHANGES,
65                    AppendToResponse::CREDITS,
66                    AppendToResponse::IMAGES,
67                    AppendToResponse::KEYWORDS,
68                    AppendToResponse::LISTS,
69                    AppendToResponse::RELEASE_DATES,
70                    AppendToResponse::REVIEWS,
71                    AppendToResponse::SIMILAR,
72                    AppendToResponse::RECOMMENDATIONS,
73                    AppendToResponse::TRANSLATIONS,
74                    AppendToResponse::VIDEOS,
75                ])
76            ]);
77        }
78
79        $data = $this->getApi()->getMovie($id, $this->parseQueryParameters($parameters), $headers);
80
81        return $this->getFactory()->create($data);
82    }
83
84    /**
85     * Get the alternative titles for a specific movie id.
86     *
87     * @param $id
88     * @param $parameters
89     * @param $headers
90     * @return GenericCollection
91     */
92    public function getAlternativeTitles($id, array $parameters = [], array $headers = [])
93    {
94        $data  = $this->getApi()->getAlternativeTitles($id, $this->parseQueryParameters($parameters), $headers);
95        $movie = $this->getFactory()->create(['alternative_titles' => $data]);
96
97        return $movie->getAlternativeTitles();
98    }
99
100    /**
101     * Get the cast and crew information for a specific movie id.
102     *
103     * @param $id
104     * @param $parameters
105     * @param $headers
106     * @return null|\Tmdb\Model\AbstractModel
107     */
108    public function getCredits($id, array $parameters = [], array $headers = [])
109    {
110        $data  = $this->getApi()->getCredits($id, $this->parseQueryParameters($parameters), $headers);
111        $movie = $this->getFactory()->create(['credits' => $data]);
112
113        return $movie->getCredits();
114    }
115
116    /**
117     * Get the images (posters and backdrops) for a specific movie id.
118     *
119     * @param $id
120     * @param $parameters
121     * @param $headers
122     * @return null|\Tmdb\Model\AbstractModel
123     */
124    public function getImages($id, array $parameters = [], array $headers = [])
125    {
126        $data  = $this->getApi()->getImages($id, $this->parseQueryParameters($parameters), $headers);
127        $movie = $this->getFactory()->create(['images' => $data]);
128
129        return $movie->getImages();
130    }
131
132    /**
133     * Get the plot keywords for a specific movie id.
134     *
135     * @param $id
136     * @param $parameters
137     * @param $headers
138     * @return null|\Tmdb\Model\AbstractModel
139     */
140    public function getKeywords($id, array $parameters = [], array $headers = [])
141    {
142        $data  = $this->getApi()->getKeywords($id, $this->parseQueryParameters($parameters), $headers);
143        $movie = $this->getFactory()->create(['keywords' => $data]);
144
145        return $movie->getKeywords();
146    }
147
148    /**
149     * Get the release date and certification information by country for a specific movie id.
150     *
151     * @param $id
152     * @param $parameters
153     * @param $headers
154     * @return null|\Tmdb\Model\AbstractModel
155     */
156    public function getReleases($id, array $parameters = [], array $headers = [])
157    {
158        $data  = $this->getApi()->getReleases($id, $this->parseQueryParameters($parameters), $headers);
159        $movie = $this->getFactory()->create(['releases' => $data]);
160
161        return $movie->getReleases();
162    }
163
164    /**
165     * Get the translations for a specific movie id.
166     *
167     * @param $id
168     * @param $parameters
169     * @param $headers
170     * @return null|\Tmdb\Model\AbstractModel
171     */
172    public function getTranslations($id, array $parameters = [], array $headers = [])
173    {
174        $data  = $this->getApi()->getTranslations($id, $this->parseQueryParameters($parameters), $headers);
175        $movie = $this->getFactory()->create(['translations' => $data]);
176
177        return $movie->getTranslations();
178    }
179
180    /**
181     * Get the similar movies for a specific movie id.
182     *
183     * @param $id
184     * @param $parameters
185     * @param $headers
186     * @return null|\Tmdb\Model\AbstractModel
187     *
188     * @deprecated Will be removed in one of the upcoming versions, has been updated to getSimilar ( following TMDB ).
189     */
190    public function getSimilarMovies($id, array $parameters = [], array $headers = [])
191    {
192        return $this->getSimilar($id, $parameters, $headers);
193    }
194
195    /**
196     * Get the similar movies for a specific movie id.
197     *
198     * @param $id
199     * @param $parameters
200     * @param $headers
201     * @return null|\Tmdb\Model\AbstractModel
202     */
203    public function getSimilar($id, array $parameters = [], array $headers = [])
204    {
205        $data  = $this->getApi()->getSimilar($id, $this->parseQueryParameters($parameters), $headers);
206        $movie = $this->getFactory()->create(['similar' => $data]);
207
208        return $movie->getSimilar();
209    }
210
211    /**
212     * Get the recommended movies for a specific movie id.
213     *
214     * @param $id
215     * @param $parameters
216     * @param $headers
217     * @return null|\Tmdb\Model\AbstractModel
218     */
219    public function getRecommendations($id, array $parameters = [], array $headers = [])
220    {
221        $data  = $this->getApi()->getRecommendations($id, $this->parseQueryParameters($parameters), $headers);
222        $movie = $this->getFactory()->create(['recommendations' => $data]);
223
224        return $movie->getRecommendations();
225    }
226
227    /**
228     * Get the reviews for a particular movie id.
229     *
230     * @param $id
231     * @param $parameters
232     * @param $headers
233     * @return null|\Tmdb\Model\AbstractModel
234     */
235    public function getReviews($id, array $parameters = [], array $headers = [])
236    {
237        $data  = $this->getApi()->getReviews($id, $this->parseQueryParameters($parameters), $headers);
238        $movie = $this->getFactory()->create(['reviews' => $data]);
239
240        return $movie->getReviews();
241    }
242
243    /**
244     * Get the lists that the movie belongs to.
245     *
246     * @param $id
247     * @param $parameters
248     * @param $headers
249     * @return null|\Tmdb\Model\AbstractModel
250     */
251    public function getLists($id, array $parameters = [], array $headers = [])
252    {
253        $data  = $this->getApi()->getLists($id, $this->parseQueryParameters($parameters), $headers);
254        $movie = $this->getFactory()->create(['lists' => $data]);
255
256        return $movie->getLists();
257    }
258
259    /**
260     * Get the changes for a specific movie id.
261     * Changes are grouped by key, and ordered by date in descending order.
262     *
263     * By default, only the last 24 hours of changes are returned.
264     * The maximum number of days that can be returned in a single request is 14.
265     *
266     * The language is present on fields that are translatable.
267     *
268     * @param $id
269     * @param $parameters
270     * @param $headers
271     * @return null|\Tmdb\Model\AbstractModel
272     */
273    public function getChanges($id, array $parameters = [], array $headers = [])
274    {
275        $data  = $this->getApi()->getChanges($id, $this->parseQueryParameters($parameters), $headers);
276        $movie = $this->getFactory()->create(['changes' => $data]);
277
278        return $movie->getChanges();
279    }
280
281    /**
282     * Get the latest movie.
283     *
284     * @param  array                          $options
285     * @return null|\Tmdb\Model\AbstractModel
286     */
287    public function getLatest(array $options = [])
288    {
289        return $this->getFactory()->create(
290            $this->getApi()->getLatest($options)
291        );
292    }
293
294    /**
295     * Get the list of upcoming movies. This list refreshes every day.
296     * The maximum number of items this list will include is 100.
297     *
298     * @param  array   $options
299     * @return Movie[]
300     */
301    public function getUpcoming(array $options = [])
302    {
303        return $this->getFactory()->createResultCollection(
304            $this->getApi()->getUpcoming($options)
305        );
306    }
307
308    /**
309     * Get the list of movies playing in theatres. This list refreshes every day.
310     * The maximum number of items this list will include is 100.
311     *
312     * @param  array   $options
313     * @return Movie[]
314     */
315    public function getNowPlaying(array $options = [])
316    {
317        return $this->getFactory()->createResultCollection(
318            $this->getApi()->getNowPlaying($options)
319        );
320    }
321
322    /**
323     * Get the list of popular movies on The Movie Database.
324     * This list refreshes every day.
325     *
326     * @param  array   $options
327     * @return Movie[]
328     */
329    public function getPopular(array $options = [])
330    {
331        return $this->getFactory()->createResultCollection(
332            $this->getApi()->getPopular($options)
333        );
334    }
335
336    /**
337     * Get the list of top rated movies.
338     *
339     * By default, this list will only include movies that have 10 or more votes.
340     * This list refreshes every day.
341     *
342     * @param  array   $options
343     * @return Movie[]
344     */
345    public function getTopRated(array $options = [])
346    {
347        return $this->getFactory()->createResultCollection(
348            $this->getApi()->getTopRated($options)
349        );
350    }
351
352    /**
353     * This method lets users get the status of whether or not the movie has been rated
354     * or added to their favourite or watch lists. A valid session id is required.
355     *
356     * @param  integer $id
357     * @return Movie[]
358     */
359    public function getAccountStates($id)
360    {
361        return $this->getFactory()->createAccountStates(
362            $this->getApi()->getAccountStates($id)
363        );
364    }
365
366    /**
367     * This method lets users rate a movie. A valid session id or guest session id is required.
368     *
369     * @param  integer $id
370     * @param  float   $rating
371     * @return Movie[]
372     */
373    public function rate($id, $rating)
374    {
375        return $this->getFactory()->createResult(
376            $this->getApi()->rateMovie($id, $rating)
377        );
378    }
379
380    /**
381     * Get the videos (trailers, teasers, clips, etc...) for a specific movie id.
382     *
383     * @param $id
384     * @param $parameters
385     * @param $headers
386     * @return Videos|Video[]
387     */
388    public function getVideos($id, array $parameters = [], array $headers = [])
389    {
390        $data  = $this->getApi()->getVideos($id, $this->parseQueryParameters($parameters), $headers);
391        $movie = $this->getFactory()->create(['videos' => $data]);
392
393        return $movie->getVideos();
394    }
395
396    /**
397     * Return the Movies API Class
398     *
399     * @return \Tmdb\Api\Movies
400     */
401    public function getApi()
402    {
403        return $this->getClient()->getMoviesApi();
404    }
405
406    /**
407     * Return the Movie Factory
408     *
409     * @return MovieFactory
410     */
411    public function getFactory()
412    {
413        return new MovieFactory($this->getClient()->getHttpClient());
414    }
415
416    /**
417     * @param  AlternativeTitleFactory $alternativeTitleFactory
418     * @return $this
419     */
420    public function setAlternativeTitleFactory($alternativeTitleFactory)
421    {
422        $this->alternativeTitleFactory = $alternativeTitleFactory;
423
424        return $this;
425    }
426
427    /**
428     * @return AlternativeTitleFactory
429     */
430    public function getAlternativeTitleFactory()
431    {
432        return $this->alternativeTitleFactory;
433    }
434
435    /**
436     * @param  ImageFactory $imageFactory
437     * @return $this
438     */
439    public function setImageFactory($imageFactory)
440    {
441        $this->imageFactory = $imageFactory;
442
443        return $this;
444    }
445
446    /**
447     * @return ImageFactory
448     */
449    public function getImageFactory()
450    {
451        return $this->imageFactory;
452    }
453
454    /**
455     * @param  PeopleFactory $peopleFactory
456     * @return $this
457     */
458    public function setPeopleFactory($peopleFactory)
459    {
460        $this->peopleFactory = $peopleFactory;
461
462        return $this;
463    }
464
465    /**
466     * @return PeopleFactory
467     */
468    public function getPeopleFactory()
469    {
470        return $this->peopleFactory;
471    }
472}
473