class CsvMimeDetector in MimeDetect 8
Provides a CSV MIME detector.
Plugin annotation
@MimeDetector(
id = "csv_mimedetector",
description = @Translation("Comma separated value (CSV) MIME type detector."),
filename_extensions = {"csv"}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\mimedetect\MimeDetectorBase implements MimeDetectorInterface
- class \Drupal\mimedetect\Plugin\MimeDetector\CsvMimeDetector
- class \Drupal\mimedetect\MimeDetectorBase implements MimeDetectorInterface
Expanded class hierarchy of CsvMimeDetector
File
- src/
Plugin/ MimeDetector/ CsvMimeDetector.php, line 16
Namespace
Drupal\mimedetect\Plugin\MimeDetectorView source
class CsvMimeDetector extends MimeDetectorBase {
/**
* {@inheritdoc}
*/
public function detect($path) {
if (($handler = @fopen($path, 'r')) !== FALSE) {
// Check first line, it must be printable text.
$first_line = fgets($handler, 16384);
if ($first_line && strlen($first_line) && ctype_print(str_replace([
"\n",
"\r",
], '', $first_line))) {
rewind($handler);
// Read the first two lines, they must have the same number of columns.
$columns = [];
while (($data = fgetcsv($handler)) !== FALSE && count($columns) < 2) {
$columns[] = count($data);
}
if (count($columns) == 1 || $columns[0] == $columns[1]) {
return 'text/csv';
}
}
}
return NULL;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CsvMimeDetector:: |
public | function |
Try MIME detection on a given file. Overrides MimeDetectorInterface:: |
|
MimeDetectorBase:: |
public | function |
Provide a description of the detector. Overrides MimeDetectorInterface:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |