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