summaryrefslogtreecommitdiffstats
path: root/vendor/gipfl/influxdb/src/InfluxDbQueryResult.php
blob: 0ca6fd1853ffad8a34faf0846d5ee632964e95ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php

namespace gipfl\InfluxDb;

use InvalidArgumentException;

class InfluxDbQueryResult
{
    public static function extractColumn($result, $idx = 0)
    {
        if (! isset($result->columns)) {
            print_r($result);
            exit;
        }
        $idx = static::getNumericColumn($idx, $result->columns);
        $column = [];
        foreach ($result->values as $row) {
            $column[] = $row[$idx];
        }

        return $column;
    }

    protected static function getNumericColumn($name, $cols)
    {
        if (\is_int($name)) {
            if (isset($cols[$name])) {
                return $name;
            }
        }
        if (\is_string($name)) {
            foreach ($cols as $idx => $alias) {
                if ($name === $alias) {
                    return $idx;
                }
            }
        }

        throw new InvalidArgumentException("There is no '$name' column in the result");
    }

    protected static function extractPairs($result, $keyColumn = 0, $valueColumn = 1)
    {
        $keyColumn = static::getNumericColumn($keyColumn, $result->columns);
        $valueColumn = static::getNumericColumn($valueColumn, $result->columns);
        $pairs = [];
        foreach ($result->values as $row) {
            $pairs[$row[$keyColumn]] = $row[$valueColumn];
        }

        return $pairs;
    }

    protected static function transformResultsTable($table)
    {
        // $table->name = 'databases'
        $cols = $table->columns;
        $values = [];
        foreach ($table->values as $row) {
            $values[] = (object) \array_combine($cols, $row);
        }

        return $values;
    }
}