| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Validator; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use ipl\Stdlib\Str; |
| 7 |
use Psr\Http\Message\UploadedFileInterface; |
| 8 |
|
| 9 |
/** |
| 10 |
* Validate an uploaded file |
| 11 |
*/ |
| 12 |
class FileValidator extends BaseValidator |
| 13 |
{ |
| 14 |
/** @var int Minimum allowed file size */ |
| 15 |
protected int $minSize; |
| 16 |
|
| 17 |
/** @var ?int Maximum allowed file size */ |
| 18 |
protected ?int $maxSize = null; |
| 19 |
|
| 20 |
/** @var ?string[] Allowed mime types */ |
| 21 |
protected ?array $allowedMimeTypes; |
| 22 |
|
| 23 |
/** @var ?int Maximum allowed file name length */ |
| 24 |
protected ?int $maxFileNameLength; |
| 25 |
|
| 26 |
/** |
| 27 |
* Create a new FileValidator |
| 28 |
* |
| 29 |
* Optional options: |
| 30 |
* - minSize: (int) Minimum allowed file size, by default 0 |
| 31 |
* - maxSize: (int) Maximum allowed file size, by default no limit |
| 32 |
* - maxFileNameLength: (int) Maximum allowed file name length, by default no limit |
| 33 |
* - mimeType: (array) Allowed mime types, by default no restriction |
| 34 |
* |
| 35 |
* @param array{minSize?: int, maxSize?: int, maxFileNameLength?: int, mimeType?: string[]} $options |
| 36 |
*/ |
| 37 |
public function __construct(array $options = []) |
| 38 |
{ |
| 39 |
$this |
| 40 |
->setMinSize($options['minSize'] ?? 0) |
| 41 |
->setMaxSize($options['maxSize'] ?? null) |
| 42 |
->setMaxFileNameLength($options['maxFileNameLength'] ?? null) |
| 43 |
->setAllowedMimeTypes($options['mimeType'] ?? null); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Get the minimum allowed file size |
| 48 |
* |
| 49 |
* @return int |
| 50 |
*/ |
| 51 |
public function getMinSize(): int |
| 52 |
{ |
| 53 |
return $this->minSize; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Set the minimum allowed file size |
| 58 |
* |
| 59 |
* @param int $minSize |
| 60 |
* |
| 61 |
* @return $this |
| 62 |
* |
| 63 |
* @throws InvalidArgumentException If minSize exceeds maxSize |
| 64 |
*/ |
| 65 |
public function setMinSize(int $minSize): static |
| 66 |
{ |
| 67 |
if (($max = $this->getMaxSize()) !== null && $minSize > $max) { |
| 68 |
throw new InvalidArgumentException( |
| 69 |
sprintf( |
| 70 |
'The minSize must be less than or equal to the maxSize, but minSize: %d and maxSize: %d given.', |
| 71 |
$minSize, |
| 72 |
$max |
| 73 |
) |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
$this->minSize = $minSize; |
| 78 |
|
| 79 |
return $this; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Get the maximum allowed file size |
| 84 |
* |
| 85 |
* @return ?int |
| 86 |
*/ |
| 87 |
public function getMaxSize(): ?int |
| 88 |
{ |
| 89 |
return $this->maxSize; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Set the maximum allowed file size |
| 94 |
* |
| 95 |
* @param ?int $maxSize |
| 96 |
* |
| 97 |
* @return $this |
| 98 |
* |
| 99 |
* @throws InvalidArgumentException If maxSize is less than minSize |
| 100 |
*/ |
| 101 |
public function setMaxSize(?int $maxSize): static |
| 102 |
{ |
| 103 |
if ($maxSize !== null && ($min = $this->getMinSize()) !== null && $maxSize < $min) { |
| 104 |
throw new InvalidArgumentException( |
| 105 |
sprintf( |
| 106 |
'The minSize must be less than or equal to the maxSize, but minSize: %d and maxSize: %d given.', |
| 107 |
$min, |
| 108 |
$maxSize |
| 109 |
) |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
$this->maxSize = $maxSize; |
| 114 |
|
| 115 |
return $this; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Get the allowed file mime types |
| 120 |
* |
| 121 |
* @return ?string[] |
| 122 |
*/ |
| 123 |
public function getAllowedMimeTypes(): ?array |
| 124 |
{ |
| 125 |
return $this->allowedMimeTypes; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Set the allowed file mime types |
| 130 |
* |
| 131 |
* @param ?string[] $allowedMimeTypes |
| 132 |
* |
| 133 |
* @return $this |
| 134 |
*/ |
| 135 |
public function setAllowedMimeTypes(?array $allowedMimeTypes): static |
| 136 |
{ |
| 137 |
$this->allowedMimeTypes = $allowedMimeTypes; |
| 138 |
|
| 139 |
return $this; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Get maximum allowed file name length |
| 144 |
* |
| 145 |
* @return ?int |
| 146 |
*/ |
| 147 |
public function getMaxFileNameLength(): ?int |
| 148 |
{ |
| 149 |
return $this->maxFileNameLength; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Set maximum allowed file name length |
| 154 |
* |
| 155 |
* @param ?int $maxFileNameLength |
| 156 |
* |
| 157 |
* @return $this |
| 158 |
*/ |
| 159 |
public function setMaxFileNameLength(?int $maxFileNameLength): static |
| 160 |
{ |
| 161 |
$this->maxFileNameLength = $maxFileNameLength; |
| 162 |
|
| 163 |
return $this; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Check whether the uploaded file passes all configured constraints |
| 168 |
* |
| 169 |
* @param UploadedFileInterface|UploadedFileInterface[] $value |
| 170 |
* |
| 171 |
* @return bool |
| 172 |
*/ |
| 173 |
public function isValid($value): bool |
| 174 |
{ |
| 175 |
// Reset messages from a previous isValid() call. |
| 176 |
$this->clearMessages(); |
| 177 |
|
| 178 |
if (is_array($value)) { |
| 179 |
foreach ($value as $file) { |
| 180 |
if (! $this->validateFile($file)) { |
| 181 |
return false; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
return true; |
| 186 |
} |
| 187 |
|
| 188 |
return $this->validateFile($value); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
/** |
| 193 |
* Validate a single uploaded file against all configured constraints |
| 194 |
* |
| 195 |
* @param UploadedFileInterface $file |
| 196 |
* |
| 197 |
* @return bool |
| 198 |
*/ |
| 199 |
private function validateFile(UploadedFileInterface $file): bool |
| 200 |
{ |
| 201 |
$isValid = true; |
| 202 |
if ($this->getMaxSize() && $file->getSize() > $this->getMaxSize()) { |
| 203 |
$this->addMessage(sprintf( |
| 204 |
$this->translate('File %s is bigger than the allowed maximum size of %d'), |
| 205 |
$file->getClientFilename(), |
| 206 |
$this->getMaxSize() |
| 207 |
)); |
| 208 |
|
| 209 |
$isValid = false; |
| 210 |
} |
| 211 |
|
| 212 |
if ($this->getMinSize() && $file->getSize() < $this->getMinSize()) { |
| 213 |
$this->addMessage(sprintf( |
| 214 |
$this->translate('File %s is smaller than the minimum required size of %d'), |
| 215 |
$file->getClientFilename(), |
| 216 |
$this->getMinSize() |
| 217 |
)); |
| 218 |
|
| 219 |
$isValid = false; |
| 220 |
} |
| 221 |
|
| 222 |
if ($this->getMaxFileNameLength()) { |
| 223 |
$strValidator = new StringLengthValidator(['max' => $this->getMaxFileNameLength()]); |
| 224 |
|
| 225 |
if (! $strValidator->isValid($file->getClientFilename() ?? '')) { |
| 226 |
$this->addMessage(sprintf( |
| 227 |
$this->translate('File name is longer than the allowed length of %d characters.'), |
| 228 |
$this->maxFileNameLength |
| 229 |
)); |
| 230 |
|
| 231 |
$isValid = false; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
if (! empty($this->getAllowedMimeTypes())) { |
| 236 |
$hasAllowedMimeType = false; |
| 237 |
$fileMimetype = $file->getClientMediaType(); |
| 238 |
|
| 239 |
if ($fileMimetype) { |
| 240 |
foreach ($this->getAllowedMimeTypes() as $type) { |
| 241 |
if (($pos = strpos($type, '/*')) !== false) { // image/* |
| 242 |
$typePrefix = substr($type, 0, $pos); |
| 243 |
if (Str::startsWith($fileMimetype, $typePrefix)) { |
| 244 |
$hasAllowedMimeType = true; |
| 245 |
break; |
| 246 |
} |
| 247 |
} elseif ($fileMimetype === $type) { // image/png |
| 248 |
$hasAllowedMimeType = true; |
| 249 |
break; |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
if (! $hasAllowedMimeType) { |
| 255 |
$this->addMessage(sprintf( |
| 256 |
$this->translate('File %s is of type %s. Only %s allowed.'), |
| 257 |
$file->getClientFilename(), |
| 258 |
$file->getClientMediaType(), |
| 259 |
implode(', ', $this->allowedMimeTypes ?? []) |
| 260 |
)); |
| 261 |
|
| 262 |
$isValid = false; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
return $isValid; |
| 267 |
} |
| 268 |
} |