|
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\Abstracts; |
|
16
|
|
|
|
|
17
|
|
|
use VfacTmdb\Exceptions\TmdbException; |
|
18
|
|
|
use VfacTmdb\Interfaces\TmdbInterface; |
|
19
|
|
|
use VfacTmdb\Traits\GeneratorTrait; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* abstract account class |
|
23
|
|
|
* @package Tmdb |
|
24
|
|
|
* @author Vincent Faliès <[email protected]> |
|
25
|
|
|
* @copyright Copyright (c) 2017 |
|
26
|
|
|
*/ |
|
27
|
|
|
abstract class Account |
|
28
|
|
|
{ |
|
29
|
|
|
use GeneratorTrait; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Tmdb object |
|
33
|
|
|
* @var TmdbInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $tmdb = null; |
|
36
|
|
|
/** |
|
37
|
|
|
* Logger |
|
38
|
|
|
* @var \Psr\Log\LoggerInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $logger = null; |
|
41
|
|
|
/** |
|
42
|
|
|
* Session_id string |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $auth = null; |
|
46
|
|
|
/** |
|
47
|
|
|
* Account id |
|
48
|
|
|
* @var int |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $account_id; |
|
51
|
|
|
/** |
|
52
|
|
|
* Configuration array |
|
53
|
|
|
* @var \stdClass |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $conf = null; |
|
56
|
|
|
/** |
|
57
|
|
|
* Options |
|
58
|
|
|
* @var array |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $options = []; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Page number |
|
64
|
|
|
* @var int |
|
65
|
|
|
*/ |
|
66
|
|
|
public $page = 1; |
|
67
|
|
|
/** |
|
68
|
|
|
* Total pages |
|
69
|
|
|
* @var int |
|
70
|
|
|
*/ |
|
71
|
|
|
public $total_pages = 1; |
|
72
|
|
|
/** |
|
73
|
|
|
* Total results |
|
74
|
|
|
* @var int |
|
75
|
|
|
*/ |
|
76
|
|
|
public $total_results = 0; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Constructor |
|
80
|
|
|
* @param TmdbInterface $tmdb |
|
81
|
|
|
* @param string $session_id |
|
82
|
|
|
* @param int $account_id |
|
83
|
|
|
* @param array $options |
|
84
|
|
|
*/ |
|
85
|
84 |
|
public function __construct(TmdbInterface $tmdb, string $session_id, int $account_id, array $options = array()) |
|
86
|
|
|
{ |
|
87
|
84 |
|
$this->tmdb = $tmdb; |
|
88
|
84 |
|
$options['session_id'] = $session_id; |
|
89
|
84 |
|
$this->logger = $tmdb->getLogger(); |
|
90
|
84 |
|
$this->options = $options; |
|
91
|
84 |
|
$this->account_id = $account_id; |
|
92
|
|
|
// Configuration |
|
93
|
84 |
|
$this->conf = $tmdb->getConfiguration(); |
|
94
|
84 |
|
$this->setGeneratorTrait($tmdb); |
|
95
|
84 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Add or remove item in list |
|
99
|
|
|
* @param string $list_type Type of list (possible value : favorite / watchlist) |
|
100
|
|
|
* @param string $media_type type of media (movie / tv) |
|
101
|
|
|
* @param int $media_id media_id |
|
102
|
|
|
* @param bool $add add or remove item in list |
|
103
|
|
|
*/ |
|
104
|
30 |
|
protected function setListItem(string $list_type, string $media_type, int $media_id, bool $add) |
|
105
|
|
|
{ |
|
106
|
|
|
try { |
|
107
|
30 |
|
$params = []; |
|
108
|
30 |
|
$params['media_type'] = $media_type; |
|
109
|
30 |
|
$params['media_id'] = $media_id; |
|
110
|
30 |
|
$params[$list_type] = $add; |
|
111
|
|
|
|
|
112
|
30 |
|
$this->tmdb->postRequest('account/' . $this->account_id . '/' . $list_type, $this->options, $params); |
|
113
|
|
|
|
|
114
|
24 |
|
return $this; |
|
115
|
6 |
|
} catch (TmdbException $e) { |
|
116
|
6 |
|
throw $e; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get account list items |
|
122
|
|
|
* @param string $list_type type of list (possible value : favorite, rated, watchlist) |
|
123
|
|
|
* @param string $item item name, possible value : movies , tv , tv/episodes |
|
124
|
|
|
* @param string $result_class class for the results |
|
125
|
|
|
* @return \Generator |
|
126
|
|
|
*/ |
|
127
|
21 |
|
protected function getAccountListItems(string $list_type, string $item, string $result_class) : \Generator |
|
128
|
|
|
{ |
|
129
|
21 |
|
$options = []; |
|
130
|
21 |
|
$this->tmdb->checkOptionSessionId($this->options, $options); |
|
131
|
21 |
|
$this->tmdb->checkOptionLanguage($this->options, $options); |
|
132
|
21 |
|
$this->tmdb->checkOptionSortBy($this->options, $options); |
|
133
|
21 |
|
$this->tmdb->checkOptionPage($this->options, $options); |
|
134
|
|
|
|
|
135
|
21 |
|
$response = $this->tmdb->getRequest('account/' . $this->account_id . '/' . $list_type . '/' . $item, $options); |
|
136
|
|
|
|
|
137
|
21 |
|
$this->page = (int) $response->page; |
|
138
|
21 |
|
$this->total_pages = (int) $response->total_pages; |
|
139
|
21 |
|
$this->total_results = (int) $response->total_results; |
|
140
|
|
|
|
|
141
|
21 |
|
return $this->searchItemGenerator($response->results, $result_class); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Get page from result search |
|
146
|
|
|
* @return int |
|
147
|
|
|
*/ |
|
148
|
3 |
|
public function getPage() : int |
|
149
|
|
|
{ |
|
150
|
3 |
|
return $this->page; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Get total page from result search |
|
155
|
|
|
* @return int |
|
156
|
|
|
*/ |
|
157
|
3 |
|
public function getTotalPages() : int |
|
158
|
|
|
{ |
|
159
|
3 |
|
return $this->total_pages; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Get total results from search |
|
164
|
|
|
* @return int |
|
165
|
|
|
*/ |
|
166
|
3 |
|
public function getTotalResults() : int |
|
167
|
|
|
{ |
|
168
|
3 |
|
return $this->total_results; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|