Skip to content

Commit

Permalink
Fix encodings
Browse files Browse the repository at this point in the history
  • Loading branch information
emohamed committed Sep 15, 2017
1 parent 4bd3aa4 commit 57b5f2b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
24 changes: 17 additions & 7 deletions src/CsvFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Enhanced CSV file object
*/
class CsvFile extends File implements \Countable {
private $file_path;
const DEFAULT_ENCODING = 'utf-8';
private $encoding = 'utf-8';
private $is_head_row = false;
/**
Expand All @@ -20,17 +20,16 @@ class CsvFile extends File implements \Countable {
private $start_column = 0;
private $columns_to_skip = array();

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

if (filesize($file_path) === 0) {
if (filesize($file) === 0) {
throw new Exception("Empty file. ");
}

$this->file_path = $file_path;
parent::__construct($file_path, 'r');
parent::__construct($file, 'r+');
$this->setFlags(File::READ_CSV | File::READ_AHEAD | File::SKIP_EMPTY | File::DROP_NEW_LINE);
$this->setCsvControl($delimiter, $enclosure, $escape);
}
Expand All @@ -43,6 +42,10 @@ function count() {
return count($this->to_array());
}

function set_encoding($encoding) {
$this->encoding = $encoding;
}

public function to_array() {
$rows = [];
foreach ($this as $row) {
Expand Down Expand Up @@ -93,7 +96,14 @@ private function remove_columns($old_row) {
return $new_row;
}

function convert_to_default_encoding($val) {
return mb_convert_encoding($val, static::DEFAULT_ENCODING, $this->encoding);
}

private function format_row($row) {
if ($this->encoding !== static::DEFAULT_ENCODING) {
$row = array_map([$this, 'convert_to_default_encoding'], $row);
}
$row = array_combine(
$this->get_column_names($row),
$row
Expand Down
16 changes: 3 additions & 13 deletions tests/CsvFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ function test_it_can_be_constructed_from_a_file() {
$csv = new CsvFile(__DIR__ . '/../sample-data/info.csv');
$this->assertEquals(5, count($csv));
}

/**
* @expectedException \Carbon_CSV\Exception
*/
Expand Down Expand Up @@ -227,9 +226,6 @@ function test_throws_exception_when_using_non_number_characters_for_skip_to_colu
$csv = new CsvFile(__DIR__ . '/../sample-data/info.csv' );
$csv->skip_to_column('a');
}
/**
* @skip
*/
function test_non_utf8_encoded_file() {
$csv = new CsvFile(__DIR__ . '/../sample-data/cp1251.csv');
$csv->set_encoding('cp-1251');
Expand All @@ -240,17 +236,11 @@ function test_non_utf8_encoded_file() {
]);
$this->assertEquals( [
[
'last_name' => 'Doe'
'name' => 'Хоумър',
'age' => '31',
],
], $csv->to_array() );
}


function test_bad_mappings_are_providing_sane_error() {
$csv = new CsvFile(__DIR__ . '/../sample-data/cp1251.csv');
$csv->use_first_row_as_header();
$csv->set_column_names([
0 => 'name',
1 => 'age',
]);
}
}

0 comments on commit 57b5f2b

Please sign in to comment.