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
|
33 |
|
public function __construct(TmdbInterface $tmdb) |
50
|
|
|
{ |
51
|
33 |
|
$this->tmdb = $tmdb; |
52
|
33 |
|
$this->logger = $tmdb->getLogger(); |
53
|
33 |
|
$this->setGeneratorTrait($tmdb); |
54
|
33 |
|
} |
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
|
33 |
|
private function searchItem(string $item, string $query, array $options, string $result_class) : \Generator |
66
|
|
|
{ |
67
|
|
|
try { |
68
|
33 |
|
$this->logger->debug('Starting search item', array('item' => $item, 'query' => $query, 'options' => $options, 'result_class' => $result_class)); |
69
|
33 |
|
$query = trim($query); |
70
|
33 |
|
if (empty($query)) { |
71
|
5 |
|
$this->logger->error('Query param cannot be empty', array('item' => $item, 'query' => $query, 'options' => $options, 'result_class' => $result_class)); |
72
|
5 |
|
throw new IncorrectParamException; |
73
|
|
|
} |
74
|
28 |
|
$options['query'] = $query; |
75
|
28 |
|
$params = $this->checkSearchItemOption($options); |
76
|
|
|
|
77
|
28 |
|
$response = $this->tmdb->getRequest('search/' . $item, $params); |
78
|
|
|
|
79
|
28 |
|
$this->page = (int) $response->page; |
80
|
28 |
|
$this->total_pages = (int) $response->total_pages; |
81
|
28 |
|
$this->total_results = (int) $response->total_results; |
82
|
|
|
|
83
|
28 |
|
return $this->searchItemGenerator($response->results, $result_class); |
84
|
5 |
|
} catch (TmdbException $ex) { |
85
|
5 |
|
throw $ex; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Check search item api option |
91
|
|
|
* @param array $options |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
28 |
|
private function checkSearchItemOption(array $options) : array |
95
|
|
|
{ |
96
|
28 |
|
$params = []; |
97
|
28 |
|
$this->tmdb->checkOptionQuery($options, $params); |
98
|
28 |
|
$this->tmdb->checkOptionPage($options, $params); |
99
|
28 |
|
$this->tmdb->checkOptionLanguage($options, $params); |
100
|
28 |
|
$this->tmdb->checkOptionIncludeAdult($options, $params); |
101
|
28 |
|
$this->tmdb->checkOptionYear($options, $params); |
102
|
|
|
|
103
|
28 |
|
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
|
8 |
View Code Duplication |
public function movie(string $query, array $options = array()) : \Generator |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
try { |
116
|
8 |
|
$this->logger->debug('Starting search movie', array('query' => $query, 'options' => $options)); |
117
|
8 |
|
return $this->searchItem('movie', $query, $options, Results\Movie::class); |
118
|
1 |
|
} catch (TmdbException $ex) { |
119
|
1 |
|
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
|
7 |
View Code Duplication |
public function tvshow(string $query, array $options = array()) : \Generator |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
try { |
133
|
7 |
|
$this->logger->debug('Starting search tv show', array('query' => $query, 'options' => $options)); |
134
|
7 |
|
return $this->searchItem('tv', $query, $options, Results\TVShow::class); |
135
|
1 |
|
} catch (TmdbException $ex) { |
136
|
1 |
|
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
|
4 |
View Code Duplication |
public function collection(string $query, array $options = array()) : \Generator |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
try { |
150
|
4 |
|
$this->logger->debug('Starting search collection', array('query' => $query, 'options' => $options)); |
151
|
4 |
|
return $this->searchItem('collection', $query, $options, Results\Collection::class); |
152
|
1 |
|
} catch (TmdbException $ex) { |
153
|
1 |
|
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
|
8 |
View Code Duplication |
public function people(string $query, array $options = array()) : \Generator |
|
|
|
|
165
|
|
|
{ |
166
|
|
|
try { |
167
|
8 |
|
$this->logger->debug('Starting search people', array('query' => $query, 'options' => $options)); |
168
|
8 |
|
return $this->searchItem('person', $query, $options, Results\People::class); |
169
|
1 |
|
} catch (TmdbException $ex) { |
170
|
1 |
|
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
|
6 |
View Code Duplication |
public function company(string $query, array $options = array()) : \Generator |
|
|
|
|
182
|
|
|
{ |
183
|
|
|
try { |
184
|
6 |
|
$this->logger->debug('Starting search company', array('query' => $query, 'options' => $options)); |
185
|
6 |
|
return $this->searchItem('company', $query, $options, Results\Company::class); |
186
|
1 |
|
} catch (TmdbException $ex) { |
187
|
1 |
|
throw $ex; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.