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

284 lines
5.6 KiB
PHP
Raw Normal View History

2015-07-04 23:18:02 -05:00
<?php
namespace FireflyIII\Helpers\Csv;
use Crypt;
use League\Csv\Reader;
use Session;
/**
* Class Data
*
* @package FireflyIII\Helpers\Csv
*/
class Data
{
/** @var string */
protected $csvFileContent;
/** @var string */
protected $csvFileLocation;
/** @var string */
protected $dateFormat;
/** @var bool */
protected $hasHeaders;
/** @var array */
2015-07-09 09:37:42 -05:00
protected $map = [];
2015-07-04 23:18:02 -05:00
/** @var array */
2015-07-09 09:37:42 -05:00
protected $mapped = [];
2015-07-04 23:18:02 -05:00
/** @var Reader */
protected $reader;
/** @var array */
2015-07-09 09:37:42 -05:00
protected $roles = [];
2015-07-04 23:18:02 -05:00
2015-07-06 13:21:55 -05:00
/** @var array */
2015-07-09 09:37:42 -05:00
protected $specifix = [];
/** @var int */
protected $importAccount = 0;
2015-07-06 13:21:55 -05:00
2015-07-04 23:18:02 -05:00
/**
*
*/
public function __construct()
{
$this->sessionHasHeaders();
$this->sessionDateFormat();
$this->sessionCsvFileLocation();
$this->sessionMap();
$this->sessionRoles();
$this->sessionMapped();
2015-07-06 13:21:55 -05:00
$this->sessionSpecifix();
2015-07-09 09:37:42 -05:00
$this->sessionImportAccount();
2015-07-04 23:18:02 -05:00
}
protected function sessionHasHeaders()
{
if (Session::has('csv-has-headers')) {
$this->hasHeaders = (bool)Session::get('csv-has-headers');
}
}
2015-07-09 09:37:42 -05:00
protected function sessionImportAccount()
{
if (Session::has('csv-import-account')) {
$this->importAccount = intval(Session::get('csv-import-account'));
}
}
2015-07-04 23:18:02 -05:00
protected function sessionDateFormat()
{
if (Session::has('csv-date-format')) {
$this->dateFormat = (string)Session::get('csv-date-format');
}
}
protected function sessionCsvFileLocation()
{
if (Session::has('csv-file')) {
$this->csvFileLocation = (string)Session::get('csv-file');
}
}
protected function sessionMap()
{
if (Session::has('csv-map')) {
$this->map = (array)Session::get('csv-map');
}
}
protected function sessionRoles()
{
if (Session::has('csv-roles')) {
$this->roles = (array)Session::get('csv-roles');
}
}
protected function sessionMapped()
{
if (Session::has('csv-mapped')) {
$this->mapped = (array)Session::get('csv-mapped');
}
}
2015-07-06 13:21:55 -05:00
protected function sessionSpecifix()
{
if (Session::has('csv-specifix')) {
$this->specifix = (array)Session::get('csv-specifix');
}
}
2015-07-04 23:18:02 -05:00
/**
* @return string
*/
public function getDateFormat()
{
return $this->dateFormat;
}
/**
* @param mixed $dateFormat
*/
public function setDateFormat($dateFormat)
{
Session::put('csv-date-format', $dateFormat);
$this->dateFormat = $dateFormat;
}
2015-07-09 09:37:42 -05:00
/**
* @param int $importAccount
*/
public function setImportAccount($importAccount)
{
Session::put('csv-import-account', $importAccount);
$this->importAccount = $importAccount;
}
2015-07-04 23:18:02 -05:00
/**
* @return bool
*/
2015-07-06 15:23:34 -05:00
public function hasHeaders()
2015-07-04 23:18:02 -05:00
{
return $this->hasHeaders;
}
/**
* @param bool $hasHeaders
*/
public function setHasHeaders($hasHeaders)
{
Session::put('csv-has-headers', $hasHeaders);
$this->hasHeaders = $hasHeaders;
}
/**
* @return array
*/
public function getMap()
{
return $this->map;
}
/**
* @param array $map
*/
public function setMap(array $map)
{
Session::put('csv-map', $map);
$this->map = $map;
}
/**
* @return array
*/
public function getMapped()
{
return $this->mapped;
}
/**
* @param array $mapped
*/
public function setMapped(array $mapped)
{
Session::put('csv-mapped', $mapped);
$this->mapped = $mapped;
}
/**
* @return Reader
*/
public function getReader()
{
if (strlen($this->csvFileContent) === 0) {
$this->loadCsvFile();
}
if (is_null($this->reader)) {
$this->reader = Reader::createFromString($this->getCsvFileContent());
}
return $this->reader;
}
protected function loadCsvFile()
{
$file = $this->getCsvFileLocation();
$content = file_get_contents($file);
$contentDecrypted = Crypt::decrypt($content);
$this->setCsvFileContent($contentDecrypted);
}
/**
* @return string
*/
public function getCsvFileLocation()
{
return $this->csvFileLocation;
}
/**
* @param string $csvFileLocation
*/
public function setCsvFileLocation($csvFileLocation)
{
Session::put('csv-file', $csvFileLocation);
$this->csvFileLocation = $csvFileLocation;
}
/**
* @return string
*/
public function getCsvFileContent()
{
return $this->csvFileContent;
}
/**
* @param string $csvFileContent
*/
public function setCsvFileContent($csvFileContent)
{
$this->csvFileContent = $csvFileContent;
}
/**
* @return array
*/
public function getRoles()
{
return $this->roles;
}
/**
* @param array $roles
*/
public function setRoles(array $roles)
{
Session::put('csv-roles', $roles);
$this->roles = $roles;
}
2015-07-06 13:21:55 -05:00
/**
* @return array
*/
public function getSpecifix()
{
2015-07-09 08:27:40 -05:00
return is_array($this->specifix) ? $this->specifix : [];
2015-07-06 13:21:55 -05:00
}
/**
* @param array $specifix
*/
public function setSpecifix($specifix)
{
Session::put('csv-specifix', $specifix);
$this->specifix = $specifix;
}
2015-07-04 23:18:02 -05:00
2015-07-09 14:26:40 -05:00
}