You are here

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

Expanded class hierarchy of CsvMimeDetector

File

src/Plugin/MimeDetector/CsvMimeDetector.php, line 16

Namespace

Drupal\mimedetect\Plugin\MimeDetector
View 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

Namesort descending Modifiers Type Description Overrides
CsvMimeDetector::detect public function Try MIME detection on a given file. Overrides MimeDetectorInterface::detect
MimeDetectorBase::description public function Provide a description of the detector. Overrides MimeDetectorInterface::description
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92