|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SubjectivePHP\Csv; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
final class CsvOptions |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var string |
|
11
|
|
|
*/ |
|
12
|
|
|
private $delimiter; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
private $enclosure; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
private $escapeChar; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Construct a new CsvOptions instance. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $delimiter The field delimiter (one character only). |
|
28
|
|
|
* @param string $enclosure The field enclosure character (one character only). |
|
29
|
|
|
* @param string $escapeChar The escape character (one character only). |
|
30
|
|
|
* |
|
31
|
|
|
* @throws InvalidArgumentException Thrown if $delimiter is not one character. |
|
32
|
|
|
* @throws InvalidArgumentException Thrown if $enclosure is not one character. |
|
33
|
|
|
* @throws InvalidArgumentException Thrown if $escapeChar is not one character. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(string $delimiter = ',', string $enclosure = '"', string $escapeChar = '\\') |
|
36
|
|
|
{ |
|
37
|
|
|
if (strlen($delimiter) !== 1) { |
|
38
|
|
|
throw new InvalidArgumentException('$delimiter must be a single character string'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if (strlen($enclosure) !== 1) { |
|
42
|
|
|
throw new InvalidArgumentException('$enclosure must be a single character string'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if (strlen($escapeChar) !== 1) { |
|
46
|
|
|
throw new InvalidArgumentException('$escapeChar must be a single character string'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$this->delimiter = $delimiter; |
|
50
|
|
|
$this->enclosure = $enclosure; |
|
51
|
|
|
$this->escapeChar = $escapeChar; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Gets the field delimiter (one character only). |
|
56
|
|
|
* |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getDelimiter() : string |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->delimiter; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Gets the field enclosure character (one character only). |
|
66
|
|
|
* |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getEnclosure() : string |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->enclosure; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Gets the escape character (one character only). |
|
76
|
|
|
* |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getEscapeChar() : string |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->escapeChar; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|