PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.13
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.13
1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 1.7.0.9 All 211 releases
charitable / vendor / square / square / src / Utils / File.php

File.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.13, at vendor/square/square/src/Utils/File.php

126 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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