1: <?php
2: namespace Worldline\Connect\Sdk\Domain;
3:
4: use UnexpectedValueException;
5:
6: /**
7: * Class UploadableFile
8: *
9: * @package Worldline\Connect\Sdk\Domain
10: */
11: class UploadableFile
12: {
13: /**
14: * @var string
15: */
16: private string $fileName;
17:
18: /**
19: * @var resource|string|callable
20: */
21: private $content;
22:
23: /**
24: * @var string
25: */
26: private string $contentType;
27:
28: /**
29: * @var int
30: */
31: private int $contentLength;
32:
33: /**
34: * @param string $fileName
35: * @param resource|string|callable $content If it's a callable it should take a length argument
36: * and return a string that is not larger than the input.
37: * @param string $contentType
38: * @param int $contentLength
39: */
40: public function __construct(string $fileName, $content, string $contentType, int $contentLength = -1)
41: {
42: if (strlen(trim($fileName)) == 0) {
43: throw new UnexpectedValueException("fileName is required");
44: }
45: if (!is_resource($content) && !is_string($content) && !is_callable($content)) {
46: throw new UnexpectedValueException('content is required as resource, string or callable');
47: }
48: if (strlen(trim($contentType)) == 0) {
49: throw new UnexpectedValueException("contentType is required");
50: }
51: $this->fileName = $fileName;
52: $this->content = $content;
53: $this->contentType = $contentType;
54: $this->contentLength = max($contentLength, -1);
55: if ($this->contentLength == -1 && is_string($content)) {
56: $this->contentLength = strlen($content);
57: }
58: }
59:
60: /**
61: * @return string The name of the file.
62: */
63: public function getFileName(): string
64: {
65: return $this->fileName;
66: }
67:
68: /**
69: * @return resource|string|callable A resource, string or callable with the file's content.
70: * If it's a callable it should take a length argument and return a string that is not larger than the input.
71: */
72: public function getContent()
73: {
74: return $this->content;
75: }
76:
77: /**
78: * @return string The file's content type.
79: */
80: public function getContentType(): string
81: {
82: return $this->contentType;
83: }
84:
85: /**
86: * @return int The file's content length, or -1 if not known.
87: */
88: public function getContentLength(): int
89: {
90: return $this->contentLength;
91: }
92: }
93: