feat: implement preview configuration
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXC\Models\Tenant;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use KTXF\Preview\MimeType;
|
||||
|
||||
final readonly class TenantPreviewVariant
|
||||
{
|
||||
public string $format;
|
||||
|
||||
public function __construct(
|
||||
public int $width,
|
||||
public int $height,
|
||||
string $format,
|
||||
public int $quality,
|
||||
) {
|
||||
if ($this->width < 1 || $this->height < 1) {
|
||||
throw new InvalidArgumentException('Preview variant dimensions must be positive');
|
||||
}
|
||||
if ($this->quality < 1 || $this->quality > 100) {
|
||||
throw new InvalidArgumentException('Preview variant quality must be between 1 and 100');
|
||||
}
|
||||
$this->format = MimeType::normalize($format);
|
||||
}
|
||||
|
||||
public static function fromArray(array $data, ?self $defaults = null): self
|
||||
{
|
||||
$width = $data['width'] ?? $defaults?->width;
|
||||
$height = $data['height'] ?? $defaults?->height;
|
||||
$format = $data['format'] ?? $defaults?->format;
|
||||
$quality = $data['quality'] ?? $defaults?->quality;
|
||||
|
||||
if (!is_int($width) || !is_int($height) || !is_string($format) || !is_int($quality)) {
|
||||
throw new InvalidArgumentException('Preview variants require integer dimensions and quality plus a MIME format');
|
||||
}
|
||||
|
||||
return new self($width, $height, $format, $quality);
|
||||
}
|
||||
|
||||
/** @return array{width:int,height:int,format:string,quality:int} */
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'width' => $this->width,
|
||||
'height' => $this->height,
|
||||
'format' => $this->format,
|
||||
'quality' => $this->quality,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user