1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace ApiVideo\Client\Api; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use ApiVideo\Client\Model\Player; |
8
|
|
|
use Buzz\Message\Form\FormUpload; |
9
|
|
|
use UnexpectedValueException; |
10
|
|
|
|
11
|
|
|
class Players extends BaseApi |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param string $playerId |
16
|
|
|
* @return Player|null |
17
|
|
|
*/ |
18
|
|
|
public function get($playerId) |
19
|
|
|
{ |
20
|
|
|
$response = $this->browser->get("/players/$playerId"); |
21
|
|
|
if (!$response->isSuccessful()) { |
22
|
|
|
$this->registerLastError($response); |
23
|
|
|
|
24
|
|
|
return null; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return $this->unmarshal($response); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Incrementally iterate over a collection of elements. |
32
|
|
|
* By default the elements are returned in an array, unless you pass a |
33
|
|
|
* $callback which will be called for each instance of Player. |
34
|
|
|
* Available parameters: |
35
|
|
|
* - currentPage (int) current pagination page |
36
|
|
|
* - pageSize (int) number of elements per page |
37
|
|
|
* - playerIds (array) videoIds to limit the search to |
38
|
|
|
* If currentPage and pageSize are not given, the method iterates over all |
39
|
|
|
* pages of results and return an array containing all the results. |
40
|
|
|
* |
41
|
|
|
* @param array $parameters |
42
|
|
|
* @param callable $callback |
43
|
|
|
* @return Player[]|null |
44
|
|
|
*/ |
45
|
|
|
public function search(array $parameters = array(), $callback = null) |
46
|
|
|
{ |
47
|
|
|
$params = $parameters; |
48
|
|
|
$currentPage = isset($parameters['currentPage']) ? $parameters['currentPage'] : 1; |
49
|
|
|
$params['pageSize'] = isset($parameters['pageSize']) ? $parameters['pageSize'] : 100; |
50
|
|
|
$allPlayers = array(); |
51
|
|
|
|
52
|
|
|
do { |
53
|
|
|
$params['currentPage'] = $currentPage; |
54
|
|
|
$response = $this->browser->get('/players?'.http_build_query($parameters)); |
55
|
|
|
if (!$response->isSuccessful()) { |
56
|
|
|
$this->registerLastError($response); |
57
|
|
|
|
58
|
|
|
return null; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$json = json_decode($response->getContent(), true); |
62
|
|
|
$players = $json['data']; |
63
|
|
|
|
64
|
|
|
$allPlayers[] = $this->castAll($players); |
65
|
|
|
|
66
|
|
|
if (null !== $callback) { |
67
|
|
|
foreach (current($allPlayers) as $player) { |
68
|
|
|
call_user_func($callback, $player); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (isset($parameters['currentPage'])) { |
73
|
|
|
break; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$pagination = $json['pagination']; |
77
|
|
|
$pagination['currentPage']++; |
78
|
|
|
} while ($pagination['pagesTotal'] > $pagination['currentPage']); |
79
|
|
|
|
80
|
|
|
$allPlayers = call_user_func_array('array_merge', $allPlayers); |
81
|
|
|
|
82
|
|
|
if (null === $callback) { |
83
|
|
|
return $allPlayers; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param array $properties |
91
|
|
|
* @return Player |
92
|
|
|
*/ |
93
|
|
|
public function create(array $properties = array()) |
94
|
|
|
{ |
95
|
|
|
$response = $this->browser->post( |
96
|
|
|
'/players', |
97
|
|
|
array(), |
98
|
|
|
json_encode( |
99
|
|
|
$properties |
100
|
|
|
) |
101
|
|
|
); |
102
|
|
|
if (!$response->isSuccessful()) { |
103
|
|
|
$this->registerLastError($response); |
104
|
|
|
|
105
|
|
|
return null; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->unmarshal($response); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $playerId |
113
|
|
|
* @param array $properties |
114
|
|
|
* @return Player |
115
|
|
|
*/ |
116
|
|
|
public function update($playerId, array $properties) |
117
|
|
|
{ |
118
|
|
|
$response = $this->browser->patch( |
119
|
|
|
"/players/$playerId", |
120
|
|
|
array(), |
121
|
|
|
json_encode($properties) |
122
|
|
|
); |
123
|
|
|
if (!$response->isSuccessful()) { |
124
|
|
|
$this->registerLastError($response); |
125
|
|
|
|
126
|
|
|
return null; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $this->unmarshal($response); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $source Path to the file to upload |
134
|
|
|
* @param $link |
135
|
|
|
* @param $playerId |
136
|
|
|
* @return Player|null |
137
|
|
|
*/ |
138
|
|
|
public function uploadLogo($source, $playerId, $link = null) |
139
|
|
|
{ |
140
|
|
|
if (!is_readable($source)) { |
141
|
|
|
throw new UnexpectedValueException("'$source' must be a readable source file."); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$resource = fopen($source, 'rb'); |
145
|
|
|
|
146
|
|
|
$stats = fstat($resource); |
|
|
|
|
147
|
|
|
$length = $stats['size']; |
148
|
|
|
if (0 >= $length) { |
149
|
|
|
throw new UnexpectedValueException("'$source' is empty."); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$payload = array( |
153
|
|
|
'file' => new FormUpload($source), |
154
|
|
|
); |
155
|
|
|
|
156
|
|
|
if (null !== $link) { |
157
|
|
|
$payload['link'] = $link; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$response = $this->browser->submit( |
161
|
|
|
"/players/$playerId/logo", |
162
|
|
|
$payload |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
if (!$response->isSuccessful()) { |
166
|
|
|
$this->registerLastError($response); |
167
|
|
|
|
168
|
|
|
return null; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this->unmarshal($response); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param string $playerId |
176
|
|
|
* @return int|null |
177
|
|
|
*/ |
178
|
|
|
public function delete($playerId) |
179
|
|
|
{ |
180
|
|
|
$response = $this->browser->delete("/players/$playerId"); |
181
|
|
|
|
182
|
|
|
if (!$response->isSuccessful()) { |
183
|
|
|
$this->registerLastError($response); |
184
|
|
|
|
185
|
|
|
return null; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return $response->getStatusCode(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param array $data |
193
|
|
|
* @return Player |
194
|
|
|
*/ |
195
|
|
|
protected function cast(array $data) |
196
|
|
|
{ |
197
|
|
|
$player = new Player(); |
198
|
|
|
$player->playerId = $data['playerId']; |
199
|
|
|
$player->shapeMargin = $data['shapeMargin']; |
200
|
|
|
$player->shapeRadius = $data['shapeRadius']; |
201
|
|
|
$player->shapeAspect = $data['shapeAspect']; |
202
|
|
|
$player->shapeBackgroundTop = $data['shapeBackgroundTop']; |
203
|
|
|
$player->shapeBackgroundBottom = $data['shapeBackgroundBottom']; |
204
|
|
|
$player->text = $data['text']; |
205
|
|
|
$player->link = $data['link']; |
206
|
|
|
$player->linkHover = $data['linkHover']; |
207
|
|
|
$player->linkActive = $data['linkActive']; |
208
|
|
|
$player->trackPlayed = $data['trackPlayed']; |
209
|
|
|
$player->trackUnplayed = $data['trackUnplayed']; |
210
|
|
|
$player->trackBackground = $data['trackBackground']; |
211
|
|
|
$player->backgroundTop = $data['backgroundTop']; |
212
|
|
|
$player->backgroundBottom = $data['backgroundBottom']; |
213
|
|
|
$player->backgroundText = $data['backgroundText']; |
214
|
|
|
$player->language = $data['language']; |
215
|
|
|
$player->enableApi = $data['enableApi']; |
216
|
|
|
$player->enableControls = $data['enableControls']; |
217
|
|
|
$player->forceAutoplay = $data['forceAutoplay']; |
218
|
|
|
$player->hideTitle = $data['hideTitle']; |
219
|
|
|
$player->forceLoop = $data['forceLoop']; |
220
|
|
|
if (array_key_exists('assets', $data)) { |
221
|
|
|
$player->logo = $data['assets']; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
return $player; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|