Accept
in package
implements
RuleInterface
Accept validation rule.
Validates that a file has an acceptable MIME type or extension.
Table of Contents
Interfaces
- RuleInterface
- Interface for validation rules.
Constants
- EXTENSION_TO_MIME : mixed = [ // Images 'jpg' => ['image/jpeg'], 'jpeg' =>...
- Common MIME type mappings.
Methods
- validate() : bool
- Validate that a file has an acceptable MIME type or extension.
- matchesExtension() : bool
- Check if a file extension matches the accept list.
- matchesMimeType() : bool
- Check if a MIME type matches the accept list.
- parseAcceptParam() : array<string|int, mixed>
- Parse accept parameter into a list of allowed MIME types.
Constants
EXTENSION_TO_MIME
Common MIME type mappings.
private
mixed
EXTENSION_TO_MIME
= [
// Images
'jpg' => ['image/jpeg'],
'jpeg' => ['image/jpeg'],
'png' => ['image/png'],
'gif' => ['image/gif'],
'webp' => ['image/webp'],
'svg' => ['image/svg+xml'],
'bmp' => ['image/bmp'],
'ico' => ['image/x-icon', 'image/vnd.microsoft.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'],
'txt' => ['text/plain'],
'csv' => ['text/csv', 'application/csv'],
// Audio
'mp3' => ['audio/mpeg', 'audio/mp3'],
'wav' => ['audio/wav', 'audio/x-wav'],
'ogg' => ['audio/ogg'],
'flac' => ['audio/flac'],
// Video
'mp4' => ['video/mp4'],
'webm' => ['video/webm'],
'avi' => ['video/x-msvideo'],
'mov' => ['video/quicktime'],
'mkv' => ['video/x-matroska'],
// Archives
'zip' => ['application/zip', 'application/x-zip-compressed'],
'rar' => ['application/x-rar-compressed', 'application/vnd.rar'],
'tar' => ['application/x-tar'],
'gz' => ['application/gzip'],
'7z' => ['application/x-7z-compressed'],
// Code
'json' => ['application/json'],
'xml' => ['application/xml', 'text/xml'],
'html' => ['text/html'],
'css' => ['text/css'],
'js' => ['application/javascript', 'text/javascript'],
]
Methods
validate()
Validate that a file has an acceptable MIME type or extension.
public
validate(mixed $value, mixed $param, array<string|int, mixed> $allData, FieldPath $path) : bool
Parameters
- $value : mixed
-
The value to validate
- $param : mixed
-
The rule parameter (e.g., min length, pattern, etc.)
- $allData : array<string|int, mixed>
-
All form data (for cross-field validation)
- $path : FieldPath
-
The data path of the value with the positions of its row keys
Return values
bool —True if valid, false otherwise
matchesExtension()
Check if a file extension matches the accept list.
private
matchesExtension(string $filename, array<string|int, mixed> $acceptList) : bool
A filename carries no MIME header, so the extension is mapped to its MIME type(s) via EXTENSION_TO_MIME and those are matched against MIME entries, including wildcards (image slash star, star slash star). Direct extension entries (.jpg) are also matched. Without this mapping a filename such as "image1.jpg" would never satisfy accept image slash star.
Parameters
- $filename : string
- $acceptList : array<string|int, mixed>
Return values
boolmatchesMimeType()
Check if a MIME type matches the accept list.
private
matchesMimeType(string $mimeType, array<string|int, mixed> $acceptList) : bool
Parameters
- $mimeType : string
- $acceptList : array<string|int, mixed>
Return values
boolparseAcceptParam()
Parse accept parameter into a list of allowed MIME types.
private
parseAcceptParam(mixed $param) : array<string|int, mixed>
Parameters
- $param : mixed