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