Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize properties of FileUpload class #235

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/Http/FileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ final class FileUpload
/** @deprecated */
public const IMAGE_MIME_TYPES = ['image/gif', 'image/png', 'image/jpeg', 'image/webp'];

private string $name;
private ?string $name = null;
private string|null $fullPath;
private string|false|null $type = null;
private string|false|null $extension = null;
private int $size;
private string $tmpName;
private int $error;
private int $size = 0;
private ?string $tmpName = null;
private ?int $error;


public function __construct(?array $value)
Expand All @@ -51,11 +51,11 @@ public function __construct(?array $value)
}
}

$this->name = $value['name'];
$this->name = $value['name'] ?? null;
$this->fullPath = $value['full_path'] ?? null;
$this->size = $value['size'];
$this->tmpName = $value['tmp_name'];
$this->error = $value['error'];
$this->size = isset($value['size']) ? (int) $value['size'] : 0;
$this->tmpName = $value['tmp_name'] ?? null;
$this->error = $value['error'] ?? null;
}


Expand Down Expand Up @@ -169,12 +169,12 @@ public function getTemporaryFile(): string
*/
public function __toString(): string
{
return $this->tmpName;
return $this->tmpName ?? '';
}


/**
* Returns the error code. It is be one of UPLOAD_ERR_XXX constants.
* Returns the error code. It has to be one of UPLOAD_ERR_XXX constants.
* @see http://php.net/manual/en/features.file-upload.errors.php
*/
public function getError(): int
Expand Down
3 changes: 3 additions & 0 deletions tests/Http/FileUpload.basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ test('', function () {
Assert::same(209, $upload->getSize());
Assert::same(__DIR__ . '/files/file.txt', $upload->getTemporaryFile());
Assert::same(__DIR__ . '/files/file.txt', (string) $upload);
Assert::same(__DIR__ . '/files/file.txt', $upload->__toString());
Assert::same(0, $upload->getError());
Assert::true($upload->isOk());
Assert::true($upload->hasFile());
Expand Down Expand Up @@ -71,4 +72,6 @@ test('', function () {
Assert::null($upload->getContentType());
Assert::false($upload->isImage());
Assert::null($upload->getSuggestedExtension());
Assert::same('', (string) $upload);
Assert::same('', $upload->__toString());
});