|
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
|
|
|
declare(strict_types=1); |
|
14
|
|
|
|
|
15
|
|
|
namespace League\Csv\Config; |
|
16
|
|
|
|
|
17
|
|
|
use InvalidArgumentException; |
|
18
|
|
|
use League\Csv\AbstractCsv; |
|
19
|
|
|
use SplFileObject; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* An abstract class to enable basic CSV manipulation |
|
23
|
|
|
* |
|
24
|
|
|
* @package League.csv |
|
25
|
|
|
* @since 9.0.0 |
|
26
|
|
|
* @internal |
|
27
|
|
|
*/ |
|
28
|
|
|
trait ControlsTrait |
|
29
|
|
|
{ |
|
30
|
|
|
use StreamTrait; |
|
31
|
|
|
use ValidatorTrait; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* the field delimiter (one character only) |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $delimiter = ','; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* the field enclosure character (one character only) |
|
42
|
|
|
* |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $enclosure = '"'; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* the field escape character (one character only) |
|
49
|
|
|
* |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $escape = '\\'; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* The Input file BOM character |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $input_bom; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* The Output file BOM character |
|
62
|
|
|
* @var string |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $output_bom = ''; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns the current field delimiter |
|
68
|
|
|
* |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
4 |
|
public function getDelimiter(): string |
|
72
|
|
|
{ |
|
73
|
4 |
|
return $this->delimiter; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Returns the current field enclosure |
|
78
|
|
|
* |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
110 |
|
public function getEnclosure(): string |
|
82
|
|
|
{ |
|
83
|
110 |
|
return $this->enclosure; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Returns the current field escape character |
|
88
|
|
|
* |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
4 |
|
public function getEscape(): string |
|
92
|
|
|
{ |
|
93
|
4 |
|
return $this->escape; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Returns the BOM sequence in use on Output methods |
|
98
|
|
|
* |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
2 |
|
public function getOutputBOM(): string |
|
102
|
|
|
{ |
|
103
|
2 |
|
return $this->output_bom; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Returns the BOM sequence of the given CSV |
|
108
|
|
|
* |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
142 |
|
public function getInputBOM(): string |
|
112
|
|
|
{ |
|
113
|
142 |
|
if (null === $this->input_bom) { |
|
114
|
|
|
$bom = [ |
|
115
|
142 |
|
AbstractCsv::BOM_UTF32_BE, AbstractCsv::BOM_UTF32_LE, |
|
116
|
|
|
AbstractCsv::BOM_UTF16_BE, AbstractCsv::BOM_UTF16_LE, AbstractCsv::BOM_UTF8, |
|
117
|
|
|
]; |
|
118
|
|
|
|
|
119
|
142 |
|
$this->document->setFlags(SplFileObject::READ_CSV); |
|
120
|
142 |
|
$this->document->rewind(); |
|
121
|
142 |
|
$line = $this->document->fgets(); |
|
122
|
142 |
|
$res = array_filter($bom, function ($sequence) use ($line) { |
|
123
|
142 |
|
return strpos($line, $sequence) === 0; |
|
124
|
142 |
|
}); |
|
125
|
|
|
|
|
126
|
142 |
|
$this->input_bom = (string) array_shift($res); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
142 |
|
return $this->input_bom; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Sets the field delimiter |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $delimiter |
|
136
|
|
|
* |
|
137
|
|
|
* @throws InvalidArgumentException If $delimiter is not a single character |
|
138
|
|
|
* |
|
139
|
|
|
* @return $this |
|
140
|
|
|
*/ |
|
141
|
208 |
|
public function setDelimiter(string $delimiter): self |
|
142
|
|
|
{ |
|
143
|
208 |
|
$this->delimiter = $this->filterControl($delimiter, 'delimiter'); |
|
144
|
|
|
|
|
145
|
208 |
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Sets the field enclosure |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $enclosure |
|
152
|
|
|
* |
|
153
|
|
|
* @throws InvalidArgumentException If $enclosure is not a single character |
|
154
|
|
|
* |
|
155
|
|
|
* @return $this |
|
156
|
|
|
*/ |
|
157
|
180 |
|
public function setEnclosure(string $enclosure): self |
|
158
|
|
|
{ |
|
159
|
180 |
|
$this->enclosure = $this->filterControl($enclosure, 'enclosure'); |
|
160
|
|
|
|
|
161
|
180 |
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Sets the field escape character |
|
166
|
|
|
* |
|
167
|
|
|
* @param string $escape |
|
168
|
|
|
* |
|
169
|
|
|
* @throws InvalidArgumentException If $escape is not a single character |
|
170
|
|
|
* |
|
171
|
|
|
* @return $this |
|
172
|
|
|
*/ |
|
173
|
180 |
|
public function setEscape(string $escape): self |
|
174
|
|
|
{ |
|
175
|
180 |
|
$this->escape = $this->filterControl($escape, 'escape'); |
|
176
|
|
|
|
|
177
|
180 |
|
return $this; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Sets the BOM sequence to prepend the CSV on output |
|
182
|
|
|
* |
|
183
|
|
|
* @param string $str The BOM sequence |
|
184
|
|
|
* |
|
185
|
|
|
* @return static |
|
186
|
|
|
*/ |
|
187
|
6 |
|
public function setOutputBOM(string $str): self |
|
188
|
|
|
{ |
|
189
|
6 |
|
$this->output_bom = $str; |
|
190
|
|
|
|
|
191
|
6 |
|
return $this; |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|