Skip to content

Commit

Permalink
Fix key-value formatting for json and csv result set
Browse files Browse the repository at this point in the history
Remove parameter type for second argument (`$value`) in `CsvResultSet::formatValue` and `JsonResultSet::formatValue`.
Also add date time formatting for `$value` parameter in case it is an instance of `DateTime` object.
  • Loading branch information
raviks789 committed Jun 20, 2023
1 parent 1756065 commit fbcda59
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 6 additions & 1 deletion library/Icingadb/Data/CsvResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Icinga\Module\Icingadb\Data;

use DateTime;
use DateTimeZone;
use ipl\Orm\Model;
use ipl\Orm\Query;
use ipl\Orm\ResultSet;
Expand All @@ -17,7 +19,7 @@ public function current()
return $this->extractKeysAndValues(parent::current());
}

protected function formatValue(string $key, ?string $value): ?string
protected function formatValue(string $key, $value): ?string
{
if (
$value
Expand All @@ -38,6 +40,9 @@ protected function formatValue(string $key, ?string $value): ?string
return '"' . str_replace('"', '""', $value) . '"';
} elseif (is_array($value)) {
return '"' . implode(',', $value) . '"';
} elseif ($value instanceof DateTime) {
return $value->setTimezone(new DateTimeZone('UTC'))
->format('Y-m-d\TH:i:s.vP');
} else {
return $value;
}
Expand Down
9 changes: 8 additions & 1 deletion library/Icingadb/Data/JsonResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Icinga\Module\Icingadb\Data;

use DateTime;
use DateTimeZone;
use Icinga\Util\Json;
use ipl\Orm\Model;
use ipl\Orm\Query;
Expand All @@ -18,7 +20,7 @@ public function current()
return $this->createObject(parent::current());
}

protected function formatValue(string $key, ?string $value): ?string
protected function formatValue(string $key, $value): ?string
{
if (
$value
Expand All @@ -33,6 +35,11 @@ protected function formatValue(string $key, ?string $value): ?string
$value = bin2hex($value);
}

if ($value instanceof DateTime) {
return $value->setTimezone(new DateTimeZone('UTC'))
->format('Y-m-d\TH:i:s.vP');
}

return $value;
}

Expand Down

0 comments on commit fbcda59

Please sign in to comment.