1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ApiVideo\Client\Api; |
4
|
|
|
|
5
|
|
|
use ApiVideo\Client\Model\Caption; |
6
|
|
|
use Buzz\Message\Form\FormUpload; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
class Captions extends BaseApi |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @param string $videoId |
13
|
|
|
* @param $language |
14
|
|
|
* @return Caption|null |
15
|
|
|
*/ |
16
|
|
|
public function get($videoId, $language) |
17
|
|
|
{ |
18
|
|
|
$response = $this->browser->get("/videos/$videoId/captions/$language"); |
19
|
|
|
if (!$response->isSuccessful()) { |
20
|
|
|
$this->registerLastError($response); |
21
|
|
|
|
22
|
|
|
return null; |
23
|
|
|
} |
24
|
|
|
return $this->unmarshal($response); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $videoId |
29
|
|
|
* @return Caption[]|null |
30
|
|
|
*/ |
31
|
|
|
public function getAll($videoId) |
32
|
|
|
{ |
33
|
|
|
$response = $this->browser->get("/videos/$videoId/captions"); |
34
|
|
|
if (!$response->isSuccessful()) { |
35
|
|
|
$this->registerLastError($response); |
36
|
|
|
|
37
|
|
|
return null; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$json = json_decode($response->getContent(), true); |
41
|
|
|
$captions = $json['data']; |
42
|
|
|
|
43
|
|
|
return $this->castAll($captions); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $source |
49
|
|
|
* @param array $properties |
50
|
|
|
* @return Caption|null |
51
|
|
|
*/ |
52
|
|
|
public function upload($source, array $properties = array()) |
53
|
|
|
{ |
54
|
|
|
if (!is_readable($source)) { |
55
|
|
|
throw new InvalidArgumentException('The source file must be readable.'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (!isset($properties['videoId'])) { |
59
|
|
|
throw new InvalidArgumentException('"videoId" property must be set for upload caption.'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (!isset($properties['language'])) { |
63
|
|
|
throw new InvalidArgumentException('"language" property must be set for upload caption.'); |
64
|
|
|
} |
65
|
|
|
$videoId = $properties['videoId']; |
66
|
|
|
$language = $properties['language']; |
67
|
|
|
|
68
|
|
|
$resource = fopen($source, 'rb'); |
69
|
|
|
|
70
|
|
|
$stats = fstat($resource); |
71
|
|
|
$length = $stats['size']; |
72
|
|
|
if (0 >= $length) { |
73
|
|
|
throw new InvalidArgumentException("'$source' is an empty file."); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$response = $this->browser->submit( |
77
|
|
|
"/videos/$videoId/captions/$language", |
78
|
|
|
array('file' => new FormUpload($source)) |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
if (!$response->isSuccessful()) { |
82
|
|
|
$this->registerLastError($response); |
83
|
|
|
|
84
|
|
|
return null; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->unmarshal($response); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $videoId |
92
|
|
|
* @param $language |
93
|
|
|
* @param $isDefault |
94
|
|
|
* @return Caption |
95
|
|
|
*/ |
96
|
|
|
public function updateDefault($videoId, $language, $isDefault) |
97
|
|
|
{ |
98
|
|
|
$response = $this->browser->patch( |
99
|
|
|
"/videos/$videoId/captions/$language", |
100
|
|
|
array(), |
101
|
|
|
json_encode(array('default' => $isDefault)) |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
if (!$response->isSuccessful()) { |
105
|
|
|
$this->registerLastError($response); |
106
|
|
|
|
107
|
|
|
return null; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this->unmarshal($response); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $videoId |
115
|
|
|
* @param $language |
116
|
|
|
* @return int|null |
117
|
|
|
*/ |
118
|
|
|
public function delete($videoId, $language) |
119
|
|
|
{ |
120
|
|
|
$response = $this->browser->delete("/videos/$videoId/captions/$language"); |
121
|
|
|
|
122
|
|
|
if (!$response->isSuccessful()) { |
123
|
|
|
$this->registerLastError($response); |
124
|
|
|
|
125
|
|
|
return null; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $response->getStatusCode(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param array $data |
133
|
|
|
* @return Caption |
134
|
|
|
*/ |
135
|
|
|
protected function cast(array $data) |
136
|
|
|
{ |
137
|
|
|
$caption = new Caption(); |
138
|
|
|
$caption->uri = $data['uri']; |
139
|
|
|
$caption->src = $data['src']; |
140
|
|
|
$caption->srclang = $data['srclang']; |
141
|
|
|
$caption->default = $data['default']; |
142
|
|
|
|
143
|
|
|
return $caption; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|