ImportsourceHookTable: show malformed data...

...plus some namespacing/cleanup
This commit is contained in:
Thomas Gelf 2019-04-04 13:11:39 +02:00
parent 1027084f04
commit 8c04de0a14
3 changed files with 18 additions and 15 deletions

View File

@ -8,7 +8,7 @@ class Json
{ {
public static function encode($mixed, $flags = null) public static function encode($mixed, $flags = null)
{ {
$result = json_encode($mixed, $flags); $result = \json_encode($mixed, $flags);
if ($result === false && json_last_error() !== JSON_ERROR_NONE) { if ($result === false && json_last_error() !== JSON_ERROR_NONE) {
throw JsonEncodeException::forLastJsonError(); throw JsonEncodeException::forLastJsonError();
@ -19,7 +19,7 @@ class Json
public static function decode($string) public static function decode($string)
{ {
$result = json_decode($string); $result = \json_decode($string);
if ($result === null && json_last_error() !== JSON_ERROR_NONE) { if ($result === null && json_last_error() !== JSON_ERROR_NONE) {
throw JsonEncodeException::forLastJsonError(); throw JsonEncodeException::forLastJsonError();

View File

@ -9,24 +9,24 @@ class JsonException extends IcingaException
public static function forLastJsonError($msg = null) public static function forLastJsonError($msg = null)
{ {
if ($msg === null) { if ($msg === null) {
return new static(static::getJsonErrorMessage(json_last_error())); return new static(static::getJsonErrorMessage(\json_last_error()));
} else { } else {
$args = func_get_args(); $args = \func_get_args();
$args[0] = $msg . ': ' . static::getJsonErrorMessage(json_last_error()); $args[0] = $msg . ': ' . static::getJsonErrorMessage(\json_last_error());
return call_user_func_array('static::__construct', $args); return \call_user_func_array('static::__construct', $args);
} }
} }
public static function getJsonErrorMessage($code) public static function getJsonErrorMessage($code)
{ {
$map = [ $map = [
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded', JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded', JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON', JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON',
JSON_ERROR_SYNTAX => 'Syntax error', JSON_ERROR_SYNTAX => 'JSON Syntax error',
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded' JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded'
]; ];
if (array_key_exists($code, $map)) { if (\array_key_exists($code, $map)) {
return $map[$code]; return $map[$code];
} }
@ -36,7 +36,7 @@ class JsonException extends IcingaException
JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded', JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded',
JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given', JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given',
]; ];
if (array_key_exists($code, $map)) { if (\array_key_exists($code, $map)) {
return $map[$code]; return $map[$code];
} }
} }
@ -47,7 +47,7 @@ class JsonException extends IcingaException
JSON_ERROR_UTF16 => 'Malformed UTF-16 characters, possibly incorrectly encoded', JSON_ERROR_UTF16 => 'Malformed UTF-16 characters, possibly incorrectly encoded',
]; ];
if (array_key_exists($code, $map)) { if (\array_key_exists($code, $map)) {
return $map[$code]; return $map[$code];
} }
} }

View File

@ -65,12 +65,15 @@ class ImportsourceHookTable extends SimpleQueryBasedTable
if ($row === null) { if ($row === null) {
return null; return null;
} }
if (\is_array($row)) {
$row = (object) $row;
}
$tr = $this::tr(); $tr = $this::tr();
foreach ($this->getColumnsToBeRendered() as $column) { foreach ($this->getColumnsToBeRendered() as $column) {
$td = $this::td(); $td = $this::td();
if (property_exists($row, $column)) { if (\property_exists($row, $column)) {
if (is_string($row->$column) || $row->$column instanceof ValidHtml) { if (\is_string($row->$column) || $row->$column instanceof ValidHtml) {
$td->setContent($row->$column); $td->setContent($row->$column);
} else { } else {
$html = Html::tag('pre', null, PlainObjectRenderer::render($row->$column)); $html = Html::tag('pre', null, PlainObjectRenderer::render($row->$column));