| 1: | <?php |
| 2: | namespace Worldline\Connect\Sdk\Communication; |
| 3: | |
| 4: | use UnexpectedValueException; |
| 5: | use Worldline\Connect\Sdk\Domain\UploadableFile; |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | class MultipartFormDataObject |
| 13: | { |
| 14: | |
| 15: | |
| 16: | |
| 17: | private string $boundary; |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | private string $contentType; |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | private array $values; |
| 28: | |
| 29: | |
| 30: | |
| 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: | |
| 44: | |
| 45: | public function getBoundary(): string |
| 46: | { |
| 47: | return $this->boundary; |
| 48: | } |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | public function getContentType(): string |
| 54: | { |
| 55: | return $this->contentType; |
| 56: | } |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | public function getValues(): array |
| 62: | { |
| 63: | return $this->values; |
| 64: | } |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | public function getFiles(): array |
| 70: | { |
| 71: | return $this->files; |
| 72: | } |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 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: | |
| 93: | |
| 94: | |
| 95: | |
| 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: | |