1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Yproximite\Api\Message\Media; |
5
|
|
|
|
6
|
|
|
use Http\Discovery\StreamFactoryDiscovery; |
7
|
|
|
use Http\Message\MultipartStream\MultipartStreamBuilder; |
8
|
|
|
|
9
|
|
|
use Yproximite\Api\Exception\LogicException; |
10
|
|
|
use Yproximite\Api\Message\MessageInterface; |
11
|
|
|
use Yproximite\Api\Message\SiteAwareMessageTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class MediaUploadMessage |
15
|
|
|
*/ |
16
|
|
|
class MediaUploadMessage implements MessageInterface |
17
|
|
|
{ |
18
|
|
|
use SiteAwareMessageTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var MediaUploadFileMessage[] |
22
|
|
|
*/ |
23
|
|
|
private $files = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var MultipartStreamBuilder|null |
27
|
|
|
*/ |
28
|
|
|
private $builder; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return MediaUploadFileMessage[] |
32
|
|
|
*/ |
33
|
|
|
public function getFiles() |
34
|
|
|
{ |
35
|
|
|
return $this->files; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param MediaUploadFileMessage $file |
40
|
|
|
*/ |
41
|
|
|
public function addFile(MediaUploadFileMessage $file) |
42
|
|
|
{ |
43
|
|
|
$this->files[] = $file; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param MediaUploadFileMessage $file |
48
|
|
|
*/ |
49
|
|
|
public function removeFile(MediaUploadFileMessage $file) |
50
|
|
|
{ |
51
|
|
|
array_splice($this->files, array_search($file, $this->files), 1); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string|null $boundary |
56
|
|
|
*/ |
57
|
|
|
public function initBuilder(string $boundary = null) |
58
|
|
|
{ |
59
|
|
|
$streamFactory = StreamFactoryDiscovery::find(); |
60
|
|
|
$builder = new MultipartStreamBuilder($streamFactory); |
61
|
|
|
|
62
|
|
|
if (!is_null($boundary)) { |
63
|
|
|
$builder->setBoundary($boundary); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
foreach ($this->getFiles() as $i => $file) { |
67
|
|
|
$options = []; |
68
|
|
|
|
69
|
|
|
if (!is_null($file->getFilename())) { |
70
|
|
|
$options['filename'] = $file->getFilename(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$builder->addResource(sprintf('api_upload_media[medias][%d]', $i), $file->getResource(), $options); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->builder = $builder; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
public function buildHeaders(): array |
83
|
|
|
{ |
84
|
|
|
if (is_null($this->builder)) { |
85
|
|
|
throw new LogicException('You need to initialize the builder before call this method.'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return [ |
89
|
|
|
'Content-Type' => sprintf('multipart/form-data; boundary="%s"', $this->builder->getBoundary()), |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
public function build() |
97
|
|
|
{ |
98
|
|
|
if (is_null($this->builder)) { |
99
|
|
|
throw new LogicException('You need to initialize the builder before call this method.'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this->builder->build(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|