45 lines
1.3 KiB
PHP
45 lines
1.3 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Shared\Models\ValueObjects;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Represents raw, not a stored picture.
|
||
|
|
*/
|
||
|
|
final readonly class RawPicture
|
||
|
|
{
|
||
|
|
public const array ALLOWED_EXTENSIONS = PicturePath::ALLOWED_EXTENSIONS;
|
||
|
|
|
||
|
|
public const int MAX_SIZE_IN_BYTES = 5 * 1024 * 1024;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param string $content Binary content of the picture
|
||
|
|
* @param string $originalName Original filename, used to infer the extension
|
||
|
|
* @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 = trim($this->originalName);
|
||
|
|
|
||
|
|
if ($trimmedName === '') {
|
||
|
|
throw new \DomainException('Raw picture original name cannot be empty.');
|
||
|
|
}
|
||
|
|
|
||
|
|
$extension = 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));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|