Files
firefly-iii/app/Helpers/Csv/Data.php

329 lines
6.7 KiB
PHP
Raw Normal View History

2015-07-05 06:18:02 +02:00
<?php
2016-02-05 12:08:25 +01:00
declare(strict_types = 1);
2015-07-05 06:18:02 +02:00
namespace FireflyIII\Helpers\Csv;
use Crypt;
use League\Csv\Reader;
use Session;
use Storage;
2015-07-05 06:18:02 +02:00
/**
* Class Data
*
* @package FireflyIII\Helpers\Csv
*/
class Data
{
/** @var string */
2016-02-11 06:49:39 +01:00
protected $csvFileContent = '';
2015-07-05 06:18:02 +02:00
/** @var string */
2016-02-11 06:49:39 +01:00
protected $csvFileLocation = '';
2015-07-05 06:18:02 +02:00
/** @var string */
2016-02-11 06:49:39 +01:00
protected $dateFormat = '';
2016-01-19 13:59:54 +01:00
/** @var string */
2016-02-11 06:49:39 +01:00
protected $delimiter = '';
2015-07-05 06:18:02 +02:00
/** @var bool */
protected $hasHeaders;
2016-01-19 13:59:54 +01:00
/** @var int */
protected $importAccount = 0;
2015-07-05 06:18:02 +02:00
/** @var array */
2015-07-09 16:37:42 +02:00
protected $map = [];
2015-07-05 06:18:02 +02:00
/** @var array */
2015-07-09 16:37:42 +02:00
protected $mapped = [];
2015-07-05 06:18:02 +02:00
/** @var Reader */
protected $reader;
/** @var array */
2015-07-09 16:37:42 +02:00
protected $roles = [];
2015-07-06 20:21:55 +02:00
/** @var array */
2015-07-09 16:37:42 +02:00
protected $specifix = [];
2015-07-05 06:18:02 +02:00
/**
*/
public function __construct()
{
$this->sessionHasHeaders();
$this->sessionDateFormat();
$this->sessionCsvFileLocation();
$this->sessionMap();
$this->sessionRoles();
$this->sessionMapped();
2015-07-06 20:21:55 +02:00
$this->sessionSpecifix();
2015-07-09 16:37:42 +02:00
$this->sessionImportAccount();
$this->sessionDelimiter();
2015-07-05 06:18:02 +02:00
}
2016-01-19 13:59:54 +01:00
/**
*
* @return string
*/
2016-04-06 16:37:28 +02:00
public function getCsvFileContent(): string
2015-07-05 06:18:02 +02:00
{
return $this->csvFileContent ?? '';
2015-07-05 06:18:02 +02:00
}
2016-01-19 13:59:54 +01:00
/**
*
* @param string $csvFileContent
*/
2016-02-05 09:25:15 +01:00
public function setCsvFileContent(string $csvFileContent)
2015-07-05 06:18:02 +02:00
{
2016-01-19 13:59:54 +01:00
$this->csvFileContent = $csvFileContent;
2015-07-05 06:18:02 +02:00
}
2016-01-19 13:59:54 +01:00
/**
* FIXxME may return null
2016-04-25 18:43:09 +02:00
*
2016-01-19 13:59:54 +01:00
* @return string
*/
2016-04-06 16:37:28 +02:00
public function getCsvFileLocation(): string
2015-07-06 20:21:55 +02:00
{
2016-01-19 13:59:54 +01:00
return $this->csvFileLocation;
}
2016-01-19 13:59:54 +01:00
/**
*
* @param string $csvFileLocation
*/
2016-02-05 09:25:15 +01:00
public function setCsvFileLocation(string $csvFileLocation)
{
2016-01-19 13:59:54 +01:00
Session::put('csv-file', $csvFileLocation);
$this->csvFileLocation = $csvFileLocation;
2015-07-06 20:21:55 +02:00
}
2015-07-05 06:18:02 +02:00
/**
* FIXxME may return null
2016-04-25 18:43:09 +02:00
*
2015-07-05 06:18:02 +02:00
* @return string
*/
2016-04-06 16:37:28 +02:00
public function getDateFormat(): string
2015-07-05 06:18:02 +02:00
{
return $this->dateFormat;
}
/**
*
2016-02-05 09:25:15 +01:00
* @param string $dateFormat
2015-07-05 06:18:02 +02:00
*/
2016-02-05 09:25:15 +01:00
public function setDateFormat(string $dateFormat)
2015-07-05 06:18:02 +02:00
{
Session::put('csv-date-format', $dateFormat);
$this->dateFormat = $dateFormat;
}
2015-07-09 16:37:42 +02:00
/**
* FIXxME may return null
2016-04-25 18:43:09 +02:00
*
2016-01-19 13:59:54 +01:00
* @return string
2015-07-05 06:18:02 +02:00
*/
2016-04-06 16:37:28 +02:00
public function getDelimiter(): string
2015-07-05 06:18:02 +02:00
{
2016-01-19 13:59:54 +01:00
return $this->delimiter;
2015-07-05 06:18:02 +02:00
}
/**
*
2016-01-19 13:59:54 +01:00
* @param string $delimiter
2015-07-05 06:18:02 +02:00
*/
2016-02-05 09:25:15 +01:00
public function setDelimiter(string $delimiter)
2015-07-05 06:18:02 +02:00
{
2016-01-19 13:59:54 +01:00
Session::put('csv-delimiter', $delimiter);
$this->delimiter = $delimiter;
2015-07-05 06:18:02 +02:00
}
/**
*
2015-07-05 06:18:02 +02:00
* @return array
*/
2016-04-06 16:37:28 +02:00
public function getMap(): array
2015-07-05 06:18:02 +02:00
{
return $this->map;
}
/**
*
2016-01-15 23:12:52 +01:00
* @param array $map
2015-07-05 06:18:02 +02:00
*/
public function setMap(array $map)
{
Session::put('csv-map', $map);
$this->map = $map;
}
/**
*
2015-07-05 06:18:02 +02:00
* @return array
*/
2016-04-06 16:37:28 +02:00
public function getMapped(): array
2015-07-05 06:18:02 +02:00
{
return $this->mapped;
}
/**
*
2016-01-15 23:12:52 +01:00
* @param array $mapped
2015-07-05 06:18:02 +02:00
*/
public function setMapped(array $mapped)
{
Session::put('csv-mapped', $mapped);
$this->mapped = $mapped;
}
/**
*
2015-07-05 06:18:02 +02:00
* @return Reader
*/
2016-04-06 16:37:28 +02:00
public function getReader(): Reader
2015-07-05 06:18:02 +02:00
{
if (!is_null($this->csvFileContent) && strlen($this->csvFileContent) === 0) {
2015-07-05 06:18:02 +02:00
$this->loadCsvFile();
}
2016-01-15 23:12:52 +01:00
2015-07-05 06:18:02 +02:00
if (is_null($this->reader)) {
$this->reader = Reader::createFromString($this->getCsvFileContent());
$this->reader->setDelimiter($this->delimiter);
2015-07-05 06:18:02 +02:00
}
2016-01-15 23:12:52 +01:00
2015-07-05 06:18:02 +02:00
return $this->reader;
}
/**
*
2016-01-19 13:59:54 +01:00
* @return array
2015-07-05 06:18:02 +02:00
*/
2016-04-06 16:37:28 +02:00
public function getRoles(): array
2015-07-05 06:18:02 +02:00
{
2016-01-19 13:59:54 +01:00
return $this->roles;
2015-07-05 06:18:02 +02:00
}
/**
*
2016-01-19 13:59:54 +01:00
* @param array $roles
2015-07-05 06:18:02 +02:00
*/
2016-01-19 13:59:54 +01:00
public function setRoles(array $roles)
2015-07-05 06:18:02 +02:00
{
2016-01-19 13:59:54 +01:00
Session::put('csv-roles', $roles);
$this->roles = $roles;
2015-07-05 06:18:02 +02:00
}
/**
*
2016-01-19 13:59:54 +01:00
* @return array
2015-07-05 06:18:02 +02:00
*/
2016-04-06 16:37:28 +02:00
public function getSpecifix(): array
2015-07-05 06:18:02 +02:00
{
2016-01-19 13:59:54 +01:00
return is_array($this->specifix) ? $this->specifix : [];
2015-07-05 06:18:02 +02:00
}
/**
*
2016-01-19 13:59:54 +01:00
* @param array $specifix
2015-07-05 06:18:02 +02:00
*/
2016-01-22 21:08:04 +01:00
public function setSpecifix(array $specifix)
2015-07-05 06:18:02 +02:00
{
2016-01-19 13:59:54 +01:00
Session::put('csv-specifix', $specifix);
$this->specifix = $specifix;
2015-07-05 06:18:02 +02:00
}
/**
*
2016-01-19 13:59:54 +01:00
* @return bool
2015-07-05 06:18:02 +02:00
*/
2016-04-06 16:37:28 +02:00
public function hasHeaders(): bool
2015-07-05 06:18:02 +02:00
{
2016-01-19 13:59:54 +01:00
return $this->hasHeaders;
2015-07-05 06:18:02 +02:00
}
/**
*
2016-01-19 13:59:54 +01:00
* @param bool $hasHeaders
2015-07-05 06:18:02 +02:00
*/
2016-02-05 09:25:15 +01:00
public function setHasHeaders(bool $hasHeaders)
2015-07-05 06:18:02 +02:00
{
2016-01-19 13:59:54 +01:00
Session::put('csv-has-headers', $hasHeaders);
$this->hasHeaders = $hasHeaders;
2015-07-05 06:18:02 +02:00
}
2015-07-06 20:21:55 +02:00
/**
*
2016-01-19 13:59:54 +01:00
* @param int $importAccount
2015-07-06 20:21:55 +02:00
*/
2016-02-05 09:25:15 +01:00
public function setImportAccount(int $importAccount)
2015-07-06 20:21:55 +02:00
{
2016-01-19 13:59:54 +01:00
Session::put('csv-import-account', $importAccount);
$this->importAccount = $importAccount;
2015-07-06 20:21:55 +02:00
}
2016-01-19 13:59:54 +01:00
protected function loadCsvFile()
2015-07-06 20:21:55 +02:00
{
2016-01-19 13:59:54 +01:00
$file = $this->getCsvFileLocation();
$disk = Storage::disk('upload');
$content = $disk->get($file);
2016-01-19 13:59:54 +01:00
$contentDecrypted = Crypt::decrypt($content);
$this->setCsvFileContent($contentDecrypted);
2015-07-06 20:21:55 +02:00
}
2016-01-19 13:59:54 +01:00
protected function sessionCsvFileLocation()
{
2016-01-19 13:59:54 +01:00
if (Session::has('csv-file')) {
2016-02-04 07:27:03 +01:00
$this->csvFileLocation = (string)session('csv-file');
2016-01-19 13:59:54 +01:00
}
}
2015-07-05 06:18:02 +02:00
2016-01-19 13:59:54 +01:00
protected function sessionDateFormat()
{
2016-01-19 13:59:54 +01:00
if (Session::has('csv-date-format')) {
2016-02-04 07:27:03 +01:00
$this->dateFormat = (string)session('csv-date-format');
2016-01-19 13:59:54 +01:00
}
}
protected function sessionDelimiter()
{
if (Session::has('csv-delimiter')) {
2016-02-04 07:27:03 +01:00
$this->delimiter = session('csv-delimiter');
2016-01-19 13:59:54 +01:00
}
}
protected function sessionHasHeaders()
{
if (Session::has('csv-has-headers')) {
2016-02-04 07:27:03 +01:00
$this->hasHeaders = (bool)session('csv-has-headers');
2016-01-19 13:59:54 +01:00
}
}
protected function sessionImportAccount()
{
if (Session::has('csv-import-account')) {
2016-02-04 07:27:03 +01:00
$this->importAccount = intval(session('csv-import-account'));
2016-01-19 13:59:54 +01:00
}
}
protected function sessionMap()
{
if (Session::has('csv-map')) {
2016-02-04 07:27:03 +01:00
$this->map = (array)session('csv-map');
2016-01-19 13:59:54 +01:00
}
}
protected function sessionMapped()
{
if (Session::has('csv-mapped')) {
2016-02-04 07:27:03 +01:00
$this->mapped = (array)session('csv-mapped');
2016-01-19 13:59:54 +01:00
}
}
protected function sessionRoles()
{
if (Session::has('csv-roles')) {
2016-02-04 07:27:03 +01:00
$this->roles = (array)session('csv-roles');
2016-01-19 13:59:54 +01:00
}
}
protected function sessionSpecifix()
{
if (Session::has('csv-specifix')) {
2016-02-04 07:27:03 +01:00
$this->specifix = (array)session('csv-specifix');
2016-01-19 13:59:54 +01:00
}
}
2015-07-09 21:26:40 +02:00
}