42 lines
1.2 KiB
PHP
42 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Domain\Models\ValueObjects;
|
|
|
|
final readonly class RawPicturePath
|
|
{
|
|
public const array ALLOWED_EXTENSIONS = PicturePath::ALLOWED_EXTENSIONS;
|
|
|
|
public const int MAX_SIZE_IN_BYTES = 5 * 1024 * 1024;
|
|
|
|
/**
|
|
* @throws \DomainException
|
|
*/
|
|
public function __construct(
|
|
public string $content,
|
|
public string $originalName,
|
|
) {
|
|
if ($this->content === '') {
|
|
throw new \DomainException('Raw picture content cannot be empty.');
|
|
}
|
|
|
|
if (strlen($this->content) > self::MAX_SIZE_IN_BYTES) {
|
|
throw new \DomainException('Raw picture exceeds the maximum allowed size.');
|
|
}
|
|
|
|
$trimmedName = mb_trim($this->originalName);
|
|
|
|
if ($trimmedName === '') {
|
|
throw new \DomainException('Raw picture original name cannot be empty.');
|
|
}
|
|
|
|
$extension = mb_strtolower(pathinfo($trimmedName, PATHINFO_EXTENSION));
|
|
if (! in_array($extension, self::ALLOWED_EXTENSIONS, true)) {
|
|
throw new \DomainException(
|
|
'Raw picture must have one of these extensions: ' .
|
|
implode(', ', self::ALLOWED_EXTENSIONS)
|
|
);
|
|
}
|
|
}
|
|
}
|