Skip to content

Commit f335368

Browse files
committed
Use CsvOptions in Reader
1 parent 12d65a6 commit f335368

2 files changed

Lines changed: 21 additions & 93 deletions

File tree

src/Reader.php

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,6 @@ class Reader implements \Iterator
1414
*/
1515
private $headers;
1616

17-
/**
18-
* The field delimiter (one character only).
19-
*
20-
* @var string
21-
*/
22-
private $delimiter;
23-
24-
/**
25-
* The field enclosure character (one character only).
26-
*
27-
* @var string
28-
*/
29-
private $enclosure;
30-
31-
/**
32-
* The escape character (one character only).
33-
*
34-
* @var string
35-
*/
36-
private $escapeChar;
37-
3817
/**
3918
* File pointer to the csv file.
4019
*
@@ -56,45 +35,32 @@ class Reader implements \Iterator
5635
*/
5736
private $current = null;
5837

38+
/**
39+
* @var CsvOptions
40+
*/
41+
private $csvOptions;
42+
5943
/**
6044
* Create a new Reader instance.
6145
*
62-
* @param string $file The full path to the csv file.
63-
* @param array $headers The column headers. If null, the headers will be derived from the first line in the
64-
* file.
65-
* @param string $delimiter The field delimiter (one character only).
66-
* @param string $enclosure The field enclosure character (one character only).
67-
* @param string $escapeChar The escape character (one character only).
46+
* @param string $file The full path to the csv file.
47+
* @param array $headers The column headers. If null, the headers will be derived from the first line in
48+
* the file.
49+
* @param CsvOptions $csvOptions Options for the csv file.
6850
*
6951
* @throws \InvalidArgumentException Thrown if $file is not readable.
70-
* @throws \InvalidArgumentException Thrown if $delimiter is a single character string.
71-
* @throws \InvalidArgumentException Thrown if $enclosure is a single character string.
72-
* @throws \InvalidArgumentException Thrown if $escapeChar is a single character string.
7352
*/
74-
public function __construct($file, array $headers = null, $delimiter = ',', $enclosure = '"', $escapeChar = '\\')
53+
public function __construct($file, array $headers = null, CsvOptions $csvOptions = null)
7554
{
7655
if (!is_readable((string)$file)) {
7756
throw new \InvalidArgumentException(
7857
'$file must be a string containing a full path to a readable delimited file'
7958
);
8059
}
8160

82-
if (strlen($delimiter) !== 1) {
83-
throw new \InvalidArgumentException('$delimiter must be a single character string');
84-
}
85-
86-
if (strlen($enclosure) !== 1) {
87-
throw new \InvalidArgumentException('$enclosure must be a single character string');
88-
}
89-
90-
if (strlen($escapeChar) !== 1) {
91-
throw new \InvalidArgumentException('$escapeChar must be a single character string');
92-
}
61+
$this->csvOptions = $csvOptions ?? new CsvOptions();
9362

9463
$this->headers = $headers;
95-
$this->delimiter = $delimiter;
96-
$this->enclosure = $enclosure;
97-
$this->escapeChar = $escapeChar;
9864
$this->handle = fopen((string)$file, 'r');
9965
}
10066

@@ -140,7 +106,13 @@ public function next()
140106
*/
141107
private function readLine()
142108
{
143-
$raw = fgetcsv($this->handle, 0, $this->delimiter, $this->enclosure, $this->escapeChar);
109+
$raw = fgetcsv(
110+
$this->handle,
111+
0,
112+
$this->csvOptions->getDelimiter(),
113+
$this->csvOptions->getEnclosure(),
114+
$this->csvOptions->getEscapeChar()
115+
);
144116
if (empty($raw)) {
145117
throw new \Exception('Empty line read');
146118
}

tests/ReaderTest.php

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace SubjectivePHPTest\Csv;
33

4+
use SubjectivePHP\Csv\CsvOptions;
45
use SubjectivePHP\Csv\Reader;
56
use PHPUnit\Framework\TestCase;
67

@@ -92,8 +93,8 @@ public function getReaders()
9293
[new Reader(__DIR__ . '/_files/basic.csv')],
9394
[new Reader(__DIR__ . '/_files/basic.csv', $headers)],
9495
[new Reader(__DIR__ . '/_files/no_headers.csv', $headers)],
95-
[new Reader(__DIR__ . '/_files/pipe_delimited.txt', $headers, '|')],
96-
[new Reader(__DIR__ . '/_files/tab_delimited.txt', $headers, "\t")],
96+
[new Reader(__DIR__ . '/_files/pipe_delimited.txt', $headers, new CsvOptions('|'))],
97+
[new Reader(__DIR__ . '/_files/tab_delimited.txt', $headers, new CsvOptions("\t"))],
9798
];
9899
}
99100

@@ -130,51 +131,6 @@ public function getFiles()
130131
];
131132
}
132133

133-
/**
134-
* Verify behavior of __construct with an invalid delimiter.
135-
*
136-
* @test
137-
* @covers ::__construct
138-
* @expectedException \InvalidArgumentException
139-
* @expectedExceptionMessage $delimiter must be a single character string
140-
*
141-
* @return void
142-
*/
143-
public function constructInvalidDelimiter()
144-
{
145-
new Reader(__DIR__ . '/_files/basic.csv', null, 'too long');
146-
}
147-
148-
/**
149-
* Verify behavior of __construct with an invalid enclosure.
150-
*
151-
* @test
152-
* @covers ::__construct
153-
* @expectedException \InvalidArgumentException
154-
* @expectedExceptionMessage $enclosure must be a single character string
155-
*
156-
* @return void
157-
*/
158-
public function constructInvalidEnclosure()
159-
{
160-
new Reader(__DIR__ . '/_files/basic.csv', null, ',', 123);
161-
}
162-
163-
/**
164-
* Verify behavior of __construct with an invalid escapeChar.
165-
*
166-
* @test
167-
* @covers ::__construct
168-
* @expectedException \InvalidArgumentException
169-
* @expectedExceptionMessage $escapeChar must be a single character string
170-
*
171-
* @return void
172-
*/
173-
public function constructInvalidEscapeChar()
174-
{
175-
new Reader(__DIR__ . '/_files/basic.csv', null, ',', '"', null);
176-
}
177-
178134
/**
179135
* Verify behaviour of consecutive rewind().
180136
*

0 commit comments

Comments
 (0)