15
15
namespace Tmdb \Model \Query \Discover ;
16
16
17
17
use DateTime ;
18
+ use Tmdb \Model \AbstractModel ;
18
19
use Tmdb \Model \Collection \QueryParametersCollection ;
19
20
20
21
/**
23
24
*/
24
25
class DiscoverTvQuery extends QueryParametersCollection
25
26
{
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
+
26
33
/**
27
34
* Minimum value is 1, expected value is an integer.
28
35
*
@@ -49,6 +56,49 @@ public function language($language)
49
56
return $ this ;
50
57
}
51
58
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
+
52
102
/**
53
103
* Available options are vote_average.desc, vote_average.asc, first_air_date.desc,
54
104
* first_air_date.asc, popularity.desc, popularity.asc
@@ -109,6 +159,44 @@ public function voteAverageGte($average)
109
159
return $ this ;
110
160
}
111
161
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
+
112
200
/**
113
201
* Creates an OR query for genres
114
202
*
@@ -227,4 +315,19 @@ public function withNetworksAnd(array $networks = [])
227
315
implode (', ' , $ networks )
228
316
);
229
317
}
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
+ }
230
333
}
0 commit comments