Passed
Push — master ( 25c805...999286 )
by Andy
02:56 queued 11s
created

AbstractCsvDocument::setDelimiter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Palmtree\Csv;
4
5
abstract class AbstractCsvDocument
6
{
7
    /** @var string Path to CSV file. */
8
    protected $filePath;
9
    /** @var bool Whether the CSV file contains headers */
10
    protected $hasHeaders = true;
11
    /** @var string Cell delimiter. Default ',' (comma) */
12
    protected $delimiter = ',';
13
    /** @var string Cell enclosure. Default '"' (double quote) */
14
    protected $enclosure = '"';
15
    /** @var string Cell escape character. Default null byte */
16
    protected $escapeCharacter = "\0";
17
    /** @var CsvFileObject|null */
18
    protected $document;
19
20 12
    public function __construct(string $filePath, bool $hasHeaders = true)
21
    {
22
        $this
23 12
            ->setHasHeaders($hasHeaders)
24 12
            ->setFilePath($filePath);
25 12
    }
26
27 8
    public function __destruct()
28
    {
29 8
        $this->closeDocument();
30 8
    }
31
32
    abstract protected function getOpenMode(): string;
33
34
    /**
35
     * Closes the document by setting our reference to null to ensure its destructor is called.
36
     */
37 12
    public function closeDocument(): void
38
    {
39 12
        $this->document = null;
40 12
    }
41
42
    public function setDocument(?CsvFileObject $document = null): self
43
    {
44
        $this->closeDocument();
45
46
        $this->document = $document;
47
48
        return $this;
49
    }
50
51 9
    public function getDocument(): CsvFileObject
52
    {
53 9
        if ($this->document === null) {
54 9
            $this->createDocument();
55
        }
56
57 8
        return $this->document;
58
    }
59
60
    public function getFilePath(): string
61
    {
62
        return $this->filePath;
63
    }
64
65 12
    public function setFilePath(string $filePath): self
66
    {
67 12
        $this->filePath = $filePath;
68
69 12
        return $this;
70
    }
71
72
    public function hasHeaders(): bool
73
    {
74
        return $this->hasHeaders;
75
    }
76
77 12
    public function setHasHeaders(bool $hasHeaders): self
78
    {
79 12
        $this->hasHeaders = $hasHeaders;
80
81 12
        return $this;
82
    }
83
84
    public function getDelimiter(): string
85
    {
86
        return $this->delimiter;
87
    }
88
89 1
    public function setDelimiter(string $delimiter): self
90
    {
91 1
        $this->delimiter = $delimiter;
92
93 1
        $this->getDocument()->setCsvControl($this->delimiter, $this->enclosure, $this->escapeCharacter);
94
95 1
        return $this;
96
    }
97
98
    public function getEnclosure(): string
99
    {
100
        return $this->enclosure;
101
    }
102
103
    public function setEnclosure(string $enclosure): self
104
    {
105
        $this->enclosure = $enclosure;
106
107
        $this->getDocument()->setCsvControl($this->delimiter, $this->enclosure, $this->escapeCharacter);
108
109
        return $this;
110
    }
111
112
    public function getEscapeCharacter(): string
113
    {
114
        return $this->escapeCharacter;
115
    }
116
117
    public function setEscapeCharacter(string $escapeCharacter): self
118
    {
119
        $this->escapeCharacter = $escapeCharacter;
120
121
        $this->getDocument()->setCsvControl($this->delimiter, $this->enclosure, $this->escapeCharacter);
122
123
        return $this;
124
    }
125
126 9
    private function createDocument(): void
127
    {
128 9
        $this->closeDocument();
129
130 9
        $document = new CsvFileObject($this->filePath, $this->getOpenMode());
131
132 8
        $document->setFlags(CsvFileObject::READ_CSV);
133 8
        $document->setCsvControl($this->delimiter, $this->enclosure, $this->escapeCharacter);
134
135 8
        $this->document = $document;
136 8
    }
137
}
138