CsvImporter
in package
implements
ImporterInterface
Importer for CSV/TSV dictionary files.
Supports configurable column mapping and delimiters.
Tags
Table of Contents
Interfaces
- ImporterInterface
- Interface for dictionary importers.
Constants
- DEFAULT_COLUMN_MAP = ['term' => 0, 'definition' => 1, 'reading' => null, 'pos' => null]
- Default column mapping.
Methods
- canImport() : bool
- Validate that a file can be imported.
- detectDelimiter() : string
- Detect the delimiter used in a CSV file.
- detectHeaders() : array<string|int, string>
- Detect column headers from the first row.
- getSupportedExtensions() : array<string|int, string>
- Get the supported file extensions for this importer.
- parse() : iterable<string|int, array{term: string, definition: string, reading?: ?string, pos?: ?string}>
- Parse a dictionary file and yield entries.
- preview() : array<string|int, array{term: string, definition: string, reading?: ?string, pos?: ?string}>
- Get a preview of the first N entries.
- suggestColumnMap() : array<string, int|null>
- Suggest column mapping based on header names.
- convertEncoding() : string
- Convert string encoding to UTF-8.
- mapRowToEntry() : array{term: string, definition: string, reading?: ?string, pos?: ?string}|null
- Map a CSV row to an entry array.
- validateFile() : void
- Validate that the file exists and is readable.
Constants
DEFAULT_COLUMN_MAP
Default column mapping.
private
mixed
DEFAULT_COLUMN_MAP
= ['term' => 0, 'definition' => 1, 'reading' => null, 'pos' => null]
Methods
canImport()
Validate that a file can be imported.
public
canImport(string $filePath) : bool
Parameters
- $filePath : string
-
Path to the file
Return values
bool —True if the file can be imported
detectDelimiter()
Detect the delimiter used in a CSV file.
public
detectDelimiter(string $filePath) : string
Parameters
- $filePath : string
-
Path to the file
Return values
string —Detected delimiter
detectHeaders()
Detect column headers from the first row.
public
detectHeaders(string $filePath[, string $delimiter = ',' ]) : array<string|int, string>
Parameters
- $filePath : string
-
Path to the file
- $delimiter : string = ','
-
Delimiter to use
Return values
array<string|int, string> —Column headers
getSupportedExtensions()
Get the supported file extensions for this importer.
public
getSupportedExtensions() : array<string|int, string>
Return values
array<string|int, string>parse()
Parse a dictionary file and yield entries.
public
parse(string $filePath[, array<string|int, mixed> $options = [] ]) : iterable<string|int, array{term: string, definition: string, reading?: ?string, pos?: ?string}>
Parameters
- $filePath : string
-
Path to the dictionary file
- $options : array<string|int, mixed> = []
-
Import options (format-specific)
Return values
iterable<string|int, array{term: string, definition: string, reading?: ?string, pos?: ?string}>preview()
Get a preview of the first N entries.
public
preview(string $filePath[, int $limit = 10 ][, array<string|int, mixed> $options = [] ]) : array<string|int, array{term: string, definition: string, reading?: ?string, pos?: ?string}>
Parameters
- $filePath : string
-
Path to the dictionary file
- $limit : int = 10
-
Number of entries to preview
- $options : array<string|int, mixed> = []
-
Import options
Return values
array<string|int, array{term: string, definition: string, reading?: ?string, pos?: ?string}>suggestColumnMap()
Suggest column mapping based on header names.
public
suggestColumnMap(array<string|int, string> $headers) : array<string, int|null>
Parameters
- $headers : array<string|int, string>
-
Column headers
Return values
array<string, int|null> —Suggested column mapping
convertEncoding()
Convert string encoding to UTF-8.
private
convertEncoding(string $string, string $encoding) : string
Parameters
- $string : string
-
String to convert
- $encoding : string
-
Source encoding
Return values
string —UTF-8 encoded string
mapRowToEntry()
Map a CSV row to an entry array.
private
mapRowToEntry(array<string|int, string> $row, array<string, mixed> $columnMap, string $encoding) : array{term: string, definition: string, reading?: ?string, pos?: ?string}|null
Parameters
- $row : array<string|int, string>
-
CSV row data
- $columnMap : array<string, mixed>
-
Column mapping
- $encoding : string
-
Source encoding
Return values
array{term: string, definition: string, reading?: ?string, pos?: ?string}|nullvalidateFile()
Validate that the file exists and is readable.
private
validateFile(string $filePath) : void
Parameters
- $filePath : string
-
Path to the file