CsvWriter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 63
ccs 16
cts 17
cp 0.9412
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A prepare() 0 6 2
A writeItem() 0 9 3
1
<?php
2
3
namespace Mathielen\DataImport\Writer;
4
5
use Ddeboer\DataImport\Writer\AbstractStreamWriter;
6
7
/**
8
 * Writes to a CSV file.
9
 *
10
 * @author David de Boer <[email protected]>
11
 */
12
class CsvWriter extends AbstractStreamWriter
13
{
14
    /**
15
     * @var string
16
     */
17
    private $delimiter = ';';
18
19
    /**
20
     * @var string
21
     */
22
    private $enclosure = '"';
23
24
    /**
25
     * @var bool
26
     */
27
    private $utf8Encoding = false;
28
    private $row = 1;
29
30
    /**
31
     * @var bool
32
     */
33
    protected $prependHeaderRow;
34
35
    /**
36
     * @param string   $delimiter    The delimiter
37
     * @param string   $enclosure    The enclosure
38
     * @param resource $stream
39
     * @param bool     $utf8Encoding
40
     */
41 3
    public function __construct($delimiter = ';', $enclosure = '"', $stream = null, $utf8Encoding = false, $prependHeaderRow = false)
42
    {
43 3
        parent::__construct($stream);
44
45 3
        $this->delimiter = $delimiter;
46 3
        $this->enclosure = $enclosure;
47 3
        $this->utf8Encoding = $utf8Encoding;
48
49 3
        $this->prependHeaderRow = $prependHeaderRow;
50 3
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 3
    public function prepare()
56
    {
57 3
        if ($this->utf8Encoding) {
58
            fprintf($this->getStream(), chr(0xEF).chr(0xBB).chr(0xBF));
59
        }
60 3
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 3
    public function writeItem(array $item)
66
    {
67 3
        if ($this->prependHeaderRow && 1 == $this->row++) {
68 3
            $headers = array_keys($item);
69 3
            fputcsv($this->getStream(), $headers, $this->delimiter, $this->enclosure);
70
        }
71
72 3
        fputcsv($this->getStream(), $item, $this->delimiter, $this->enclosure);
73 3
    }
74
}
75