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