f39ad1804d
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
47 lines
1.4 KiB
PHP
47 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KTXF\Preview;
|
|
|
|
use Closure;
|
|
use InvalidArgumentException;
|
|
use KTXF\Resource\BinaryResource;
|
|
|
|
final readonly class PreviewSource
|
|
{
|
|
public string $mimeType;
|
|
|
|
/**
|
|
* @param Closure(): BinaryResource $resourceFactory
|
|
*/
|
|
public function __construct(
|
|
public string $sourceType,
|
|
public string $identity,
|
|
public string $signature,
|
|
string $mimeType,
|
|
public ?int $size,
|
|
public Closure $resourceFactory,
|
|
) {
|
|
if (preg_match('/^[a-z][a-z0-9-]*$/', $this->sourceType) !== 1) {
|
|
throw new InvalidArgumentException('Preview source type must use lowercase letters, numbers, and hyphens');
|
|
}
|
|
if ($this->identity === '' || $this->signature === '') {
|
|
throw new InvalidArgumentException('Preview sources require an identity and content signature');
|
|
}
|
|
if ($this->size !== null && $this->size < 0) {
|
|
throw new InvalidArgumentException('Preview source size cannot be negative');
|
|
}
|
|
$this->mimeType = MimeType::normalize($mimeType);
|
|
}
|
|
|
|
public function open(): BinaryResource
|
|
{
|
|
$resource = ($this->resourceFactory)();
|
|
if (!$resource instanceof BinaryResource) {
|
|
throw new PreviewGenerationException('Preview source factory did not return a BinaryResource');
|
|
}
|
|
return $resource;
|
|
}
|
|
}
|