Skip to content

Commit

Permalink
Merge pull request #18 from jabranr/bugfix-trim-spaces
Browse files Browse the repository at this point in the history
Bugfix trim spaces
  • Loading branch information
jabranr authored Feb 10, 2017
2 parents fdfac10 + 78743c5 commit 0fc71b1
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jabranr/csv-parser",
"type": "library",
"version": "2.1.0",
"version": "2.1.1",
"description": "Parse CSV data from a file, stream or string",
"keywords": ["csv", "php", "parser"],
"homepage": "https://github.com/jabranr/csv-parser",
Expand Down
25 changes: 23 additions & 2 deletions src/CSV_Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Parse CSV data from a file, path, stream, resource or string
*
* @author: Jabran Rafique <[email protected]>
* @version: 2.1.0
* @version: 2.1.1
* @license: MIT License
* @link: https://github.com/jabranr/csv-parser
*/
Expand Down Expand Up @@ -136,6 +136,7 @@ public function setHeaders($headers = null) {
throw new InvalidDataException('Unexpected headers data.');
}

$headers = $this->trimRecursively($headers);
$this->headers = $headers;
return $this;
}
Expand Down Expand Up @@ -166,10 +167,29 @@ public function setColumns($columns = null) {
throw new InvalidDataException('Unexpected columns data.');
}

$columns = $this->trimRecursively($columns);
$this->columns = $columns;
return $this;
}

/**
* Trim data recursively
*
* @param mixed $data
* @return mixed
*/
public function trimRecursively($data) {
if (!is_string($data) && !is_array($data)) {
return $data;
}

if (is_string($data)) {
return trim($data);
}

return array_map(array($this, 'trimRecursively'), $data);
}

/**
* Get columns
*
Expand All @@ -196,6 +216,7 @@ public function setRows($rows = null) {
throw new InvalidDataException('Unexpected rows data.');
}

$rows = $this->trimRecursively($rows);
$this->rows = $rows;
return $this;
}
Expand Down Expand Up @@ -394,7 +415,7 @@ private function _makeRowsWithHeaders() {
// Ignore rows that do not have same length
if ( count($this->getHeaders()) !== count($row) ) {
continue;
}
}

$rowsWithHeader[] = array_combine($this->getHeaders(), $row);
}
Expand Down
49 changes: 45 additions & 4 deletions test/CSV_ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function testSetEncoding_ArgumentAsFloat() {
public function testSetEncoding_ArgumentAsBoolean() {
return $this->csv->setEncoding(true);
}

/**
* Test setEncoding method
* @expectedException Jabran\Exception\InvalidEncodingException
Expand Down Expand Up @@ -738,6 +738,47 @@ public function testFromString_DataType() {
return $this->assertEquals('string', gettype($this->csv->getData()));
}

/**
* ---------------------------------------------------------------
* Jabran\CSV_Parser::trimRecursively tests
* ---------------------------------------------------------------
*/

/**
* Test trim recursively for an integer
* @depends testFromString_DataType
*/
public function testTrimRecursivelyInteger() {
$trimmed = $this->csv->trimRecursively(1);
return $this->assertEquals(1, $trimmed);
}

/**
* Test trim recursively for a string
* @depends testFromString_DataType
*/
public function testTrimRecursivelyString() {
$trimmed = $this->csv->trimRecursively(' foo');
return $this->assertEquals('foo', $trimmed);
}

/**
* Test trim recursively for an array
* @depends testFromString_DataType
*/
public function testTrimRecursivelyArray() {
$trimmed = $this->csv->trimRecursively(array(' foo', ' bar', ' baz'));
return $this->assertEquals(array('foo', 'bar', 'baz'), $trimmed);
}

/**
* Test trim recursively for associative array
* @depends testFromString_DataType
*/
public function testTrimRecursivelyAssociativeArray() {
$trimmed = $this->csv->trimRecursively(array('foo' => ' bar', ' baz' => ' meow'));
return $this->assertEquals(array('foo' => 'bar', ' baz' => 'meow'), $trimmed);
}

/**
* ---------------------------------------------------------------
Expand Down Expand Up @@ -904,7 +945,7 @@ public function testConvertEncoding_ASCIIToUTF8() {
$this->csv->fromString($this->sampleInput);
$this->csv->encode();
return $this->assertEquals(
$this->csv->getEncoding(),
$this->csv->getEncoding(),
mb_detect_encoding($this->csv->getData())
);
}
Expand All @@ -917,7 +958,7 @@ public function testConvertEncoding_UTF8ToASCII() {
$this->csv->setEncoding('ASCII');
$this->csv->encode();
return $this->assertEquals(
$this->csv->getEncoding(),
$this->csv->getEncoding(),
mb_detect_encoding($this->csv->getData())
);
}
Expand All @@ -931,7 +972,7 @@ public function testConvertEncoding_UTF8ToUTF16() {
$this->csv->setEncoding('UTF-16');
$this->csv->encode();
return $this->assertEquals(
$this->csv->getEncoding(),
$this->csv->getEncoding(),
mb_detect_encoding($this->csv->getData())
);
}
Expand Down

0 comments on commit 0fc71b1

Please sign in to comment.