1: | <?php |
2: | namespace Worldline\Acquiring\Sdk\Communication; |
3: | |
4: | use UnexpectedValueException; |
5: | use Worldline\Acquiring\Sdk\Domain\UploadableFile; |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | class MultipartFormDataObject |
13: | { |
14: | |
15: | private $boundary; |
16: | |
17: | |
18: | private $contentType; |
19: | |
20: | |
21: | private $values; |
22: | |
23: | |
24: | private $files; |
25: | |
26: | public function __construct() |
27: | { |
28: | $this->boundary = UuidGenerator::generatedUuid(); |
29: | $this->contentType = 'multipart/form-data; boundary=' . $this->boundary; |
30: | $this->values = []; |
31: | $this->files = []; |
32: | } |
33: | |
34: | |
35: | |
36: | |
37: | public function getBoundary() |
38: | { |
39: | return $this->boundary; |
40: | } |
41: | |
42: | |
43: | |
44: | |
45: | public function getContentType() |
46: | { |
47: | return $this->contentType; |
48: | } |
49: | |
50: | |
51: | |
52: | |
53: | public function getValues() |
54: | { |
55: | return $this->values; |
56: | } |
57: | |
58: | |
59: | |
60: | |
61: | public function getFiles() |
62: | { |
63: | return $this->files; |
64: | } |
65: | |
66: | |
67: | |
68: | |
69: | |
70: | public function addValue($parameterName, $value) |
71: | { |
72: | if (is_null($parameterName) || strlen(trim($parameterName)) == 0) { |
73: | throw new UnexpectedValueException("boundary is required"); |
74: | } |
75: | if (is_null($value)) { |
76: | throw new UnexpectedValueException("value is required"); |
77: | } |
78: | if (array_key_exists($parameterName, $this->values) || array_key_exists($parameterName, $this->files)) { |
79: | throw new UnexpectedValueException('Duplicate parameter name: ' . $parameterName); |
80: | } |
81: | $this->values[$parameterName] = $value; |
82: | } |
83: | |
84: | |
85: | |
86: | |
87: | |
88: | public function addFile($parameterName, UploadableFile $file) |
89: | { |
90: | if (is_null($parameterName) || strlen(trim($parameterName)) == 0) { |
91: | throw new UnexpectedValueException("boundary is required"); |
92: | } |
93: | if (array_key_exists($parameterName, $this->values) || array_key_exists($parameterName, $this->files)) { |
94: | throw new UnexpectedValueException('Duplicate parameter name: ' . $parameterName); |
95: | } |
96: | $this->files[$parameterName] = $file; |
97: | } |
98: | } |
99: | |