Livestatus\Query: resultrow method - base for more

This is where query-based column fixup voodoo starts
This commit is contained in:
Thomas Gelf 2014-11-16 16:35:02 +01:00
parent 628597d6cb
commit 6bf0ca216a
1 changed files with 80 additions and 0 deletions

View File

@ -58,6 +58,11 @@ class Query extends SimpleQuery
return $this->columns; return $this->columns;
} }
public function withHeaders(& $row)
{
return array_combine($this->preparedHeaders, $row->toArray());
}
/** /**
* Whether the named columns value is generated by a filter expression * Whether the named columns value is generated by a filter expression
*/ */
@ -66,6 +71,81 @@ class Query extends SimpleQuery
return array_key_exists($column, $this->filter_flags); return array_key_exists($column, $this->filter_flags);
} }
// completes a given row
public function resultRow(& $row)
{
// $row -> raw SplArray
// $res -> object
// $cv ->
// $result -> object to be returned
$result = (object) array();
$res = $this->withHeaders($row);
$cv = array();
if (array_key_exists('custom_variables', $res)) {
foreach ($this->parseArray($res['custom_variables']) as $cvp) {
$cv[$cvp[0]] = $cvp[1];
}
}
$combined = array();
foreach ($this->columns as $alias => $col) {
if (is_int($alias)) {
$alias = $col;
}
if ($col[0] === '_') {
$result->$alias = array_key_exists($alias, $cv) ? $cv[$alias] : null;
} else {
$func = 'mungeResult_' . $col;
if (method_exists($this, $func)) {
$this->$func($res[$this->available_columns[$col]], $result);
} elseif (is_array($this->available_columns[$col])) {
$combined[$alias] = $col;
$result->$alias = null;
} else {
if (strpos($this->available_columns[$col], ' ') === false) {
$result->$alias = $res[$this->available_columns[$col]];
} else {
$result->$alias = $res[$alias];
}
}
}
}
// TODO: Quite some redundancy here :(
if (! $this->filterIsSupported()) {
foreach ($this->filter->listFilteredColumns() as $col) {
if ($this->isFilterFlag($col)) {
$result->$col = (string) (int) $this->filterStringToFilter(
$this->filter_flags[$col]
)->matches((object) $res);
} else {
$func = 'combineResult_' . $col;
if (method_exists($this, $func)) {
$result->$col = $this->$func($result, $res);
}
}
}
}
foreach ($combined as $alias => $col) {
if ($this->isFilterFlag($col)) {
$result->$alias = (string) (int) $this->filterStringToFilter(
$this->filter_flags[$col]
)->matches((object) $res);
continue;
}
$func = 'combineResult_' . $col;
if (method_exists($this, $func)) {
$result->$alias = $this->$func($result, $res);
} else {
$result->$alias = implode(' - ', $this->available_columns[$col]);
}
}
return $result;
}
/** /**
* Parse the given encoded array * Parse the given encoded array
* *