Fix PHP Fatal error "Method Icinga\File\Csv::__toString() must not throw an exception in /vagrant/test/php/library/Icinga/File/CsvTest.php on line 35"
This commit is contained in:
parent
1532594948
commit
0065cbe998
|
@ -5,6 +5,7 @@
|
||||||
namespace Icinga\File;
|
namespace Icinga\File;
|
||||||
|
|
||||||
use Icinga\Data\Browsable;
|
use Icinga\Data\Browsable;
|
||||||
|
use Exception;
|
||||||
|
|
||||||
class Csv
|
class Csv
|
||||||
{
|
{
|
||||||
|
@ -27,20 +28,24 @@ class Csv
|
||||||
|
|
||||||
public function __toString()
|
public function __toString()
|
||||||
{
|
{
|
||||||
$first = true;
|
try {
|
||||||
$csv = '';
|
$first = true;
|
||||||
foreach ($this->query->getQuery()->fetchAll() as $row) {
|
$csv = '';
|
||||||
if ($first) {
|
foreach ($this->query->getQuery()->fetchAll() as $row) {
|
||||||
$csv .= implode(',', array_keys((array) $row)) . "\r\n";
|
if ($first) {
|
||||||
$first = false;
|
$csv .= implode(',', array_keys((array) $row)) . "\r\n";
|
||||||
|
$first = false;
|
||||||
|
}
|
||||||
|
$out = array();
|
||||||
|
foreach ($row as & $val) {
|
||||||
|
$out[] = '"' . $val . '"';
|
||||||
|
}
|
||||||
|
$csv .= implode(',', $out) . "\r\n";
|
||||||
}
|
}
|
||||||
$out = array();
|
|
||||||
foreach ($row as & $val) {
|
|
||||||
$out[] = '"' . $val . '"';
|
|
||||||
}
|
|
||||||
$csv .= implode(',', $out) . "\r\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
return $csv;
|
return $csv;
|
||||||
|
} catch (Exception $e) {
|
||||||
|
return (string) $e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue