1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Tmdb package. |
4
|
|
|
* |
5
|
|
|
* (c) Vincent Faliès <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @author Vincent Faliès <[email protected]> |
11
|
|
|
* @copyright Copyright (c) 2017 |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
namespace VfacTmdb; |
16
|
|
|
|
17
|
|
|
use VfacTmdb\Exceptions\IncorrectParamException; |
18
|
|
|
use VfacTmdb\Exceptions\TmdbException; |
19
|
|
|
use VfacTmdb\Interfaces\TmdbInterface; |
20
|
|
|
use VfacTmdb\Traits\ListItems; |
21
|
|
|
use VfacTmdb\Traits\GeneratorTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Search class |
25
|
|
|
* @package Tmdb |
26
|
|
|
* @author Vincent Faliès <[email protected]> |
27
|
|
|
* @copyright Copyright (c) 2017 |
28
|
|
|
*/ |
29
|
|
|
class Search |
30
|
|
|
{ |
31
|
|
|
use ListItems; |
32
|
|
|
use GeneratorTrait; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Tmdb object |
36
|
|
|
* @var TmdbInterface |
37
|
|
|
*/ |
38
|
|
|
private $tmdb = null; |
39
|
|
|
/** |
40
|
|
|
* Logger |
41
|
|
|
* @var \Psr\Log\LoggerInterface |
42
|
|
|
*/ |
43
|
|
|
private $logger = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor |
47
|
|
|
* @param TmdbInterface $tmdb |
48
|
|
|
*/ |
49
|
99 |
|
public function __construct(TmdbInterface $tmdb) |
50
|
|
|
{ |
51
|
99 |
|
$this->tmdb = $tmdb; |
52
|
99 |
|
$this->logger = $tmdb->getLogger(); |
53
|
99 |
|
$this->setGeneratorTrait($tmdb); |
54
|
99 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Search specify item |
58
|
|
|
* @param string $item item to search : movie / tv / collection |
59
|
|
|
* @param string $query Query string to search like a $item |
60
|
|
|
* @param array $options Array of options for the request |
61
|
|
|
* @param string $result_class class name of the wanted result |
62
|
|
|
* @return \Generator |
63
|
|
|
* @throws TmdbException |
64
|
|
|
*/ |
65
|
99 |
|
private function searchItem(string $item, string $query, array $options, string $result_class) : \Generator |
66
|
|
|
{ |
67
|
|
|
try { |
68
|
99 |
|
$this->logger->debug('Starting search item', array('item' => $item, 'query' => $query, 'options' => $options, 'result_class' => $result_class)); |
69
|
99 |
|
$query = trim($query); |
70
|
99 |
|
if (empty($query)) { |
71
|
15 |
|
$this->logger->error('Query param cannot be empty', array('item' => $item, 'query' => $query, 'options' => $options, 'result_class' => $result_class)); |
72
|
15 |
|
throw new IncorrectParamException; |
73
|
|
|
} |
74
|
84 |
|
$options['query'] = $query; |
75
|
84 |
|
$params = $this->checkSearchItemOption($options); |
76
|
|
|
|
77
|
84 |
|
$response = $this->tmdb->getRequest('search/' . $item, $params); |
78
|
|
|
|
79
|
84 |
|
$this->page = (int) $response->page; |
80
|
84 |
|
$this->total_pages = (int) $response->total_pages; |
81
|
84 |
|
$this->total_results = (int) $response->total_results; |
82
|
|
|
|
83
|
84 |
|
return $this->searchItemGenerator($response->results, $result_class); |
84
|
15 |
|
} catch (TmdbException $ex) { |
85
|
15 |
|
throw $ex; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Check search item api option |
91
|
|
|
* @param array $options |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
84 |
|
private function checkSearchItemOption(array $options) : array |
95
|
|
|
{ |
96
|
84 |
|
$params = []; |
97
|
84 |
|
$this->tmdb->checkOptionQuery($options, $params); |
98
|
84 |
|
$this->tmdb->checkOptionPage($options, $params); |
99
|
84 |
|
$this->tmdb->checkOptionLanguage($options, $params); |
100
|
84 |
|
$this->tmdb->checkOptionIncludeAdult($options, $params); |
101
|
84 |
|
$this->tmdb->checkOptionYear($options, $params); |
102
|
|
|
|
103
|
84 |
|
return $params; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Search a movie |
108
|
|
|
* @param string $query Query string to search like a movie |
109
|
|
|
* @param array $options Array of options for the search |
110
|
|
|
* @return \Generator|Results\Movie |
111
|
|
|
* @throws TmdbException |
112
|
|
|
*/ |
113
|
24 |
|
public function movie(string $query, array $options = array()) : \Generator |
114
|
|
|
{ |
115
|
|
|
try { |
116
|
24 |
|
$this->logger->debug('Starting search movie', array('query' => $query, 'options' => $options)); |
117
|
24 |
|
return $this->searchItem('movie', $query, $options, Results\Movie::class); |
118
|
3 |
|
} catch (TmdbException $ex) { |
119
|
3 |
|
throw $ex; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Search a TV Show |
125
|
|
|
* @param string $query Query string to search like a TV Show |
126
|
|
|
* @param array $options Array of options for the request |
127
|
|
|
* @return \Generator|Results\TVShow |
128
|
|
|
* @throws TmdbException |
129
|
|
|
*/ |
130
|
21 |
|
public function tvshow(string $query, array $options = array()) : \Generator |
131
|
|
|
{ |
132
|
|
|
try { |
133
|
21 |
|
$this->logger->debug('Starting search tv show', array('query' => $query, 'options' => $options)); |
134
|
21 |
|
return $this->searchItem('tv', $query, $options, Results\TVShow::class); |
135
|
3 |
|
} catch (TmdbException $ex) { |
136
|
3 |
|
throw $ex; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Search a collection |
142
|
|
|
* @param string $query Query string to search like a collection |
143
|
|
|
* @param array $options Array of option for the request |
144
|
|
|
* @return \Generator|Results\Collection |
145
|
|
|
* @throws TmdbException |
146
|
|
|
*/ |
147
|
12 |
|
public function collection(string $query, array $options = array()) : \Generator |
148
|
|
|
{ |
149
|
|
|
try { |
150
|
12 |
|
$this->logger->debug('Starting search collection', array('query' => $query, 'options' => $options)); |
151
|
12 |
|
return $this->searchItem('collection', $query, $options, Results\Collection::class); |
152
|
3 |
|
} catch (TmdbException $ex) { |
153
|
3 |
|
throw $ex; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Search a people |
159
|
|
|
* @param string $query Query string to search like a people |
160
|
|
|
* @param array $options Array of option for the request |
161
|
|
|
* @return \Generator|Results\People |
162
|
|
|
* @throws TmdbException |
163
|
|
|
*/ |
164
|
24 |
|
public function people(string $query, array $options = array()) : \Generator |
165
|
|
|
{ |
166
|
|
|
try { |
167
|
24 |
|
$this->logger->debug('Starting search people', array('query' => $query, 'options' => $options)); |
168
|
24 |
|
return $this->searchItem('person', $query, $options, Results\People::class); |
169
|
3 |
|
} catch (TmdbException $ex) { |
170
|
3 |
|
throw $ex; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Search a company |
176
|
|
|
* @param string $query Query string to search like a company |
177
|
|
|
* @param array $options Array of option for the request |
178
|
|
|
* @return \Generator|Results\Company |
179
|
|
|
* @throws TmdbException |
180
|
|
|
*/ |
181
|
18 |
|
public function company(string $query, array $options = array()) : \Generator |
182
|
|
|
{ |
183
|
|
|
try { |
184
|
18 |
|
$this->logger->debug('Starting search company', array('query' => $query, 'options' => $options)); |
185
|
18 |
|
return $this->searchItem('company', $query, $options, Results\Company::class); |
186
|
3 |
|
} catch (TmdbException $ex) { |
187
|
3 |
|
throw $ex; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|