1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Localdisk\Monar; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Localdisk\Monar\Exceptions\MonarException; |
8
|
|
|
|
9
|
|
|
abstract class AbstractDriver implements Driver |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $url; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Client |
18
|
|
|
*/ |
19
|
|
|
protected $client; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $category; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $board; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $thread; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $baseUrl; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $encoding; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $cookie; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* AbstractDriver constructor. |
53
|
|
|
* |
54
|
|
|
* @param string $url |
55
|
|
|
* @param Client $client |
56
|
|
|
*/ |
57
|
|
|
public function __construct(string $url, Client $client) |
58
|
|
|
{ |
59
|
|
|
$this->url = $url; |
60
|
|
|
$this->client = $client; |
61
|
|
|
$this->parse(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function getCategory(): string |
68
|
|
|
{ |
69
|
|
|
return $this->category; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function getBoard(): string |
76
|
|
|
{ |
77
|
|
|
return $this->board; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getThread(): string |
84
|
|
|
{ |
85
|
|
|
return $this->thread; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* renew array. |
90
|
|
|
* |
91
|
|
|
* @param array $array |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
protected function renewArray(array $array): array |
96
|
|
|
{ |
97
|
|
|
return array_values(array_filter($array)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* send request. |
102
|
|
|
* |
103
|
|
|
* @param string $method |
104
|
|
|
* @param string $url |
105
|
|
|
* @param array $options |
106
|
|
|
* |
107
|
|
|
* @return mixed|string |
108
|
|
|
* @throws MonarException |
109
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
110
|
|
|
*/ |
111
|
|
|
protected function request(string $method, string $url, array $options = []) |
112
|
|
|
{ |
113
|
|
|
$response = $this->client->request($method, $url, $options); |
114
|
|
|
|
115
|
|
|
$error = $response->getHeader('ERROR'); |
116
|
|
|
|
117
|
|
|
if (! empty($error)) { |
118
|
|
|
$errorStr = \is_array($error) ? implode(',', $error) : $error; |
119
|
|
|
throw new MonarException("url: {$url}. method: {$method}. error:{$errorStr}"); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if ($cookie = $response->getHeader('Set-Cookie')) { |
123
|
|
|
$this->cookie = $cookie[0]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $this->encode($response->getBody(), $this->encoding, 'UTF-8'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* encode body text. |
131
|
|
|
* |
132
|
|
|
* @param string $body |
133
|
|
|
* @param string $from |
134
|
|
|
* @param string $to |
135
|
|
|
* |
136
|
|
|
* @return mixed|string |
137
|
|
|
*/ |
138
|
|
|
protected function encode(string $body, string $from, string $to) |
139
|
|
|
{ |
140
|
|
|
return mb_convert_encoding($body, $to, $from); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* parse url. |
145
|
|
|
* |
146
|
|
|
* @return void |
147
|
|
|
*/ |
148
|
|
|
abstract protected function parse(): void; |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* parse dat collection. |
152
|
|
|
* |
153
|
|
|
* @param string $body |
154
|
|
|
* @param int|null $start |
155
|
|
|
* @param int|null $end |
156
|
|
|
* |
157
|
|
|
* @return \Illuminate\Support\Collection |
158
|
|
|
*/ |
159
|
|
|
abstract protected function parseDatCollection(string $body, ?int $start = null, ?int $end = null): Collection; |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* parse threads collection. |
163
|
|
|
* |
164
|
|
|
* @param string $body |
165
|
|
|
* |
166
|
|
|
* @return \Illuminate\Support\Collection |
167
|
|
|
*/ |
168
|
|
|
abstract protected function parseThreadsCollection(string $body): Collection; |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* build message url. |
172
|
|
|
* |
173
|
|
|
* @param int $start |
174
|
|
|
* @param int|null $end |
175
|
|
|
* |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
|
|
abstract protected function messagesUrl(int $start = 1, ?int $end = null): string; |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* build thread url. |
182
|
|
|
* |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
abstract protected function threadsUrl(): string; |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* build post url. |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
abstract protected function postUrl(): string; |
193
|
|
|
} |
194
|
|
|
|