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