Skip to content

Commit 57b5f2b

Browse files
committed
Fix encodings
1 parent 4bd3aa4 commit 57b5f2b

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

src/CsvFile.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Enhanced CSV file object
88
*/
99
class CsvFile extends File implements \Countable {
10-
private $file_path;
10+
const DEFAULT_ENCODING = 'utf-8';
1111
private $encoding = 'utf-8';
1212
private $is_head_row = false;
1313
/**
@@ -20,17 +20,16 @@ class CsvFile extends File implements \Countable {
2020
private $start_column = 0;
2121
private $columns_to_skip = array();
2222

23-
function __construct($file_path, $delimiter = ',', $enclosure = '"', $escape = "\\") {
24-
if (!file_exists($file_path)) {
25-
throw new Exception("File $file_path does not exist. ");
23+
function __construct($file, $delimiter = ',', $enclosure = '"', $escape = "\\") {
24+
if (!file_exists($file)) {
25+
throw new Exception("File $file does not exist. ");
2626
}
2727

28-
if (filesize($file_path) === 0) {
28+
if (filesize($file) === 0) {
2929
throw new Exception("Empty file. ");
3030
}
3131

32-
$this->file_path = $file_path;
33-
parent::__construct($file_path, 'r');
32+
parent::__construct($file, 'r+');
3433
$this->setFlags(File::READ_CSV | File::READ_AHEAD | File::SKIP_EMPTY | File::DROP_NEW_LINE);
3534
$this->setCsvControl($delimiter, $enclosure, $escape);
3635
}
@@ -43,6 +42,10 @@ function count() {
4342
return count($this->to_array());
4443
}
4544

45+
function set_encoding($encoding) {
46+
$this->encoding = $encoding;
47+
}
48+
4649
public function to_array() {
4750
$rows = [];
4851
foreach ($this as $row) {
@@ -93,7 +96,14 @@ private function remove_columns($old_row) {
9396
return $new_row;
9497
}
9598

99+
function convert_to_default_encoding($val) {
100+
return mb_convert_encoding($val, static::DEFAULT_ENCODING, $this->encoding);
101+
}
102+
96103
private function format_row($row) {
104+
if ($this->encoding !== static::DEFAULT_ENCODING) {
105+
$row = array_map([$this, 'convert_to_default_encoding'], $row);
106+
}
97107
$row = array_combine(
98108
$this->get_column_names($row),
99109
$row

tests/CsvFileTest.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ function test_it_can_be_constructed_from_a_file() {
1010
$csv = new CsvFile(__DIR__ . '/../sample-data/info.csv');
1111
$this->assertEquals(5, count($csv));
1212
}
13-
1413
/**
1514
* @expectedException \Carbon_CSV\Exception
1615
*/
@@ -227,9 +226,6 @@ function test_throws_exception_when_using_non_number_characters_for_skip_to_colu
227226
$csv = new CsvFile(__DIR__ . '/../sample-data/info.csv' );
228227
$csv->skip_to_column('a');
229228
}
230-
/**
231-
* @skip
232-
*/
233229
function test_non_utf8_encoded_file() {
234230
$csv = new CsvFile(__DIR__ . '/../sample-data/cp1251.csv');
235231
$csv->set_encoding('cp-1251');
@@ -240,17 +236,11 @@ function test_non_utf8_encoded_file() {
240236
]);
241237
$this->assertEquals( [
242238
[
243-
'last_name' => 'Doe'
239+
'name' => 'Хоумър',
240+
'age' => '31',
244241
],
245242
], $csv->to_array() );
246243
}
244+
247245

248-
function test_bad_mappings_are_providing_sane_error() {
249-
$csv = new CsvFile(__DIR__ . '/../sample-data/cp1251.csv');
250-
$csv->use_first_row_as_header();
251-
$csv->set_column_names([
252-
0 => 'name',
253-
1 => 'age',
254-
]);
255-
}
256246
}

0 commit comments

Comments
 (0)