firefly-iii/app/Export/Exporter/CsvExporter.php

84 lines
1.8 KiB
PHP
Raw Normal View History

2016-02-04 10:16:16 -06:00
<?php
2016-02-05 05:08:25 -06:00
declare(strict_types = 1);
2016-02-04 10:16:16 -06:00
/**
* CsvExporter.php
* Copyright (C) 2016 Sander Dorigo
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Export\Exporter;
use FireflyIII\Export\Entry;
use FireflyIII\Models\ExportJob;
use League\Csv\Writer;
use SplFileObject;
/**
* Class CsvExporter
*
* @package FireflyIII\Export\Exporter
*/
class CsvExporter extends BasicExporter implements ExporterInterface
{
/** @var string */
private $fileName;
/** @var resource */
private $handler;
/**
* CsvExporter constructor.
2016-02-05 08:41:40 -06:00
*
* @param ExportJob $job
2016-02-04 10:16:16 -06:00
*/
public function __construct(ExportJob $job)
{
parent::__construct($job);
}
/**
* @return string
*/
public function getFileName()
{
return $this->fileName;
}
/**
*
*/
public function run()
{
// create temporary file:
$this->tempFile();
// create CSV writer:
$writer = Writer::createFromPath(new SplFileObject($this->fileName, 'a+'), 'w');
//the $writer object open mode will be 'w'!!
// all rows:
$rows = [];
// add header:
$first = $this->getEntries()->first();
$rows[] = array_keys(get_object_vars($first));
// then the rest:
/** @var Entry $entry */
foreach ($this->getEntries() as $entry) {
$rows[] = array_values(get_object_vars($entry));
}
$writer->insertAll($rows);
}
private function tempFile()
{
$fileName = $this->job->key . '-records.csv';
$this->fileName = storage_path('export') . DIRECTORY_SEPARATOR . $fileName;
$this->handler = fopen($this->fileName, 'w');
}
2016-02-10 09:01:18 -06:00
}