Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
fab2s committed Apr 15, 2018
1 parent e60146b commit bf91e16
Showing 1 changed file with 43 additions and 13 deletions.
56 changes: 43 additions & 13 deletions src/Loaders/File/CsvLoader.php
Expand Up @@ -49,7 +49,8 @@ public function __construct($destination, $delimiter = null, $enclosure = null,
*/
public function exec($param)
{
$this->handleFirstLine($param)->writeCsvLine($param);
$this->handleFirstLine($param)
->writeCsvLine($param);
}

/**
Expand All @@ -60,23 +61,52 @@ public function exec($param)
protected function handleFirstLine($param)
{
if ($this->isFirstLine) {
if ($this->useBom && ($bom = $this->prependBom(''))) {
fwrite($this->handle, $bom);
}
$this->handleBom()
->handleSep()
->handleHeader($param);
$this->isFirstLine = false;
}

if ($this->useSep) {
fwrite($this->handle, "sep=$this->delimiter" . PHP_EOL);
}
return $this;
}

/**
* @return $this
*/
protected function handleBom()
{
if ($this->useBom && ($bom = $this->prependBom(''))) {
fwrite($this->handle, $bom);
}

return $this;
}

if ($this->useHeader) {
if (!isset($this->header)) {
$this->header = array_keys($param);
}
/**
* @return $this
*/
protected function handleSep()
{
if ($this->useSep) {
fwrite($this->handle, "sep=$this->delimiter" . PHP_EOL);
}

return $this;
}

$this->writeCsvLine($this->header);
/**
* @param array $param
*
* @return $this
*/
protected function handleHeader(array $param)
{
if ($this->useHeader) {
if (!isset($this->header)) {
$this->header = array_keys($param);
}

$this->isFirstLine = false;
$this->writeCsvLine($this->header);
}

return $this;
Expand Down

0 comments on commit bf91e16

Please sign in to comment.