Make CSV export compatible with PHP 5.3
This commit is contained in:
@@ -29,6 +29,7 @@ Bug fixes:
|
|||||||
* Fix PHP error when adding a new user with email notification enabled
|
* Fix PHP error when adding a new user with email notification enabled
|
||||||
* Add missing template for activity stream to show event "file.create"
|
* Add missing template for activity stream to show event "file.create"
|
||||||
* Fix wrong value for PLUGINS_DIR in config.default.php
|
* Fix wrong value for PLUGINS_DIR in config.default.php
|
||||||
|
* Make CSV export compatible with PHP 5.3
|
||||||
|
|
||||||
Version 1.0.20
|
Version 1.0.20
|
||||||
--------------
|
--------------
|
||||||
|
|||||||
@@ -93,8 +93,7 @@ class Csv
|
|||||||
{
|
{
|
||||||
if (! empty($value)) {
|
if (! empty($value)) {
|
||||||
$value = trim(strtolower($value));
|
$value = trim(strtolower($value));
|
||||||
return $value === '1' || $value{0}
|
return $value === '1' || $value{0} === 't' ? 1 : 0;
|
||||||
=== 't' ? 1 : 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -164,10 +163,14 @@ class Csv
|
|||||||
*/
|
*/
|
||||||
public function write($filename, array $rows)
|
public function write($filename, array $rows)
|
||||||
{
|
{
|
||||||
$file = new SplFileObject($filename, 'w');
|
$fp = fopen($filename, 'w');
|
||||||
|
|
||||||
foreach ($rows as $row) {
|
if (is_resource($fp)) {
|
||||||
$file->fputcsv($row, $this->delimiter, $this->enclosure);
|
foreach ($rows as $row) {
|
||||||
|
fputcsv($fp, $row, $this->delimiter, $this->enclosure);
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose($fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
|||||||
@@ -19,4 +19,34 @@ class CsvTest extends Base
|
|||||||
$this->assertEquals(0, Csv::getBooleanValue('123'));
|
$this->assertEquals(0, Csv::getBooleanValue('123'));
|
||||||
$this->assertEquals(0, Csv::getBooleanValue('anything'));
|
$this->assertEquals(0, Csv::getBooleanValue('anything'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testGetEnclosures()
|
||||||
|
{
|
||||||
|
$this->assertCount(3, Csv::getEnclosures());
|
||||||
|
$this->assertCount(4, Csv::getDelimiters());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testReadWrite()
|
||||||
|
{
|
||||||
|
$filename = tempnam(sys_get_temp_dir(), 'UT');
|
||||||
|
$rows = array(
|
||||||
|
array('Column A', 'Column B'),
|
||||||
|
array('value a', 'value b'),
|
||||||
|
);
|
||||||
|
|
||||||
|
$csv = new Csv;
|
||||||
|
$csv->write($filename, $rows);
|
||||||
|
$csv->setColumnMapping(array('A', 'B', 'C'));
|
||||||
|
$csv->read($filename, array($this, 'readRow'));
|
||||||
|
|
||||||
|
unlink($filename);
|
||||||
|
|
||||||
|
$this->expectOutputString('"Column A","Column B"'.PHP_EOL.'"value a","value b"'.PHP_EOL, $csv->output($rows));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function readRow(array $row, $line)
|
||||||
|
{
|
||||||
|
$this->assertEquals(array('value a', 'value b', ''), $row);
|
||||||
|
$this->assertEquals(1, $line);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user