Document_Parser
Dispatches file parsing to a format-specific parser.
Tags
Table of Contents
- VERSION = '1.0.0'
- Parser version used for cache invalidation.
- $extension_mime_map : array<string, string>
- Extension to MIME map for fallback support.
- $parsers : array<string, string>
- MIME type to parser class map.
- get_supported_extensions() : array<int, string>
- Get supported file extensions.
- get_supported_mime_types() : array<int, string>
- Get supported MIME types.
- get_version() : string
- Get parser version for cache invalidation.
- is_supported() : bool
- Check if the MIME type is supported.
- parse() : string
- Parse the file and return extracted plain text.
- resolve_supported_mime() : string
- Resolve a supported MIME type using MIME and extension fallback.
- get_extension_mime_map() : array<string, string>
- Get extension fallback MIME map.
- get_parsers() : array<string, string>
- Get the parser map.
- normalize_mime_type() : string
- Normalize MIME string for comparisons.
Constants
VERSION
Parser version used for cache invalidation.
public
mixed
VERSION
= '1.0.0'
Tags
Properties
$extension_mime_map
Extension to MIME map for fallback support.
private
static array<string, string>
$extension_mime_map
= [
'pdf' => 'application/pdf',
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'docm' => 'application/vnd.ms-word.document.macroenabled.12',
'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
'dotm' => 'application/vnd.ms-word.template.macroenabled.12',
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
// xlsb is intentionally excluded (binary spreadsheet format).
'xlsm' => 'application/vnd.ms-excel.sheet.macroenabled.12',
'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
'xltm' => 'application/vnd.ms-excel.template.macroenabled.12',
'xlam' => 'application/vnd.ms-excel.addin.macroenabled.12',
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'pptm' => 'application/vnd.ms-powerpoint.presentation.macroenabled.12',
'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroenabled.12',
'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
'potm' => 'application/vnd.ms-powerpoint.template.macroenabled.12',
'ppam' => 'application/vnd.ms-powerpoint.addin.macroenabled.12',
'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
'sldm' => 'application/vnd.ms-powerpoint.slide.macroenabled.12',
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
'odp' => 'application/vnd.oasis.opendocument.presentation',
'odg' => 'application/vnd.oasis.opendocument.graphics',
'odc' => 'application/vnd.oasis.opendocument.chart',
'odb' => 'application/vnd.oasis.opendocument.database',
'odf' => 'application/vnd.oasis.opendocument.formula',
]
Tags
$parsers
MIME type to parser class map.
private
static array<string, string>
$parsers
= [
'application/pdf' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Pdf_Parser::class,
'application/x-pdf' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Pdf_Parser::class,
'application/acrobat' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Pdf_Parser::class,
'applications/vnd.pdf' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Pdf_Parser::class,
'text/pdf' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Pdf_Parser::class,
'text/x-pdf' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Pdf_Parser::class,
// Office Open XML and supported macro/template variants.
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Office_Parser::class,
'application/vnd.ms-word.document.macroenabled.12' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Office_Parser::class,
'application/vnd.openxmlformats-officedocument.wordprocessingml.template' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Office_Parser::class,
'application/vnd.ms-word.template.macroenabled.12' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Office_Parser::class,
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Office_Parser::class,
'application/vnd.ms-excel.sheet.macroenabled.12' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Office_Parser::class,
'application/vnd.openxmlformats-officedocument.spreadsheetml.template' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Office_Parser::class,
'application/vnd.ms-excel.template.macroenabled.12' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Office_Parser::class,
'application/vnd.ms-excel.addin.macroenabled.12' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Office_Parser::class,
'application/vnd.openxmlformats-officedocument.presentationml.presentation' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Office_Parser::class,
'application/vnd.ms-powerpoint.presentation.macroenabled.12' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Office_Parser::class,
'application/vnd.openxmlformats-officedocument.presentationml.slideshow' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Office_Parser::class,
'application/vnd.ms-powerpoint.slideshow.macroenabled.12' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Office_Parser::class,
'application/vnd.openxmlformats-officedocument.presentationml.template' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Office_Parser::class,
'application/vnd.ms-powerpoint.template.macroenabled.12' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Office_Parser::class,
'application/vnd.ms-powerpoint.addin.macroenabled.12' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Office_Parser::class,
'application/vnd.openxmlformats-officedocument.presentationml.slide' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Office_Parser::class,
'application/vnd.ms-powerpoint.slide.macroenabled.12' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Office_Parser::class,
// OpenDocument.
'application/vnd.oasis.opendocument.text' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Odf_Parser::class,
'application/vnd.oasis.opendocument.spreadsheet' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Odf_Parser::class,
'application/vnd.oasis.opendocument.presentation' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Odf_Parser::class,
'application/vnd.oasis.opendocument.graphics' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Odf_Parser::class,
'application/vnd.oasis.opendocument.chart' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Odf_Parser::class,
'application/vnd.oasis.opendocument.database' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Odf_Parser::class,
'application/vnd.oasis.opendocument.formula' => \Barn2\Plugin\Document_Library_Advanced\File_Content\Parsers\Odf_Parser::class,
]
Tags
Methods
get_supported_extensions()
Get supported file extensions.
public
static get_supported_extensions() : array<int, string>
Tags
Return values
array<int, string> —get_supported_mime_types()
Get supported MIME types.
public
static get_supported_mime_types() : array<int, string>
Tags
Return values
array<int, string> —get_version()
Get parser version for cache invalidation.
public
static get_version() : string
Tags
Return values
string —is_supported()
Check if the MIME type is supported.
public
static is_supported(string $mime_type) : bool
Parameters
- $mime_type : string
-
File MIME type.
Tags
Return values
bool —parse()
Parse the file and return extracted plain text.
public
static parse(string $file_path, string $mime_type) : string
Parameters
- $file_path : string
-
Absolute file path.
- $mime_type : string
-
File MIME type.
Tags
Return values
string —resolve_supported_mime()
Resolve a supported MIME type using MIME and extension fallback.
public
static resolve_supported_mime(string $primary_mime, string $file_path[, string $secondary_mime = '' ]) : string
Parameters
- $primary_mime : string
-
MIME from attachment metadata.
- $file_path : string
-
Absolute file path.
- $secondary_mime : string = ''
-
Optional MIME from fallback detection.
Tags
Return values
string —Empty string when unsupported.
get_extension_mime_map()
Get extension fallback MIME map.
private
static get_extension_mime_map() : array<string, string>
Tags
Return values
array<string, string> —get_parsers()
Get the parser map.
private
static get_parsers() : array<string, string>
Tags
Return values
array<string, string> —normalize_mime_type()
Normalize MIME string for comparisons.
private
static normalize_mime_type(string $mime_type) : string
Parameters
- $mime_type : string
-
Raw MIME value.