Parser.php in CSV Importer 8
File
src/Parser.php
View source
<?php
namespace Drupal\csv_importer;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class Parser implements ParserInterface {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
public function getCsvById(int $id, string $delimiter) {
$entity = $this
->getCsvEntity($id);
$return = [];
if (($csv = fopen($entity->uri
->getString(), 'r')) !== FALSE) {
while (($row = fgetcsv($csv, 0, $delimiter)) !== FALSE) {
$return[] = $row;
}
fclose($csv);
}
return $return;
}
public function getCsvFieldsById(int $id) {
$csv = $this
->getCsvById($id);
if ($csv && is_array($csv)) {
return $csv[0];
}
return NULL;
}
public function getCsvEntity(int $id) {
if ($id) {
return $this->entityTypeManager
->getStorage('file')
->load($id);
}
return NULL;
}
}