Skip to content

Commit 12d65a6

Browse files
committed
Add CsvOptions
1 parent 87e92a6 commit 12d65a6

2 files changed

Lines changed: 156 additions & 0 deletions

File tree

src/CsvOptions.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
}

tests/CsvOptionsTest.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace SubjectivePHPTest\Csv;
4+
5+
use SubjectivePHP\Csv\CsvOptions;
6+
use PHPUnit\Framework\TestCase;
7+
8+
/**
9+
* @coversDefaultClass \SubjectivePHP\Csv\CsvOptions
10+
* @covers ::__construct
11+
*/
12+
final class CsvOptionsTest extends TestCase
13+
{
14+
/**
15+
* @test
16+
* @covers ::__construct
17+
* @expectedException \InvalidArgumentException
18+
* @expectedExceptionMessage $delimiter must be a single character string
19+
*/
20+
public function constructWithDelimiterGreaterThanOneCharacter()
21+
{
22+
new CsvOptions('too long');
23+
}
24+
25+
/**
26+
* @test
27+
* @covers ::__construct
28+
* @expectedException \InvalidArgumentException
29+
* @expectedExceptionMessage $enclosure must be a single character string
30+
*/
31+
public function constructWithEnclosureGreaterThanOneCharacter()
32+
{
33+
new CsvOptions(',', '##');
34+
}
35+
36+
/**
37+
* @test
38+
* @covers ::__construct
39+
* @expectedException \InvalidArgumentException
40+
* @expectedExceptionMessage $escapeChar must be a single character string
41+
*/
42+
public function constructWithEscapeCharGreaterThanOneCharacter()
43+
{
44+
new CsvOptions(',', '"', '\\\\');
45+
}
46+
47+
/**
48+
* @test
49+
* @covers ::getDelimiter
50+
*/
51+
public function getDelimiter()
52+
{
53+
$this->assertSame(',', (new CsvOptions(',', '"', '\\'))->getDelimiter());
54+
}
55+
56+
/**
57+
* @test
58+
* @covers ::getEnclosure
59+
*/
60+
public function getEnclosure()
61+
{
62+
$this->assertSame('"', (new CsvOptions(',', '"', '\\'))->getEnclosure());
63+
}
64+
65+
/**
66+
* @test
67+
* @covers ::getEscapeChar
68+
*/
69+
public function getEscapeChar()
70+
{
71+
$this->assertSame('\\', (new CsvOptions(',', '"', '\\'))->getEscapeChar());
72+
}
73+
}

0 commit comments

Comments
 (0)