2016-02-04 17:16:16 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* CsvExporter.php
|
2016-04-01 16:44:46 +02:00
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
2016-02-04 17:16:16 +01:00
|
|
|
*
|
2016-10-05 06:52:15 +02:00
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
2016-02-04 17:16:16 +01:00
|
|
|
*/
|
|
|
|
|
2017-04-09 07:44:22 +02:00
|
|
|
declare(strict_types=1);
|
2016-05-20 12:27:31 +02:00
|
|
|
|
2016-02-04 17:16:16 +01:00
|
|
|
namespace FireflyIII\Export\Exporter;
|
|
|
|
|
2016-04-26 12:39:29 +02:00
|
|
|
use FireflyIII\Export\Entry\Entry;
|
2016-02-04 17:16:16 +01:00
|
|
|
use League\Csv\Writer;
|
|
|
|
use SplFileObject;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class CsvExporter
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Export\Exporter
|
|
|
|
*/
|
|
|
|
class CsvExporter extends BasicExporter implements ExporterInterface
|
|
|
|
{
|
|
|
|
/** @var string */
|
|
|
|
private $fileName;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CsvExporter constructor.
|
|
|
|
*/
|
2017-02-05 15:58:42 +01:00
|
|
|
public function __construct()
|
2016-02-04 17:16:16 +01:00
|
|
|
{
|
2017-02-05 15:58:42 +01:00
|
|
|
parent::__construct();
|
2016-02-04 17:16:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-04-06 16:37:28 +02:00
|
|
|
public function getFileName(): string
|
2016-02-04 17:16:16 +01:00
|
|
|
{
|
|
|
|
return $this->fileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-06 16:37:28 +02:00
|
|
|
* @return bool
|
2016-02-04 17:16:16 +01:00
|
|
|
*/
|
2016-04-06 16:37:28 +02:00
|
|
|
public function run(): bool
|
2016-02-04 17:16:16 +01:00
|
|
|
{
|
|
|
|
// create temporary file:
|
|
|
|
$this->tempFile();
|
|
|
|
|
2016-02-23 07:27:29 +01:00
|
|
|
// necessary for CSV writer:
|
|
|
|
$fullPath = storage_path('export') . DIRECTORY_SEPARATOR . $this->fileName;
|
2016-05-18 07:01:27 +02:00
|
|
|
$writer = Writer::createFromPath(new SplFileObject($fullPath, 'a+'), 'w');
|
|
|
|
$rows = [];
|
2016-02-23 07:27:29 +01:00
|
|
|
|
2016-10-23 09:44:14 +02:00
|
|
|
// get field names for header row:
|
|
|
|
$first = $this->getEntries()->first();
|
|
|
|
$headers = array_keys(get_object_vars($first));
|
|
|
|
$rows[] = $headers;
|
2016-05-17 15:19:07 +02:00
|
|
|
|
2016-02-04 17:16:16 +01:00
|
|
|
/** @var Entry $entry */
|
|
|
|
foreach ($this->getEntries() as $entry) {
|
2016-10-23 09:44:14 +02:00
|
|
|
$line = [];
|
|
|
|
foreach ($headers as $header) {
|
|
|
|
$line[] = $entry->$header;
|
|
|
|
}
|
|
|
|
$rows[] = $line;
|
2016-02-04 17:16:16 +01:00
|
|
|
}
|
|
|
|
$writer->insertAll($rows);
|
2016-04-25 18:43:09 +02:00
|
|
|
|
2016-04-06 16:37:28 +02:00
|
|
|
return true;
|
2016-02-04 17:16:16 +01:00
|
|
|
}
|
|
|
|
|
2016-05-17 15:19:07 +02:00
|
|
|
|
2016-02-04 17:16:16 +01:00
|
|
|
private function tempFile()
|
|
|
|
{
|
2016-02-23 07:27:29 +01:00
|
|
|
$this->fileName = $this->job->key . '-records.csv';
|
2016-02-04 17:16:16 +01:00
|
|
|
}
|
2016-02-10 16:01:18 +01:00
|
|
|
}
|