|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the DataImporter package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Loïc Sapone <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace IQ2i\DataImporter\Reader; |
|
15
|
|
|
|
|
16
|
|
|
class CsvReader implements ReaderInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
final public const CONTEXT_DELIMITER = 'csv_delimiter'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
final public const CONTEXT_ENCLOSURE = 'csv_enclosure'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
final public const CONTEXT_ESCAPE_CHAR = 'csv_escape_char'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
final public const CONTEXT_HEADERS = 'csv_headers'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
final public const CONTEXT_NO_HEADERS = 'no_headers'; |
|
42
|
|
|
|
|
43
|
|
|
private readonly \SplFileInfo $file; |
|
44
|
|
|
|
|
45
|
|
|
private readonly \SplFileObject $iterator; |
|
46
|
|
|
|
|
47
|
|
|
private int $count = 0; |
|
48
|
|
|
|
|
49
|
|
|
private int $index = 1; |
|
50
|
|
|
|
|
51
|
|
|
private array $defaultContext = [ |
|
52
|
|
|
self::CONTEXT_DELIMITER => ',', |
|
53
|
|
|
self::CONTEXT_ENCLOSURE => '"', |
|
54
|
|
|
self::CONTEXT_ESCAPE_CHAR => '', |
|
55
|
|
|
self::CONTEXT_HEADERS => [], |
|
56
|
|
|
self::CONTEXT_NO_HEADERS => false, |
|
57
|
|
|
]; |
|
58
|
|
|
|
|
59
|
|
|
public function __construct( |
|
60
|
|
|
string $filePath, |
|
61
|
|
|
private readonly ?string $dto = null, |
|
62
|
|
|
array $defaultContext = [], |
|
63
|
|
|
) { |
|
64
|
|
|
$this->file = new \SplFileInfo($filePath); |
|
|
|
|
|
|
65
|
|
|
if (!$this->file->isReadable()) { |
|
66
|
|
|
throw new \InvalidArgumentException('The file '.$this->file->getFilename().' is not readable.'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$this->defaultContext = \array_merge($this->defaultContext, $defaultContext); |
|
70
|
|
|
|
|
71
|
|
|
$this->iterator = $this->file->openFile(); |
|
|
|
|
|
|
72
|
|
|
$this->iterator->setFlags( |
|
73
|
|
|
\SplFileObject::READ_CSV | |
|
74
|
|
|
\SplFileObject::SKIP_EMPTY | |
|
75
|
|
|
\SplFileObject::READ_AHEAD | |
|
76
|
|
|
\SplFileObject::DROP_NEW_LINE |
|
77
|
|
|
); |
|
78
|
|
|
$this->iterator->setCsvControl( |
|
79
|
|
|
$this->defaultContext[self::CONTEXT_DELIMITER], |
|
80
|
|
|
$this->defaultContext[self::CONTEXT_ENCLOSURE], |
|
81
|
|
|
$this->defaultContext[self::CONTEXT_ESCAPE_CHAR] |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
if (!$this->defaultContext[self::CONTEXT_NO_HEADERS]) { |
|
85
|
|
|
$this->rewind(); |
|
86
|
|
|
$this->defaultContext[self::CONTEXT_HEADERS] = $this->iterator->current(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->rewind(); |
|
90
|
|
|
while ($this->valid()) { |
|
91
|
|
|
++$this->count; |
|
92
|
|
|
$this->next(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$this->rewind(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function getDto(): ?string |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->dto; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function isDenormalizable(): bool |
|
104
|
|
|
{ |
|
105
|
|
|
return null !== $this->dto && !empty($this->defaultContext[self::CONTEXT_HEADERS]); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function getFile(): \SplFileInfo |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->file; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function index(): mixed |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->index; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function current(): array |
|
119
|
|
|
{ |
|
120
|
|
|
if (!$this->valid()) { |
|
121
|
|
|
return []; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if (empty($this->defaultContext[self::CONTEXT_HEADERS])) { |
|
125
|
|
|
return $this->iterator->current(); |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
if ((\is_countable($this->defaultContext[self::CONTEXT_HEADERS]) ? \count($this->defaultContext[self::CONTEXT_HEADERS]) : 0) === (\is_countable($this->iterator->current()) ? \count($this->iterator->current()) : 0)) { |
|
|
|
|
|
|
129
|
|
|
return \array_combine($this->defaultContext[self::CONTEXT_HEADERS], $this->iterator->current()); |
|
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return []; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function next(): void |
|
136
|
|
|
{ |
|
137
|
|
|
$this->iterator->next(); |
|
138
|
|
|
++$this->index; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function key(): mixed |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->iterator->key(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function valid(): bool |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->iterator->valid(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function rewind(): void |
|
152
|
|
|
{ |
|
153
|
|
|
$this->iterator->rewind(); |
|
154
|
|
|
|
|
155
|
|
|
if (!empty($this->defaultContext[self::CONTEXT_HEADERS])) { |
|
156
|
|
|
$this->next(); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
$this->index = 1; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
public function count(): int |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->count; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|