1515namespace Tmdb \Model \Query \Discover ;
1616
1717use DateTime ;
18+ use Tmdb \Model \AbstractModel ;
1819use Tmdb \Model \Collection \QueryParametersCollection ;
1920
2021/**
2324 */
2425class DiscoverTvQuery extends QueryParametersCollection
2526{
27+ /** Transform args to an AND query */
28+ public const MODE_AND = 0 ;
29+
30+ /** Transform args to an OR query */
31+ public const MODE_OR = 1 ;
32+
2633 /**
2734 * Minimum value is 1, expected value is an integer.
2835 *
@@ -49,6 +56,49 @@ public function language($language)
4956 return $ this ;
5057 }
5158
59+ /**
60+ * An ISO 3166-1 code. Combine this filter with with_watch_providers in order to filter your results by a specific watch provider in a specific region.
61+ *
62+ * @param string $watchRegion
63+ * @return $this
64+ */
65+ public function watchRegion ($ watchRegion )
66+ {
67+ $ this ->set ('watch_region ' , $ watchRegion );
68+
69+ return $ this ;
70+ }
71+
72+ /**
73+ * Only include movies with the specified watch providers. Combine with watch_region.
74+ *
75+ * @param array|string $watchProviders
76+ * @param int $mode
77+ * @return $this
78+ */
79+ public function withWatchProviders ($ watchProviders , $ mode = self ::MODE_OR )
80+ {
81+ $ this ->set ('with_watch_providers ' , $ this ->with ($ watchProviders , $ mode ));
82+
83+ return $ this ;
84+ }
85+
86+ /**
87+ * Only include movies with the specified monetization types. Combine with watch_region.
88+ *
89+ * Allowed Values: flatrate, free, ads, rent, buy
90+ *
91+ * @param array|string $watchProviders
92+ * @param int $mode
93+ * @return $this
94+ */
95+ public function withWatchMonetizationTypes ($ watchProviders , $ mode = self ::MODE_OR )
96+ {
97+ $ this ->set ('with_watch_monetization_types ' , $ this ->with ($ watchProviders , $ mode ));
98+
99+ return $ this ;
100+ }
101+
52102 /**
53103 * Available options are vote_average.desc, vote_average.asc, first_air_date.desc,
54104 * first_air_date.asc, popularity.desc, popularity.asc
@@ -109,6 +159,44 @@ public function voteAverageGte($average)
109159 return $ this ;
110160 }
111161
162+ /**
163+ * Format the with compatible parameters.
164+ *
165+ * @param array|string $with
166+ * @param int $mode
167+ *
168+ * @return null|string
169+ */
170+ protected function with ($ with = null , $ mode = self ::MODE_OR ): ?string
171+ {
172+ if ($ with instanceof GenericCollection) {
173+ $ with = $ with ->toArray ();
174+ }
175+
176+ if (is_array ($ with )) {
177+ return $ this ->andWith ((array )$ with , $ mode );
178+ }
179+
180+ return $ with ;
181+ }
182+
183+ /**
184+ * Creates an and query to combine an AND or an OR expression.
185+ *
186+ * @param array $with
187+ * @param int $mode
188+ * @return string
189+ */
190+ protected function andWith (array $ with , $ mode )
191+ {
192+ return (
193+ implode (
194+ $ mode === self ::MODE_OR ? '| ' : ', ' ,
195+ array_map ([$ this , 'normalize ' ], $ with )
196+ )
197+ );
198+ }
199+
112200 /**
113201 * Creates an OR query for genres
114202 *
@@ -227,4 +315,19 @@ public function withNetworksAnd(array $networks = [])
227315 implode (', ' , $ networks )
228316 );
229317 }
318+
319+ /**
320+ * Extract object id's if an collection was passed on.
321+ *
322+ * @param $mixed
323+ * @return mixed
324+ */
325+ protected function normalize ($ mixed )
326+ {
327+ if (is_object ($ mixed ) && $ mixed instanceof AbstractModel) {
328+ return $ mixed ->getId ();
329+ }
330+
331+ return $ mixed ;
332+ }
230333}
0 commit comments