2013-07-12 12:02:51 +02:00
|
|
|
<?php
|
2014-04-23 14:40:39 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-07-12 12:02:51 +02:00
|
|
|
|
|
|
|
namespace Icinga\File;
|
|
|
|
|
2014-06-21 00:09:11 +02:00
|
|
|
use Icinga\Data\Browsable;
|
2013-07-12 12:02:51 +02:00
|
|
|
|
|
|
|
class Csv
|
|
|
|
{
|
|
|
|
protected $query;
|
|
|
|
|
2014-05-07 13:32:06 +02:00
|
|
|
protected function __construct() {}
|
2013-07-12 12:02:51 +02:00
|
|
|
|
2014-06-21 00:09:11 +02:00
|
|
|
public static function fromQuery(Browsable $query)
|
2013-07-12 12:02:51 +02:00
|
|
|
{
|
|
|
|
$csv = new Csv();
|
|
|
|
$csv->query = $query;
|
|
|
|
return $csv;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dump()
|
|
|
|
{
|
|
|
|
header('Content-type: text/csv');
|
2013-09-04 18:27:16 +02:00
|
|
|
echo (string) $this;
|
2013-07-12 12:02:51 +02:00
|
|
|
}
|
2013-09-04 18:27:16 +02:00
|
|
|
|
2013-07-12 12:02:51 +02:00
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
$first = true;
|
|
|
|
$csv = '';
|
2014-06-21 00:09:11 +02:00
|
|
|
foreach ($this->query->getQuery()->fetchAll() as $row) {
|
2013-07-12 12:02:51 +02:00
|
|
|
if ($first) {
|
|
|
|
$csv .= implode(',', array_keys((array) $row)) . "\r\n";
|
|
|
|
$first = false;
|
|
|
|
}
|
|
|
|
$out = array();
|
|
|
|
foreach ($row as & $val) {
|
|
|
|
$out[] = '"' . $val . '"';
|
|
|
|
}
|
|
|
|
$csv .= implode(',', $out) . "\r\n";
|
|
|
|
}
|
2014-04-23 14:40:39 +02:00
|
|
|
|
2013-07-12 12:02:51 +02:00
|
|
|
return $csv;
|
|
|
|
}
|
|
|
|
}
|