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\Model\Query\Discover;
14
15use Tmdb\Model\Collection\QueryParametersCollection;
16
17/**
18 * Class DiscoverTvQuery
19 * @package Tmdb\Model\Query\Discover
20 */
21class DiscoverTvQuery extends QueryParametersCollection
22{
23    /**
24     * Minimum value is 1, expected value is an integer.
25     *
26     * @param  integer $page
27     * @return $this
28     */
29    public function page($page = 1)
30    {
31        $this->set('page', (int) $page);
32
33        return $this;
34    }
35
36    /**
37     * ISO 639-1 code.
38     *
39     * @param  string $language
40     * @return $this
41     */
42    public function language($language)
43    {
44        $this->set('language', $language);
45
46        return $this;
47    }
48
49    /**
50     * Available options are vote_average.desc, vote_average.asc, first_air_date.desc,
51     * first_air_date.asc, popularity.desc, popularity.asc
52     *
53     * @param  string $option
54     * @return $this
55     */
56    public function sortBy($option)
57    {
58        $this->set('sort_by', $option);
59
60        return $this;
61    }
62
63    /**
64     * Filter the results release dates to matches that include this value.
65     * Expected value is a year.
66     *
67     * @param  \DateTime|integer $year
68     * @return $this
69     */
70    public function firstAirDateYear($year)
71    {
72        if ($year instanceof \DateTime) {
73            $year = $year->format('Y');
74        }
75
76        $this->set('first_air_date_year', (int) $year);
77
78        return $this;
79    }
80
81    /**
82     * Only include TV shows that are equal to, or have a vote count higher than this value.
83     * Expected value is an integer.
84     *
85     * @param  integer $count
86     * @return $this
87     */
88    public function voteCountGte($count)
89    {
90        $this->set('vote_count.gte', (int) $count);
91
92        return $this;
93    }
94
95    /**
96     * Only include TV shows that are equal to, or have a higher average rating than this value.
97     * Expected value is a float.
98     *
99     * @param  float $average
100     * @return $this
101     */
102    public function voteAverageGte($average)
103    {
104        $this->set('vote_average.gte', (float) $average);
105
106        return $this;
107    }
108
109    /**
110     * Only include TV shows with the specified genres.
111     * Expected value is an integer (the id of a genre).
112     *
113     * Multiple values can be specified.
114     *
115     * Comma separated indicates an 'AND' query,
116     * while a pipe (|) separated value indicates an 'OR'.
117     *
118     * @param  array|string $genres
119     * @return $this
120     */
121    public function withGenres($genres)
122    {
123        if (is_array($genres)) {
124            $genres = $this->withGenresAnd($genres);
125        }
126
127        $this->set('with_genres', $genres);
128
129        return $this;
130    }
131
132    /**
133     * Creates an OR query for genres
134     *
135     * @param  array $genres
136     * @return $this
137     */
138    public function withGenresOr(array $genres = [])
139    {
140        return $this->withGenres(
141            implode('|', $genres)
142        );
143    }
144
145    /**
146     * Creates an AND query for genres
147     *
148     * @param  array $genres
149     * @return $this
150     */
151    public function withGenresAnd(array $genres = [])
152    {
153        return $this->withGenres(
154            implode(',', $genres)
155        );
156    }
157
158    /**
159     * The minimum release to include. Expected format is YYYY-MM-DD.
160     *
161     * @param  \DateTime|string $date
162     * @return $this
163     */
164    public function firstAirDateGte($date)
165    {
166        if ($date instanceof \DateTime) {
167            $date = $date->format('Y-m-d');
168        }
169
170        $this->set('first_air_date.gte', $date);
171
172        return $this;
173    }
174
175    /**
176     * The maximum release to include. Expected format is YYYY-MM-DD.
177     *
178     * @param  \DateTime|string $date
179     * @return $this
180     */
181    public function firstAirDateLte($date)
182    {
183        if ($date instanceof \DateTime) {
184            $date = $date->format('Y-m-d');
185        }
186
187        $this->set('first_air_date.lte', $date);
188
189        return $this;
190    }
191
192    /**
193     * Filter TV shows to include a specific network.
194     *
195     * Expected value is an integer (the id of a network).
196     * They can be comma separated to indicate an 'AND' query.
197     *
198     * Expected value is an integer (the id of a company).
199     * They can be comma separated to indicate an 'AND' query.
200     *
201     * @param  array|string $networks
202     * @return $this
203     */
204    public function withNetworks($networks)
205    {
206        if (is_array($networks)) {
207            $networks = $this->withNetworksAnd($networks);
208        }
209
210        $this->set('with_networks', $networks);
211
212        return $this;
213    }
214
215    /**
216     * Creates an and query for networks
217     *
218     * @param  array $networks
219     * @return $this
220     */
221    public function withNetworksAnd(array $networks = [])
222    {
223        return $this->withNetworks(
224            implode(',', $networks)
225        );
226    }
227}
228