|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the League.csv library |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/MIT |
|
6
|
|
|
* @link https://github.com/thephpleague/csv/ |
|
7
|
|
|
* @version 9.0.0 |
|
8
|
|
|
* @package League.csv |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
namespace League\Csv; |
|
14
|
|
|
|
|
15
|
|
|
use InvalidArgumentException; |
|
16
|
|
|
use IteratorAggregate; |
|
17
|
|
|
use League\Csv\Config\Controls; |
|
18
|
|
|
use SplFileInfo; |
|
19
|
|
|
use SplFileObject; |
|
20
|
|
|
use SplTempFileObject; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Abstract class to set the CSV document properties |
|
24
|
|
|
* |
|
25
|
|
|
* @package League.csv |
|
26
|
|
|
* @since 4.0.0 |
|
27
|
|
|
*/ |
|
28
|
|
|
abstract class AbstractCsv implements IteratorAggregate |
|
29
|
|
|
{ |
|
30
|
|
|
use Controls; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* UTF-8 BOM sequence |
|
34
|
|
|
*/ |
|
35
|
|
|
const BOM_UTF8 = "\xEF\xBB\xBF"; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* UTF-16 BE BOM sequence |
|
39
|
|
|
*/ |
|
40
|
|
|
const BOM_UTF16_BE = "\xFE\xFF"; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* UTF-16 LE BOM sequence |
|
44
|
|
|
*/ |
|
45
|
|
|
const BOM_UTF16_LE = "\xFF\xFE"; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* UTF-32 BE BOM sequence |
|
49
|
|
|
*/ |
|
50
|
|
|
const BOM_UTF32_BE = "\x00\x00\xFE\xFF"; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* UTF-32 LE BOM sequence |
|
54
|
|
|
*/ |
|
55
|
|
|
const BOM_UTF32_LE = "\x00\x00\xFF\xFE"; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* The constructor path |
|
59
|
|
|
* |
|
60
|
|
|
* can be a SplFileInfo object or the string path to a file |
|
61
|
|
|
* |
|
62
|
|
|
* @var SplFileObject|string |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $path; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* The file open mode flag |
|
68
|
|
|
* |
|
69
|
|
|
* @var string |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $open_mode; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Creates a new instance |
|
75
|
|
|
* |
|
76
|
|
|
* The path must be an SplFileInfo object |
|
77
|
|
|
* an object that implements the `__toString` method |
|
78
|
|
|
* a path to a file |
|
79
|
|
|
* |
|
80
|
|
|
* @param SplFileObject|string $path The file path |
|
81
|
|
|
* @param string $open_mode The file open mode flag |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function __construct($path, $open_mode = 'r+') |
|
84
|
|
|
{ |
|
85
|
|
|
$this->open_mode = strtolower($open_mode); |
|
86
|
|
|
$this->path = $path; |
|
87
|
|
|
$this->initStreamFilter($this->path); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Release the underlying SplFileObject if it exists |
|
92
|
|
|
*/ |
|
93
|
|
|
public function __destruct() |
|
94
|
|
|
{ |
|
95
|
|
|
$this->path = null; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Return a new {@link AbstractCsv} from a SplFileObject |
|
100
|
|
|
* |
|
101
|
|
|
* @param SplFileObject $file |
|
102
|
|
|
* |
|
103
|
|
|
* @return static |
|
104
|
|
|
*/ |
|
105
|
357 |
|
public static function createFromFileObject(SplFileObject $file) |
|
106
|
|
|
{ |
|
107
|
357 |
|
$csv = new static($file); |
|
108
|
357 |
|
$controls = $file->getCsvControl(); |
|
109
|
357 |
|
$csv->setDelimiter($controls[0]); |
|
110
|
357 |
|
$csv->setEnclosure($controls[1]); |
|
111
|
|
|
if (isset($controls[2])) { |
|
112
|
|
|
$csv->setEscape($controls[2]); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
240 |
|
return $csv; |
|
116
|
|
|
} |
|
117
|
240 |
|
|
|
118
|
240 |
|
/** |
|
119
|
|
|
* Return a new {@link AbstractCsv} from a string |
|
120
|
|
|
* |
|
121
|
|
|
* The string must be an object that implements the `__toString` method, |
|
122
|
|
|
* or a string |
|
123
|
|
|
* |
|
124
|
|
|
* @param string|object $str the string |
|
125
|
|
|
* |
|
126
|
|
|
* @return static |
|
127
|
312 |
|
*/ |
|
128
|
|
|
public static function createFromString($str) |
|
129
|
312 |
|
{ |
|
130
|
|
|
$file = new SplTempFileObject(); |
|
131
|
|
|
$file->fwrite(static::filterString($str)); |
|
132
|
|
|
|
|
133
|
|
|
return new static($file); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Return a new {@link AbstractCsv} from a string |
|
138
|
|
|
* |
|
139
|
|
|
* @param mixed $path file path |
|
140
|
|
|
* @param string $open_mode the file open mode flag |
|
141
|
|
|
* |
|
142
|
27 |
|
* @throws InvalidArgumentException If $path is a SplTempFileObject object |
|
143
|
|
|
* |
|
144
|
27 |
|
* @return static |
|
145
|
27 |
|
*/ |
|
146
|
|
|
public static function createFromPath($path, $open_mode = 'r+') |
|
147
|
27 |
|
{ |
|
148
|
|
|
if ($path instanceof SplTempFileObject) { |
|
149
|
|
|
throw new InvalidArgumentException('an `SplTempFileObject` object does not contain a valid path'); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
if ($path instanceof SplFileInfo) { |
|
153
|
|
|
$path = $path->getPath().'/'.$path->getBasename(); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
return new static(static::filterString($path), $open_mode); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
90 |
|
/** |
|
160
|
|
|
* Return a new {@link AbstractCsv} instance from another {@link AbstractCsv} object |
|
161
|
90 |
|
* |
|
162
|
87 |
|
* @param string $class the class to be instantiated |
|
163
|
|
|
* @param string $open_mode the file open mode flag |
|
164
|
3 |
|
* |
|
165
|
|
|
* @return static |
|
166
|
|
|
*/ |
|
167
|
|
|
protected function newInstance($class, $open_mode) |
|
168
|
|
|
{ |
|
169
|
|
|
$csv = new $class($this->path, $open_mode); |
|
170
|
|
|
$csv->delimiter = $this->delimiter; |
|
171
|
|
|
$csv->enclosure = $this->enclosure; |
|
172
|
|
|
$csv->escape = $this->escape; |
|
173
|
|
|
$csv->input_encoding = $this->input_encoding; |
|
174
|
|
|
$csv->input_bom = $this->input_bom; |
|
175
|
|
|
$csv->output_bom = $this->output_bom; |
|
176
|
|
|
$csv->newline = $this->newline; |
|
177
|
54 |
|
$csv->header = $this->header; |
|
178
|
|
|
|
|
179
|
54 |
|
return $csv; |
|
180
|
3 |
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
51 |
|
* Return a new {@link Writer} instance from a {@link AbstractCsv} object |
|
184
|
3 |
|
* |
|
185
|
2 |
|
* @param string $open_mode the file open mode flag |
|
186
|
|
|
* |
|
187
|
51 |
|
* @return Writer |
|
188
|
|
|
*/ |
|
189
|
|
|
public function newWriter($open_mode = 'r+') |
|
190
|
|
|
{ |
|
191
|
|
|
return $this->newInstance(Writer::class, $open_mode); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Return a new {@link Reader} instance from a {@link AbstractCsv} object |
|
196
|
|
|
* |
|
197
|
|
|
* @param string $open_mode the file open mode flag |
|
198
|
6 |
|
* |
|
199
|
|
|
* @return Reader |
|
200
|
6 |
|
*/ |
|
201
|
6 |
|
public function newReader($open_mode = 'r+') |
|
202
|
6 |
|
{ |
|
203
|
6 |
|
return $this->newInstance(Reader::class, $open_mode); |
|
204
|
6 |
|
} |
|
205
|
6 |
|
|
|
206
|
6 |
|
/** |
|
207
|
6 |
|
* Returns the inner SplFileObject |
|
208
|
|
|
* |
|
209
|
6 |
|
* @return SplFileObject |
|
210
|
|
|
*/ |
|
211
|
|
|
public function getIterator() |
|
212
|
|
|
{ |
|
213
|
|
|
$iterator = $this->path; |
|
214
|
|
|
if (!$iterator instanceof SplFileObject) { |
|
215
|
|
|
$iterator = new SplFileObject($this->getStreamFilterPath(), $this->open_mode); |
|
216
|
|
|
} |
|
217
|
|
|
$iterator->setCsvControl($this->delimiter, $this->enclosure, $this->escape); |
|
218
|
|
|
$iterator->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY); |
|
219
|
3 |
|
|
|
220
|
|
|
return $iterator; |
|
221
|
3 |
|
} |
|
222
|
|
|
} |
|
223
|
|
|
|