Passed
Push — master ( a0f654...53c07a )
by Fabrice
02:26
created

CsvLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of YaEtl.
5
 *     (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl
6
 * This source file is licensed under the MIT license which you will
7
 * find in the LICENSE file or at https://opensource.org/licenses/MIT
8
 */
9
10
namespace fab2s\YaEtl\Loaders\File;
11
12
use fab2s\NodalFlow\NodalFlowException;
13
use fab2s\NodalFlow\YaEtlException;
14
use fab2s\YaEtl\Traits\CsvHandlerTrait;
15
16
/**
17
 * Class CsvLoader
18
 */
19
class CsvLoader extends FileLoaderAbstract
20
{
21
    use CsvHandlerTrait;
22
23
    /**
24
     * @var bool
25
     */
26
    protected $isFirstLine = true;
27
28
    /**
29
     * CsvLoader constructor.
30
     *
31
     * @param string      $destination
32
     * @param string|null $delimiter
33
     * @param string|null $enclosure
34
     * @param string|null $escape
35
     *
36
     * @throws NodalFlowException
37
     * @throws YaEtlException
38
     */
39
    public function __construct($destination, $delimiter = null, $enclosure = null, $escape = null)
40
    {
41
        parent::__construct($destination);
42
        $this->initCsvOptions($delimiter, $enclosure, $escape);
43
    }
44
45
    /**
46
     * Execute this Node
47
     *
48
     * @param array $param
49
     */
50
    public function exec($param)
51
    {
52
        $this->handleFirstLine($param)->writeCsvLine($param);
53
    }
54
55
    /**
56
     * @param array $param
57
     *
58
     * @return $this
59
     */
60
    protected function handleFirstLine($param)
61
    {
62
        if ($this->isFirstLine) {
63
            if ($this->useBom && ($bom = $this->prependBom(''))) {
64
                fwrite($this->handle, $bom);
0 ignored issues
show
Bug introduced by
$bom of type true is incompatible with the type string expected by parameter $string of fwrite(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
                fwrite($this->handle, /** @scrutinizer ignore-type */ $bom);
Loading history...
65
            }
66
67
            if ($this->useSep) {
68
                fwrite($this->handle, "sep=$this->delimiter" . PHP_EOL);
69
            }
70
71
            if ($this->useHeader) {
72
                if (!isset($this->header)) {
73
                    $this->header = array_keys($param);
74
                }
75
76
                $this->writeCsvLine($this->header);
77
            }
78
79
            $this->isFirstLine = false;
80
        }
81
82
        return $this;
83
    }
84
85
    /**
86
     * @param array $record
87
     *
88
     * @return bool|int
89
     */
90
    protected function writeCsvLine(array $record)
91
    {
92
        return fputcsv($this->handle, $record, $this->delimiter, $this->enclosure, $this->escape);
93
    }
94
}
95