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 $boundary;
16:
17: /** @var string */
18: private $contentType;
19:
20: /** @var array */
21: private $values;
22:
23: /** @var array */
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: * @return string
36: */
37: public function getBoundary()
38: {
39: return $this->boundary;
40: }
41:
42: /**
43: * @return string
44: */
45: public function getContentType()
46: {
47: return $this->contentType;
48: }
49:
50: /**
51: * @return array
52: */
53: public function getValues()
54: {
55: return $this->values;
56: }
57:
58: /**
59: * @return array
60: */
61: public function getFiles()
62: {
63: return $this->files;
64: }
65:
66: /**
67: * @param string $parameterName
68: * @param string $value
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: * @param string $parameterName
86: * @param UploadableFile $file
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: