You are here

public function TaskManager::toCsv in General Data Protection Regulation 3.0.x

Same name and namespace in other branches
  1. 8.2 modules/gdpr_tasks/src/TaskManager.php \Drupal\gdpr_tasks\TaskManager::toCsv()
  2. 8 modules/gdpr_tasks/src/TaskManager.php \Drupal\gdpr_tasks\TaskManager::toCsv()

Writes array data to a csv file.

Parameters

array $data: The data to be stored in csv.

string $dirname: The local path or stream wrapper for destination directory.

Return value

string The uri path of the created file.

File

modules/gdpr_tasks/src/TaskManager.php, line 110

Class

TaskManager
Defines a helper class for stuff related to views data.

Namespace

Drupal\gdpr_tasks

Code

public function toCsv(array $data, $dirname = 'private://') {

  // Prepare destination.
  $this->fileSystem
    ->prepareDirectory($dirname, FileSystemInterface::CREATE_DIRECTORY);

  // Generate a file entity.
  $random = new Random();
  $destination = $dirname . '/' . $random
    ->name(10, TRUE) . '.csv';

  // Update csv with actual data.
  $fp = fopen($destination, 'wb');
  foreach ($data as $line) {
    fputcsv($fp, $line);
  }
  fclose($fp);
  return $destination;
}