Skip to content

Commit 677c456

Browse files
author
Greg Bowler
authored
Upload files using FormData (#214)
* wip: add formdata class * wip: add urlsearchparams class * feature: implement URLSearchParams * feature: tweak urlsearchparams * wip: extract KeyValuePairStore * wip: implement KeyValuePairStore for FormData * wip: improve tests by expecting warning to be emitted * wip: empty formdata options * wip: test submitter is included in the form submission * wip: test constructing with square brackets * wip: fix static analysis error on DOMNodeList * wip: implement form types * wip: remove unused constants * feature: implement promise-based functions closes #208 * test: improve static tests * wip: uploading of files for #213
1 parent 292e850 commit 677c456

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"php": ">=8.1",
1515
"ext-dom": "*",
1616
"ext-curl": "*",
17+
"ext-fileinfo": "*",
1718
"phpgt/input": "^1.2",
1819
"phpgt/typesafegetter": "^1.3",
1920
"phpgt/promise": "^2.2",

src/File.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
<?php
22
namespace Gt\Http;
33

4+
use SplFileObject;
5+
46
class File extends Blob {
57
/**
68
* @param array<ArrayBuffer|Blob|string> $bits
79
* @param string $name
810
* @param array<string, string> $options
911
*/
1012
public function __construct(
11-
array $bits,
13+
SplFileObject|array $bits,
1214
string $name,
1315
array $options = [],
1416
) {
17+
if($bits instanceof SplFileObject) {
18+
$file = $bits;
19+
$bits = [];
20+
while(!$file->eof()) {
21+
array_push($bits, $file->fread(1024));
22+
}
23+
}
24+
25+
/** @var array<ArrayBuffer|Blob|string> $bits */
26+
1527
parent::__construct($bits, $options);
1628
$this->name = $name;
1729
}

src/FormData.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use DOMXPath;
77
use Generator;
88
use Gt\TypeSafeGetter\NullableTypeSafeGetter;
9+
use SplFileObject;
910
use Stringable;
1011
use Countable;
1112
use Iterator;
@@ -104,17 +105,19 @@ public function getBlob(string $name):?Blob {
104105
*/
105106
public function append(
106107
string $name,
107-
Blob|File|string $value,
108+
Blob|File|SplFileObject|string $value,
108109
string $filename = null
109110
):void {
111+
$value = $this->normaliseFileValue($value);
110112
$this->appendAnyValue($name, $value, $filename);
111113
}
112114

113115
public function set(
114116
string $name,
115-
Blob|File|string $value,
117+
Blob|File|SplFileObject|string $value,
116118
?string $filename = null,
117119
):void {
120+
$value = $this->normaliseFileValue($value);
118121
$this->setAnyValue($name, $value, $filename);
119122
}
120123

@@ -183,4 +186,14 @@ private function getValueFromSelect(DOMElement $select):string {
183186

184187
return "";
185188
}
189+
190+
private function normaliseFileValue(Blob|File|SplFileObject|string $value):Blob|File|string {
191+
if(!$value instanceof SplFileObject) {
192+
return $value;
193+
}
194+
195+
/** @var SplFileObject $file */
196+
$file = $value;
197+
return new File($file, $file->getFilename());
198+
}
186199
}

src/KeyValuePairStore.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace Gt\Http;
33

44
use Generator;
5+
use SplFileObject;
56

67
/** @SuppressWarnings("TooManyPublicMethods") */
78
abstract class KeyValuePairStore {

0 commit comments

Comments
 (0)