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