1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace vfalies\tmdb; |
4
|
|
|
|
5
|
|
|
class Search |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
private $tmdb = null; |
9
|
|
|
private $page = 1; // Page number of the search result |
10
|
|
|
private $total_pages = 1; // Total pages of the search result |
11
|
|
|
private $total_results = 0; // Total results of the search result |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Constructor |
15
|
|
|
* @param \vfalies\tmdb\Tmdb $tmdb |
16
|
|
|
*/ |
17
|
|
|
|
18
|
46 |
|
public function __construct(Tmdb $tmdb) |
19
|
|
|
{ |
20
|
46 |
|
$this->tmdb = $tmdb; |
21
|
46 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Search specify item |
25
|
|
|
* @param string $item item to search : movie / tv / collection |
26
|
|
|
* @param string $query Query string to search like a $item |
27
|
|
|
* @param array $options Array of options for the request |
28
|
|
|
* @param string $result_class class name of the wanted result |
29
|
|
|
* @return \Generator|$result_class |
|
|
|
|
30
|
|
|
* @throws \Exception |
31
|
|
|
*/ |
32
|
43 |
|
private function searchItem(string $item, string $query, array $options, $result_class): \Generator |
33
|
|
|
{ |
34
|
|
|
try |
35
|
|
|
{ |
36
|
43 |
|
$query = trim($query); |
37
|
43 |
|
if (empty($query)) |
38
|
|
|
{ |
39
|
3 |
|
throw new \Exception('query parameter can not be empty'); |
40
|
|
|
} |
41
|
40 |
|
$params = $this->tmdb->checkOptions($options); |
42
|
37 |
|
$response = $this->tmdb->sendRequest(new CurlRequest(), 'search/'.$item, $query, $params); |
43
|
|
|
|
44
|
37 |
|
$this->page = (int) $response->page; |
45
|
37 |
|
$this->total_pages = (int) $response->total_pages; |
46
|
37 |
|
$this->total_results = (int) $response->total_results; |
47
|
|
|
|
48
|
37 |
|
return $this->searchItemGenerator($response->results, $result_class); |
49
|
|
|
} |
50
|
6 |
|
catch (\Exception $ex) |
51
|
|
|
{ |
52
|
6 |
|
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Search Item generator method |
58
|
|
|
* @param array $results |
59
|
|
|
* @param string $class |
60
|
|
|
*/ |
61
|
37 |
|
private function searchItemGenerator(array $results, string $class): \Generator |
62
|
|
|
{ |
63
|
37 |
|
foreach ($results as $result) |
64
|
|
|
{ |
65
|
36 |
|
$element = new $class($this->tmdb, $result); |
66
|
|
|
|
67
|
36 |
|
yield $element; |
68
|
|
|
} |
69
|
1 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Search a movie |
73
|
|
|
* @param string $query Query string to search like a movie |
74
|
|
|
* @param array $options Array of options for the search |
75
|
|
|
* @return \Generator|Results\Movie |
76
|
|
|
* @throws \Exception |
77
|
|
|
*/ |
78
|
15 |
View Code Duplication |
public function searchMovie(string $query, array $options = array()): \Generator |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
try |
81
|
|
|
{ |
82
|
15 |
|
return $this->searchItem('movie', $query, $options, __NAMESPACE__."\\Results\\".'Movie'); |
83
|
|
|
} |
84
|
2 |
|
catch (\Exception $ex) |
85
|
|
|
{ |
86
|
2 |
|
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Search a TV Show |
92
|
|
|
* @param string $query Query string to search like a TV Show |
93
|
|
|
* @param array $options Array of options for the request |
94
|
|
|
* @return \Generator|Results\TVShow |
95
|
|
|
* @throws \Exception |
96
|
|
|
*/ |
97
|
14 |
|
public function searchTVShow(string $query, array $options = array()): \Generator |
98
|
|
|
{ |
99
|
|
|
try |
100
|
|
|
{ |
101
|
14 |
|
return $this->searchItem('tv', $query, $options, __NAMESPACE__."\\Results\\".'TVShow'); |
102
|
|
|
} |
103
|
2 |
|
catch (\Exception $ex) |
104
|
|
|
{ |
105
|
2 |
|
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Search a collection |
111
|
|
|
* @param string $query Query string to search like a collection |
112
|
|
|
* @param array $options Array of option for the request |
113
|
|
|
* @return \Generator|Results\Collection |
114
|
|
|
* @throws \Exception |
115
|
|
|
*/ |
116
|
14 |
View Code Duplication |
public function searchCollection(string $query, array $options = array()): \Generator |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
try |
119
|
|
|
{ |
120
|
14 |
|
return $this->searchItem('collection', $query, $options, __NAMESPACE__."\\Results\\".'Collection'); |
121
|
|
|
} |
122
|
2 |
|
catch (\Exception $ex) |
123
|
|
|
{ |
124
|
2 |
|
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get movie details |
130
|
|
|
* @param int $movie_id |
131
|
|
|
* @param array $options |
132
|
|
|
* @return \vfalies\tmdb\Movie |
133
|
|
|
*/ |
134
|
1 |
|
public function getMovie(int $movie_id, array $options = array()): Movie |
135
|
|
|
{ |
136
|
1 |
|
$movie = new Movie($this->tmdb, $movie_id, $options); |
137
|
|
|
|
138
|
1 |
|
return $movie; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get collection details |
143
|
|
|
* @param int $collection_id |
144
|
|
|
* @param array $options |
145
|
|
|
* @return \vfalies\tmdb\Collection |
146
|
|
|
*/ |
147
|
1 |
|
public function getCollection(int $collection_id, array $options = array()): Collection |
148
|
|
|
{ |
149
|
1 |
|
$collection = new Collection($this->tmdb, $collection_id, $options); |
150
|
|
|
|
151
|
1 |
|
return $collection; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get TV Show details |
156
|
|
|
* @param int $tv_id |
157
|
|
|
* @param array $options |
158
|
|
|
* @return \vfalies\tmdb\TVShow |
159
|
|
|
*/ |
160
|
1 |
|
public function getTVShow(int $tv_id, array $options = array()): TVShow |
161
|
|
|
{ |
162
|
1 |
|
$tv = new TVShow($this->tmdb, $tv_id, $options); |
163
|
|
|
|
164
|
1 |
|
return $tv; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get page from result search |
169
|
|
|
* @return int |
170
|
|
|
*/ |
171
|
1 |
|
public function getPage(): int |
172
|
|
|
{ |
173
|
1 |
|
return $this->page; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get total page from result search |
178
|
|
|
* @return int |
179
|
|
|
*/ |
180
|
1 |
|
public function getTotalPages(): int |
181
|
|
|
{ |
182
|
1 |
|
return $this->total_pages; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Get total results from search |
187
|
|
|
* @return int |
188
|
|
|
*/ |
189
|
1 |
|
public function getTotalResults(): int |
190
|
|
|
{ |
191
|
1 |
|
return $this->total_results; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.