| 1 |
<?php |
| 2 |
|
| 3 |
namespace Square\Utils; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use GuzzleHttp\Psr7\Utils; |
| 7 |
use Psr\Http\Message\StreamInterface; |
| 8 |
use Square\Core\Multipart\MultipartFormDataPart; |
| 9 |
|
| 10 |
/** |
| 11 |
* File is a utility class used to transfer files as multipart form data. |
| 12 |
*/ |
| 13 |
class File |
| 14 |
{ |
| 15 |
/** @var StreamInterface */ |
| 16 |
private StreamInterface $stream; |
| 17 |
|
| 18 |
/** @var ?string */ |
| 19 |
private ?string $filename; |
| 20 |
|
| 21 |
/** @var ?string */ |
| 22 |
private ?string $contentType; |
| 23 |
|
| 24 |
/** |
| 25 |
* @param StreamInterface $stream |
| 26 |
* @param ?string $filename |
| 27 |
* @param ?string $contentType |
| 28 |
*/ |
| 29 |
public function __construct( |
| 30 |
StreamInterface $stream, |
| 31 |
?string $filename = null, |
| 32 |
?string $contentType = null, |
| 33 |
) { |
| 34 |
$this->filename = $filename; |
| 35 |
$this->contentType = $contentType; |
| 36 |
$this->stream = $stream; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Creates a File instance from a filepath. |
| 41 |
* |
| 42 |
* @param string $filepath |
| 43 |
* @param ?string $filename |
| 44 |
* @param ?string $contentType |
| 45 |
* @return File |
| 46 |
* @throws Exception |
| 47 |
*/ |
| 48 |
public static function createFromFilepath( |
| 49 |
string $filepath, |
| 50 |
?string $filename = null, |
| 51 |
?string $contentType = null, |
| 52 |
): File { |
| 53 |
$resource = fopen($filepath, 'r'); |
| 54 |
if (!$resource) { |
| 55 |
throw new Exception("Unable to open file $filepath"); |
| 56 |
} |
| 57 |
$stream = Utils::streamFor($resource); |
| 58 |
if (!$stream->isReadable()) { |
| 59 |
throw new Exception("File $filepath is not readable"); |
| 60 |
} |
| 61 |
return new self( |
| 62 |
stream: $stream, |
| 63 |
filename: $filename ?? basename($filepath), |
| 64 |
contentType: $contentType, |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Creates a File instance from a string. |
| 70 |
* |
| 71 |
* @param string $content |
| 72 |
* @param ?string $filename |
| 73 |
* @param ?string $contentType |
| 74 |
* @return File |
| 75 |
*/ |
| 76 |
public static function createFromString( |
| 77 |
string $content, |
| 78 |
?string $filename, |
| 79 |
?string $contentType = null, |
| 80 |
): File { |
| 81 |
return new self( |
| 82 |
stream: Utils::streamFor($content), |
| 83 |
filename: $filename, |
| 84 |
contentType: $contentType, |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Maps this File into a multipart form data part. |
| 90 |
* |
| 91 |
* @param string $name The name of the multipart form data part. |
| 92 |
* @param ?string $contentType Overrides the Content-Type associated with the file, if any. |
| 93 |
* @return MultipartFormDataPart |
| 94 |
*/ |
| 95 |
public function toMultipartFormDataPart(string $name, ?string $contentType = null): MultipartFormDataPart |
| 96 |
{ |
| 97 |
$contentType ??= $this->contentType; |
| 98 |
$headers = $contentType != null |
| 99 |
? ['Content-Type' => $contentType] |
| 100 |
: null; |
| 101 |
|
| 102 |
return new MultipartFormDataPart( |
| 103 |
name: $name, |
| 104 |
value: $this->stream, |
| 105 |
filename: $this->filename, |
| 106 |
headers: $headers, |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Closes the file stream. |
| 112 |
*/ |
| 113 |
public function close(): void |
| 114 |
{ |
| 115 |
$this->stream->close(); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Destructor to ensure stream is closed. |
| 120 |
*/ |
| 121 |
public function __destruct() |
| 122 |
{ |
| 123 |
$this->close(); |
| 124 |
} |
| 125 |
} |
| 126 |
|