Passed
Push — master ( 288a96...964b09 )
by Daniel
10:30
created

Query::rewind()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ORM\Connect;
4
5
use SilverStripe\Core\Convert;
6
use Iterator;
7
8
/**
9
 * Abstract query-result class.
10
 * Once again, this should be subclassed by an actual database implementation.  It will only
11
 * ever be constructed by a subclass of SS_Database.  The result of a database query - an iteratable object
12
 * that's returned by DB::SS_Query
13
 *
14
 * Primarily, the SS_Query class takes care of the iterator plumbing, letting the subclasses focusing
15
 * on providing the specific data-access methods that are required: {@link nextRecord()}, {@link numRecords()}
16
 * and {@link seek()}
17
 */
18
abstract class Query implements \IteratorAggregate
19
{
20
21
    /**
22
     * Return an array containing all the values from a specific column. If no column is set, then the first will be
23
     * returned
24
     *
25
     * @param string $column
26
     * @return array
27
     */
28
    public function column($column = null)
29
    {
30
        $result = [];
31
32
        foreach ($this as $record) {
33
            if ($column) {
34
                $result[] = $record[$column];
35
            } else {
36
                $result[] = $record[key($record)];
37
            }
38
        }
39
40
        return $result;
41
    }
42
43
    /**
44
     * Return an array containing all values in the leftmost column, where the keys are the
45
     * same as the values.
46
     *
47
     * @return array
48
     */
49
    public function keyedColumn()
50
    {
51
        $column = [];
52
53
        foreach ($this as $record) {
54
            $val = $record[key($record)];
55
            $column[$val] = $val;
56
        }
57
        return $column;
58
    }
59
60
    /**
61
     * Return a map from the first column to the second column.
62
     *
63
     * @return array
64
     */
65
    public function map()
66
    {
67
        $column = array();
68
        foreach ($this as $record) {
69
            $key = reset($record);
70
            $val = next($record);
71
            $column[$key] = $val;
72
        }
73
        return $column;
74
    }
75
76
    /**
77
     * Returns the first record in the result
78
     *
79
     * @return array
80
     */
81
    public function record()
82
    {
83
        return $this->getIterator()->current();
84
    }
85
86
    /**
87
     * @deprecated Use record() instead
88
     * @return array
89
     */
90
    public function first()
91
    {
92
        return $this->record();
93
    }
94
95
    /**
96
     * Returns the first column of the first record.
97
     *
98
     * @return string
99
     */
100
    public function value()
101
    {
102
        $record = $this->record();
103
        if ($record) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $record of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
104
            return $record[key($record)];
105
        }
106
        return null;
107
    }
108
109
    /**
110
     * Return an HTML table containing the full result-set
111
     *
112
     * @return string
113
     */
114
    public function table()
115
    {
116
        $first = true;
117
        $result = "<table>\n";
118
119
        foreach ($this as $record) {
120
            if ($first) {
121
                $result .= "<tr>";
122
                foreach ($record as $k => $v) {
123
                    $result .= "<th>" . Convert::raw2xml($k) . "</th> ";
124
                }
125
                $result .= "</tr> \n";
126
            }
127
128
            $result .= "<tr>";
129
            foreach ($record as $k => $v) {
130
                $result .= "<td>" . Convert::raw2xml($v) . "</td> ";
131
            }
132
            $result .= "</tr> \n";
133
134
            $first = false;
135
        }
136
        $result .= "</table>\n";
137
138
        if ($first) {
139
            return "No records found";
140
        }
141
        return $result;
142
    }
143
144
    /**
145
     * Return the next record in the query result.
146
     *
147
     * @return array
148
     */
149
    abstract public function getIterator();
150
151
    /**
152
     * Return the total number of items in the query result.
153
     *
154
     * @return int
155
     */
156
    abstract public function numRecords();
157
}
158