Skip to content

Commit

Permalink
FileUpload::__construct() accepts path
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 5, 2024
1 parent 5be5e6e commit d267af0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/Http/FileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ final class FileUpload
private readonly int $error;


public function __construct(?array $value)
public function __construct(array|string|null $value)
{
foreach (['name', 'size', 'tmp_name', 'error'] as $key) {
if (!isset($value[$key]) || !is_scalar($value[$key])) {
$value = [];
break;
}
if (is_string($value)) {
$value = [
'name' => basename($value),
'full_path' => $value,
'size' => filesize($value),
'tmp_name' => $value,
'error' => UPLOAD_ERR_OK,
];
}

$this->name = $value['name'] ?? '';
Expand Down
19 changes: 19 additions & 0 deletions tests/Http/FileUpload.basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,22 @@ test('', function () {
Assert::null($upload->getSuggestedExtension());
Assert::same('', (string) $upload);
});


test('', function () {
$upload = new FileUpload($file = __DIR__ . '/files/file.txt');

Assert::same('file.txt', $upload->getName());
Assert::same('file.txt', $upload->getUntrustedName());
Assert::same('file.txt', $upload->getSanitizedName());
Assert::same($file, $upload->getUntrustedFullPath());
Assert::same(filesize($file), $upload->getSize());
Assert::same($file, $upload->getTemporaryFile());
Assert::same($file, (string) $upload);
Assert::same(0, $upload->getError());
Assert::true($upload->isOk());
Assert::true($upload->hasFile());
Assert::false($upload->isImage());
Assert::null($upload->getSuggestedExtension());
Assert::same(file_get_contents($file), $upload->getContents());
});

0 comments on commit d267af0

Please sign in to comment.