1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace OpenSubtitles\Endpoints; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\ClientInterface; |
8
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
9
|
|
|
use GuzzleHttp\RequestOptions; |
10
|
|
|
use OpenSubtitles\HttpClientHandler; |
11
|
|
|
|
12
|
|
|
class Subtitle implements Endpoint |
13
|
|
|
{ |
14
|
|
|
private string $baseUrl; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var HttpClientHandler |
18
|
|
|
*/ |
19
|
|
|
private HttpClientHandler $clientHandler; |
20
|
|
|
|
21
|
|
|
public function __construct(ClientInterface $client, string $baseUrl, string $apiKey = null) |
22
|
|
|
{ |
23
|
|
|
$this->baseUrl = $baseUrl; |
24
|
|
|
|
25
|
|
|
$this->clientHandler = new HttpClientHandler($client); |
26
|
|
|
if ($apiKey) { |
27
|
|
|
$this->clientHandler->setApiKey($apiKey); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Find subtitle by title |
33
|
|
|
* |
34
|
|
|
* @param string $title |
35
|
|
|
* @return mixed |
36
|
|
|
* @throws GuzzleException |
37
|
|
|
*/ |
38
|
|
|
public function findByTitle(string $title) |
39
|
|
|
{ |
40
|
|
|
return $this->findByQuery($title); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Find subtitle by query |
45
|
|
|
* |
46
|
|
|
* @param string $query |
47
|
|
|
* @return mixed |
48
|
|
|
* @throws GuzzleException |
49
|
|
|
*/ |
50
|
|
|
public function findByQuery(string $query) |
51
|
|
|
{ |
52
|
|
|
return $this->find(compact('query')); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Find subtitle for a movie or tv show |
57
|
|
|
* |
58
|
|
|
* @param array $params |
59
|
|
|
* @return mixed |
60
|
|
|
* @throws GuzzleException |
61
|
|
|
*/ |
62
|
|
|
public function find(array $params) |
63
|
|
|
{ |
64
|
|
|
return $this->clientHandler->toJson( |
65
|
|
|
$this->clientHandler->get($this->baseUrl . '/subtitles', [RequestOptions::QUERY => $params]) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Find subtitle by movie hash |
71
|
|
|
* |
72
|
|
|
* @param string $hash |
73
|
|
|
* @return mixed |
74
|
|
|
* @throws GuzzleException |
75
|
|
|
*/ |
76
|
|
|
public function findByMovieHash(string $hash) |
77
|
|
|
{ |
78
|
|
|
return $this->find(['moviehash' => $hash]); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|