|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) Padosoft.com 2016. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Padosoft\HTTPClient; |
|
7
|
|
|
|
|
8
|
|
|
use Psr\Http\Message\StreamInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class Multipart |
|
12
|
|
|
* @package Padosoft\HTTPClient |
|
13
|
|
|
*/ |
|
14
|
|
|
class Multipart |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* required "name" key mapping to the form field, name |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $name = ''; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* required "contents" key mapping to a StreamInterface|resource|string |
|
24
|
|
|
* @var (StreamInterface|resource|string) string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $contents; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* optional "headers" associative array of custom headers |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $headers; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* optional "filename" key mapping to a string to send as the filename in |
|
36
|
|
|
* the part. If no "filename" key is present, then no "filename" attribute |
|
37
|
|
|
* will be added to the part. |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $filename=''; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Multipart constructor. |
|
44
|
|
|
* @param string $name required "name" key mapping to the form field, name |
|
45
|
|
|
* @param (StreamInterface|resource|string) $contents required "contents" key mapping to a StreamInterface|resource|string |
|
46
|
|
|
* @param string $filename optional "filename" key mapping to a string to send as the filename in |
|
47
|
|
|
* the part. If no "filename" key is present, then no "filename" attribute |
|
48
|
|
|
* will be added to the part. |
|
49
|
|
|
* @param array $headers optional "headers" associative array of custom headers |
|
50
|
|
|
* @throws \Exception |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct($name, $contents, $filename=null, $headers=[]) |
|
53
|
|
|
{ |
|
54
|
|
|
if($name===null || $name==''){ |
|
55
|
|
|
throw new \Exception("name is mandatory in Multipart!"); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if(!is_resource($contents) && !is_string($contents) && !is_a($contents, StreamInterface::class)) |
|
59
|
|
|
{ |
|
60
|
|
|
throw new \Exception("contents is mandatory in Multipart and it's a StreamInterface or resource or string!"); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$this->name = $name; |
|
64
|
|
|
$this->contents = $contents; |
|
65
|
|
|
|
|
66
|
|
|
if(!is_string($filename)) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->filename = null; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if(!is_array($headers)) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->headers = []; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return resource|string |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getContents() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->contents; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getFilename() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->filename; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return array|null |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getHeaders() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->headers; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getName() |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->name; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Return an associative array for RequestHelper::SetMultipart() |
|
111
|
|
|
* @return array |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getMultipartArray() |
|
114
|
|
|
{ |
|
115
|
|
|
$arrMultipart = array(); |
|
116
|
|
|
|
|
117
|
|
|
$arrMultipart[] = ['name' => $this->name, |
|
118
|
|
|
'contents' => $this->contents, |
|
119
|
|
|
'headers' => $this->headers, |
|
120
|
|
|
'filename' => $this->filename |
|
121
|
|
|
]; |
|
122
|
|
|
|
|
123
|
|
|
return $arrMultipart; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|