Initial commit: AuroraX
This commit is contained in:
@@ -0,0 +1,400 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace WordPress\AiClient\Files\DTO;
|
||||
|
||||
use WordPress\AiClient\Common\AbstractDataTransferObject;
|
||||
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
|
||||
use WordPress\AiClient\Common\Exception\RuntimeException;
|
||||
use WordPress\AiClient\Files\Enums\FileTypeEnum;
|
||||
use WordPress\AiClient\Files\ValueObjects\MimeType;
|
||||
/**
|
||||
* Represents a file in the AI client.
|
||||
*
|
||||
* This DTO automatically detects whether a file is a URL, base64 data, or local file path
|
||||
* and handles them appropriately.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @phpstan-type FileArrayShape array{
|
||||
* fileType: string,
|
||||
* url?: string,
|
||||
* mimeType: string,
|
||||
* base64Data?: string
|
||||
* }
|
||||
*
|
||||
* @extends AbstractDataTransferObject<FileArrayShape>
|
||||
*/
|
||||
class File extends AbstractDataTransferObject
|
||||
{
|
||||
public const KEY_FILE_TYPE = 'fileType';
|
||||
public const KEY_MIME_TYPE = 'mimeType';
|
||||
public const KEY_URL = 'url';
|
||||
public const KEY_BASE64_DATA = 'base64Data';
|
||||
/**
|
||||
* @var MimeType The MIME type of the file.
|
||||
*/
|
||||
private MimeType $mimeType;
|
||||
/**
|
||||
* @var FileTypeEnum The type of file storage.
|
||||
*/
|
||||
private FileTypeEnum $fileType;
|
||||
/**
|
||||
* @var string|null The URL for remote files.
|
||||
*/
|
||||
private ?string $url = null;
|
||||
/**
|
||||
* @var string|null The base64 data for inline files.
|
||||
*/
|
||||
private ?string $base64Data = null;
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param string $file The file string (URL, base64 data, or local path).
|
||||
* @param string|null $mimeType The MIME type of the file (optional).
|
||||
* @throws InvalidArgumentException If the file format is invalid or MIME type cannot be determined.
|
||||
*/
|
||||
public function __construct(string $file, ?string $mimeType = null)
|
||||
{
|
||||
// Detect and process the file type (will set MIME type if possible)
|
||||
$this->detectAndProcessFile($file, $mimeType);
|
||||
}
|
||||
/**
|
||||
* Detects the file type and processes it accordingly.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param string $file The file string to process.
|
||||
* @param string|null $providedMimeType The explicitly provided MIME type.
|
||||
* @throws InvalidArgumentException If the file format is invalid or MIME type cannot be determined.
|
||||
*/
|
||||
private function detectAndProcessFile(string $file, ?string $providedMimeType): void
|
||||
{
|
||||
// Check if it's a URL
|
||||
if ($this->isUrl($file)) {
|
||||
$this->fileType = FileTypeEnum::remote();
|
||||
$this->url = $file;
|
||||
$this->mimeType = $this->determineMimeType($providedMimeType, null, $file);
|
||||
return;
|
||||
}
|
||||
// Data URI pattern.
|
||||
$dataUriPattern = '/^data:(?:([a-zA-Z0-9][a-zA-Z0-9!#$&\-\^_+.]*\/[a-zA-Z0-9][a-zA-Z0-9!#$&\-\^_+.]*' . '(?:;[a-zA-Z0-9\-]+=[a-zA-Z0-9\-]+)*)?;)?base64,([A-Za-z0-9+\/]*={0,2})$/';
|
||||
// Check if it's a data URI.
|
||||
if (preg_match($dataUriPattern, $file, $matches)) {
|
||||
$this->fileType = FileTypeEnum::inline();
|
||||
$this->base64Data = $matches[2];
|
||||
// Extract just the base64 data
|
||||
$extractedMimeType = empty($matches[1]) ? null : $matches[1];
|
||||
$this->mimeType = $this->determineMimeType($providedMimeType, $extractedMimeType, null);
|
||||
return;
|
||||
}
|
||||
// Check if it's a local file path (before base64 check)
|
||||
if (file_exists($file) && is_file($file)) {
|
||||
$this->fileType = FileTypeEnum::inline();
|
||||
$this->base64Data = $this->convertFileToBase64($file);
|
||||
$this->mimeType = $this->determineMimeType($providedMimeType, null, $file);
|
||||
return;
|
||||
}
|
||||
// Check if it's plain base64
|
||||
if (preg_match('/^[A-Za-z0-9+\/]*={0,2}$/', $file)) {
|
||||
if ($providedMimeType === null) {
|
||||
throw new InvalidArgumentException('MIME type is required when providing plain base64 data without data URI format.');
|
||||
}
|
||||
$this->fileType = FileTypeEnum::inline();
|
||||
$this->base64Data = $file;
|
||||
$this->mimeType = new MimeType($providedMimeType);
|
||||
return;
|
||||
}
|
||||
throw new InvalidArgumentException('Invalid file provided. Expected URL, base64 data, or valid local file path.');
|
||||
}
|
||||
/**
|
||||
* Checks if a string is a valid URL.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param string $string The string to check.
|
||||
* @return bool True if the string is a URL.
|
||||
*/
|
||||
private function isUrl(string $string): bool
|
||||
{
|
||||
return filter_var($string, \FILTER_VALIDATE_URL) !== \false && preg_match('/^https?:\/\//i', $string);
|
||||
}
|
||||
/**
|
||||
* Converts a local file to base64.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param string $filePath The path to the local file.
|
||||
* @return string The base64-encoded file data.
|
||||
* @throws RuntimeException If the file cannot be read.
|
||||
*/
|
||||
private function convertFileToBase64(string $filePath): string
|
||||
{
|
||||
$fileContent = @file_get_contents($filePath);
|
||||
if ($fileContent === \false) {
|
||||
throw new RuntimeException(sprintf('Unable to read file: %s', $filePath));
|
||||
}
|
||||
return base64_encode($fileContent);
|
||||
}
|
||||
/**
|
||||
* Gets the file type.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return FileTypeEnum The file type.
|
||||
*/
|
||||
public function getFileType(): FileTypeEnum
|
||||
{
|
||||
return $this->fileType;
|
||||
}
|
||||
/**
|
||||
* Checks if the file is an inline file.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return bool True if the file is inline (base64/data URI).
|
||||
*/
|
||||
public function isInline(): bool
|
||||
{
|
||||
return $this->fileType->isInline();
|
||||
}
|
||||
/**
|
||||
* Checks if the file is a remote file.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return bool True if the file is remote (URL).
|
||||
*/
|
||||
public function isRemote(): bool
|
||||
{
|
||||
return $this->fileType->isRemote();
|
||||
}
|
||||
/**
|
||||
* Gets the URL for remote files.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return string|null The URL, or null if not a remote file.
|
||||
*/
|
||||
public function getUrl(): ?string
|
||||
{
|
||||
return $this->url;
|
||||
}
|
||||
/**
|
||||
* Gets the base64-encoded data for inline files.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return string|null The plain base64-encoded data (without data URI prefix), or null if not an inline file.
|
||||
*/
|
||||
public function getBase64Data(): ?string
|
||||
{
|
||||
return $this->base64Data;
|
||||
}
|
||||
/**
|
||||
* Gets the data as a data URI for inline files.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return string|null The data URI in format: data:[mimeType];base64,[data], or null if not an inline file.
|
||||
*/
|
||||
public function getDataUri(): ?string
|
||||
{
|
||||
if ($this->base64Data === null) {
|
||||
return null;
|
||||
}
|
||||
return sprintf('data:%s;base64,%s', $this->getMimeType(), $this->base64Data);
|
||||
}
|
||||
/**
|
||||
* Gets the MIME type of the file as a string.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return string The MIME type string value.
|
||||
*/
|
||||
public function getMimeType(): string
|
||||
{
|
||||
return (string) $this->mimeType;
|
||||
}
|
||||
/**
|
||||
* Gets the MIME type object.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return MimeType The MIME type object.
|
||||
*/
|
||||
public function getMimeTypeObject(): MimeType
|
||||
{
|
||||
return $this->mimeType;
|
||||
}
|
||||
/**
|
||||
* Checks if the file is a video.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return bool True if the file is a video.
|
||||
*/
|
||||
public function isVideo(): bool
|
||||
{
|
||||
return $this->mimeType->isVideo();
|
||||
}
|
||||
/**
|
||||
* Checks if the file is an image.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return bool True if the file is an image.
|
||||
*/
|
||||
public function isImage(): bool
|
||||
{
|
||||
return $this->mimeType->isImage();
|
||||
}
|
||||
/**
|
||||
* Checks if the file is audio.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return bool True if the file is audio.
|
||||
*/
|
||||
public function isAudio(): bool
|
||||
{
|
||||
return $this->mimeType->isAudio();
|
||||
}
|
||||
/**
|
||||
* Checks if the file is text.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return bool True if the file is text.
|
||||
*/
|
||||
public function isText(): bool
|
||||
{
|
||||
return $this->mimeType->isText();
|
||||
}
|
||||
/**
|
||||
* Checks if the file is a document.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return bool True if the file is a document.
|
||||
*/
|
||||
public function isDocument(): bool
|
||||
{
|
||||
return $this->mimeType->isDocument();
|
||||
}
|
||||
/**
|
||||
* Checks if the file is a specific MIME type.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param string $type The mime type to check (e.g. 'image', 'text', 'video', 'audio').
|
||||
*
|
||||
* @return bool True if the file is of the specified type.
|
||||
*/
|
||||
public function isMimeType(string $type): bool
|
||||
{
|
||||
return $this->mimeType->isType($type);
|
||||
}
|
||||
/**
|
||||
* Determines the MIME type from various sources.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param string|null $providedMimeType The explicitly provided MIME type.
|
||||
* @param string|null $extractedMimeType The MIME type extracted from data URI.
|
||||
* @param string|null $pathOrUrl The file path or URL to extract extension from.
|
||||
* @return MimeType The determined MIME type.
|
||||
* @throws InvalidArgumentException If MIME type cannot be determined.
|
||||
*/
|
||||
private function determineMimeType(?string $providedMimeType, ?string $extractedMimeType, ?string $pathOrUrl): MimeType
|
||||
{
|
||||
// Prefer explicitly provided MIME type
|
||||
if ($providedMimeType !== null) {
|
||||
return new MimeType($providedMimeType);
|
||||
}
|
||||
// Use extracted MIME type from data URI
|
||||
if ($extractedMimeType !== null) {
|
||||
return new MimeType($extractedMimeType);
|
||||
}
|
||||
// Try to determine from file extension
|
||||
if ($pathOrUrl !== null) {
|
||||
$parsedUrl = parse_url($pathOrUrl);
|
||||
$path = $parsedUrl['path'] ?? $pathOrUrl;
|
||||
// Remove query string and fragment if present
|
||||
$cleanPath = strtok($path, '?#');
|
||||
if ($cleanPath === \false) {
|
||||
$cleanPath = $path;
|
||||
}
|
||||
$extension = pathinfo($cleanPath, \PATHINFO_EXTENSION);
|
||||
if (!empty($extension)) {
|
||||
try {
|
||||
return MimeType::fromExtension($extension);
|
||||
} catch (InvalidArgumentException $e) {
|
||||
// Extension not recognized, continue to error
|
||||
unset($e);
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new InvalidArgumentException('Unable to determine MIME type. Please provide it explicitly.');
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public static function getJsonSchema(): array
|
||||
{
|
||||
return ['type' => 'object', 'oneOf' => [['properties' => [self::KEY_FILE_TYPE => ['type' => 'string', 'const' => FileTypeEnum::REMOTE, 'description' => 'The file type.'], self::KEY_MIME_TYPE => ['type' => 'string', 'description' => 'The MIME type of the file.', 'pattern' => '^[a-zA-Z0-9][a-zA-Z0-9!#$&\-\^_+.]*\/[a-zA-Z0-9]' . '[a-zA-Z0-9!#$&\-\^_+.]*$'], self::KEY_URL => ['type' => 'string', 'format' => 'uri', 'description' => 'The URL to the remote file.']], 'required' => [self::KEY_FILE_TYPE, self::KEY_MIME_TYPE, self::KEY_URL]], ['properties' => [self::KEY_FILE_TYPE => ['type' => 'string', 'const' => FileTypeEnum::INLINE, 'description' => 'The file type.'], self::KEY_MIME_TYPE => ['type' => 'string', 'description' => 'The MIME type of the file.', 'pattern' => '^[a-zA-Z0-9][a-zA-Z0-9!#$&\-\^_+.]*\/[a-zA-Z0-9]' . '[a-zA-Z0-9!#$&\-\^_+.]*$'], self::KEY_BASE64_DATA => ['type' => 'string', 'description' => 'The base64-encoded file data.']], 'required' => [self::KEY_FILE_TYPE, self::KEY_MIME_TYPE, self::KEY_BASE64_DATA]]]];
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return FileArrayShape
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
$data = [self::KEY_FILE_TYPE => $this->fileType->value, self::KEY_MIME_TYPE => $this->getMimeType()];
|
||||
if ($this->url !== null) {
|
||||
$data[self::KEY_URL] = $this->url;
|
||||
} elseif (!$this->fileType->isRemote() && $this->base64Data !== null) {
|
||||
$data[self::KEY_BASE64_DATA] = $this->base64Data;
|
||||
} else {
|
||||
throw new RuntimeException('File requires either url or base64Data. This should not be a possible condition.');
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public static function fromArray(array $array): self
|
||||
{
|
||||
static::validateFromArrayData($array, [self::KEY_FILE_TYPE]);
|
||||
// Check which properties are set to determine how to construct the File
|
||||
$mimeType = $array[self::KEY_MIME_TYPE] ?? null;
|
||||
if (isset($array[self::KEY_URL])) {
|
||||
return new self($array[self::KEY_URL], $mimeType);
|
||||
} elseif (isset($array[self::KEY_BASE64_DATA])) {
|
||||
return new self($array[self::KEY_BASE64_DATA], $mimeType);
|
||||
} else {
|
||||
throw new InvalidArgumentException('File requires either url or base64Data.');
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Performs a deep clone of the file.
|
||||
*
|
||||
* This method ensures that the MimeType value object is cloned to prevent
|
||||
* any shared references between the original and cloned file.
|
||||
*
|
||||
* @since 0.4.2
|
||||
*/
|
||||
public function __clone()
|
||||
{
|
||||
$this->mimeType = clone $this->mimeType;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace WordPress\AiClient\Files\Enums;
|
||||
|
||||
use WordPress\AiClient\Common\AbstractEnum;
|
||||
/**
|
||||
* Represents the type of file storage.
|
||||
*
|
||||
* @method static self inline() Returns the inline file type.
|
||||
* @method static self remote() Returns the remote file type.
|
||||
* @method bool isInline() Checks if this is an inline file type.
|
||||
* @method bool isRemote() Checks if this is a remote file type.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class FileTypeEnum extends AbstractEnum
|
||||
{
|
||||
/**
|
||||
* Inline file with base64-encoded data.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public const INLINE = 'inline';
|
||||
/**
|
||||
* Remote file referenced by URL.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public const REMOTE = 'remote';
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace WordPress\AiClient\Files\Enums;
|
||||
|
||||
use WordPress\AiClient\Common\AbstractEnum;
|
||||
/**
|
||||
* Represents the type of file storage.
|
||||
*
|
||||
* @method static self square() Returns the square orientation
|
||||
* @method static self landscape() Returns the landscape orientation.
|
||||
* @method static self portrait() Returns the portrait orientation.
|
||||
* @method bool isSquare() Checks if this is an square orientation
|
||||
* @method bool isLandscape() Checks if this is a landscape orientation.
|
||||
* @method bool isPortrait() Checks if this is a portrait orientation.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class MediaOrientationEnum extends AbstractEnum
|
||||
{
|
||||
/**
|
||||
* Square orientation.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public const SQUARE = 'square';
|
||||
/**
|
||||
* Landscape orientation.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public const LANDSCAPE = 'landscape';
|
||||
/**
|
||||
* Portrait orientation.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public const PORTRAIT = 'portrait';
|
||||
}
|
||||
@@ -0,0 +1,255 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace WordPress\AiClient\Files\ValueObjects;
|
||||
|
||||
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
|
||||
/**
|
||||
* Value object representing a MIME type.
|
||||
*
|
||||
* This immutable value object encapsulates MIME type validation and
|
||||
* provides convenient methods for checking MIME type categories.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
final class MimeType
|
||||
{
|
||||
/**
|
||||
* @var string The MIME type value.
|
||||
*/
|
||||
private string $value;
|
||||
/**
|
||||
* Common MIME type mappings for file extensions.
|
||||
*
|
||||
* @var array<string, string>
|
||||
*/
|
||||
private static array $extensionMap = [
|
||||
// Text
|
||||
'txt' => 'text/plain',
|
||||
'html' => 'text/html',
|
||||
'htm' => 'text/html',
|
||||
'css' => 'text/css',
|
||||
'js' => 'application/javascript',
|
||||
'json' => 'application/json',
|
||||
'xml' => 'application/xml',
|
||||
'csv' => 'text/csv',
|
||||
'md' => 'text/markdown',
|
||||
// Images
|
||||
'jpg' => 'image/jpeg',
|
||||
'jpeg' => 'image/jpeg',
|
||||
'png' => 'image/png',
|
||||
'gif' => 'image/gif',
|
||||
'bmp' => 'image/bmp',
|
||||
'webp' => 'image/webp',
|
||||
'svg' => 'image/svg+xml',
|
||||
'ico' => 'image/x-icon',
|
||||
// Documents
|
||||
'pdf' => 'application/pdf',
|
||||
'doc' => 'application/msword',
|
||||
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
||||
'xls' => 'application/vnd.ms-excel',
|
||||
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
'ppt' => 'application/vnd.ms-powerpoint',
|
||||
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
||||
'odt' => 'application/vnd.oasis.opendocument.text',
|
||||
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
|
||||
// Archives
|
||||
'zip' => 'application/zip',
|
||||
'tar' => 'application/x-tar',
|
||||
'gz' => 'application/gzip',
|
||||
'rar' => 'application/x-rar-compressed',
|
||||
'7z' => 'application/x-7z-compressed',
|
||||
// Audio
|
||||
'mp3' => 'audio/mpeg',
|
||||
'wav' => 'audio/wav',
|
||||
'ogg' => 'audio/ogg',
|
||||
'flac' => 'audio/flac',
|
||||
'm4a' => 'audio/m4a',
|
||||
'aac' => 'audio/aac',
|
||||
// Video
|
||||
'mp4' => 'video/mp4',
|
||||
'avi' => 'video/x-msvideo',
|
||||
'mov' => 'video/quicktime',
|
||||
'wmv' => 'video/x-ms-wmv',
|
||||
'flv' => 'video/x-flv',
|
||||
'webm' => 'video/webm',
|
||||
'mkv' => 'video/x-matroska',
|
||||
// Fonts
|
||||
'ttf' => 'font/ttf',
|
||||
'otf' => 'font/otf',
|
||||
'woff' => 'font/woff',
|
||||
'woff2' => 'font/woff2',
|
||||
// Other
|
||||
'php' => 'application/x-httpd-php',
|
||||
'sh' => 'application/x-sh',
|
||||
'exe' => 'application/x-msdownload',
|
||||
];
|
||||
/**
|
||||
* Document MIME types.
|
||||
*
|
||||
* @var array<string>
|
||||
*/
|
||||
private static array $documentTypes = ['application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'application/vnd.oasis.opendocument.text', 'application/vnd.oasis.opendocument.spreadsheet'];
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param string $value The MIME type value.
|
||||
* @throws InvalidArgumentException If the MIME type is invalid.
|
||||
*/
|
||||
public function __construct(string $value)
|
||||
{
|
||||
if (!self::isValid($value)) {
|
||||
throw new InvalidArgumentException(sprintf('Invalid MIME type: %s', $value));
|
||||
}
|
||||
$this->value = strtolower($value);
|
||||
}
|
||||
/**
|
||||
* Gets the primary known file extension for this MIME type.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return string The file extension (without the dot).
|
||||
* @throws InvalidArgumentException If no known extension exists for this MIME type.
|
||||
*/
|
||||
public function toExtension(): string
|
||||
{
|
||||
// Reverse lookup for the MIME type to find the extension.
|
||||
$extension = array_search($this->value, self::$extensionMap, \true);
|
||||
if ($extension === \false) {
|
||||
throw new InvalidArgumentException(sprintf('No known extension for MIME type: %s', $this->value));
|
||||
}
|
||||
return $extension;
|
||||
}
|
||||
/**
|
||||
* Creates a MimeType from a file extension.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param string $extension The file extension (without the dot).
|
||||
* @return self The MimeType instance.
|
||||
* @throws InvalidArgumentException If the extension is not recognized.
|
||||
*/
|
||||
public static function fromExtension(string $extension): self
|
||||
{
|
||||
$extension = strtolower($extension);
|
||||
if (!isset(self::$extensionMap[$extension])) {
|
||||
throw new InvalidArgumentException(sprintf('Unknown file extension: %s', $extension));
|
||||
}
|
||||
return new self(self::$extensionMap[$extension]);
|
||||
}
|
||||
/**
|
||||
* Checks if a MIME type string is valid.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param string $mimeType The MIME type to validate.
|
||||
* @return bool True if valid.
|
||||
*/
|
||||
public static function isValid(string $mimeType): bool
|
||||
{
|
||||
// Basic MIME type validation: type/subtype
|
||||
return (bool) preg_match('/^[a-zA-Z0-9][a-zA-Z0-9!#$&\-\^_+.]*\/[a-zA-Z0-9][a-zA-Z0-9!#$&\-\^_+.]*$/', $mimeType);
|
||||
}
|
||||
/**
|
||||
* Checks if this MIME type is a specific type.
|
||||
*
|
||||
* This method returns true when the stored MIME type begins with the
|
||||
* given prefix. For example, `"audio"` matches `"audio/mpeg"`.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param string $mimeType The MIME type prefix to check (e.g., "audio", "image").
|
||||
* @return bool True if this MIME type is of the specified type.
|
||||
*/
|
||||
public function isType(string $mimeType): bool
|
||||
{
|
||||
return str_starts_with($this->value, strtolower($mimeType) . '/');
|
||||
}
|
||||
/**
|
||||
* Checks if this is an image MIME type.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return bool True if this is an image type.
|
||||
*/
|
||||
public function isImage(): bool
|
||||
{
|
||||
return $this->isType('image');
|
||||
}
|
||||
/**
|
||||
* Checks if this is an audio MIME type.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return bool True if this is an audio type.
|
||||
*/
|
||||
public function isAudio(): bool
|
||||
{
|
||||
return $this->isType('audio');
|
||||
}
|
||||
/**
|
||||
* Checks if this is a video MIME type.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return bool True if this is a video type.
|
||||
*/
|
||||
public function isVideo(): bool
|
||||
{
|
||||
return $this->isType('video');
|
||||
}
|
||||
/**
|
||||
* Checks if this is a text MIME type.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return bool True if this is a text type.
|
||||
*/
|
||||
public function isText(): bool
|
||||
{
|
||||
return $this->isType('text');
|
||||
}
|
||||
/**
|
||||
* Checks if this is a document MIME type.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return bool True if this is a document type.
|
||||
*/
|
||||
public function isDocument(): bool
|
||||
{
|
||||
return in_array($this->value, self::$documentTypes, \true);
|
||||
}
|
||||
/**
|
||||
* Checks if this MIME type equals another.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param self|string $other The other MIME type to compare.
|
||||
* @return bool True if equal.
|
||||
* @throws InvalidArgumentException If the other MIME type is invalid.
|
||||
*/
|
||||
public function equals($other): bool
|
||||
{
|
||||
if ($other instanceof self) {
|
||||
return $this->value === $other->value;
|
||||
}
|
||||
if (is_string($other)) {
|
||||
return $this->value === strtolower($other);
|
||||
}
|
||||
throw new InvalidArgumentException(sprintf('Invalid MIME type comparison: %s', gettype($other)));
|
||||
}
|
||||
/**
|
||||
* Gets the string representation of the MIME type.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return string The MIME type value.
|
||||
*/
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user