|
1
|
|
|
<?php |
|
2
|
|
|
namespace Ivory\Relation; |
|
3
|
|
|
|
|
4
|
|
|
use Ivory\Type\IType; |
|
5
|
|
|
use Ivory\Type\ITypeDictionary; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Relation built from an array of rows. |
|
9
|
|
|
* |
|
10
|
|
|
* Immutable - once constructed, the data cannot be changed. |
|
11
|
|
|
*/ |
|
12
|
|
|
class ArrayRelation extends RelationBase |
|
13
|
|
|
{ |
|
14
|
|
|
private $cols; |
|
15
|
|
|
private $colNameMap; |
|
16
|
|
|
private $rows; |
|
17
|
|
|
private $numRows; |
|
18
|
|
|
|
|
19
|
|
|
public static function createAutodetect(array $rows, ITypeDictionary $typeDictionary) |
|
20
|
|
|
{ |
|
21
|
|
|
if (!$rows) { |
|
|
|
|
|
|
22
|
|
|
return new ArrayRelation([], []); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
$remaining = reset($rows); |
|
26
|
|
|
if (!is_array($remaining)) { |
|
27
|
|
|
throw new \InvalidArgumentException('$rows'); |
|
28
|
|
|
} |
|
29
|
|
|
$dataTypes = array_fill_keys(array_keys($remaining), null); |
|
30
|
|
|
foreach ($rows as $row) { |
|
31
|
|
|
foreach ($remaining as $colName => $_) { |
|
32
|
|
|
$val = ($row[$colName] ?? null); |
|
33
|
|
|
if ($val !== null) { |
|
34
|
|
|
$dataTypes[$colName] = $typeDictionary->requireTypeByValue($val); |
|
35
|
|
|
unset($remaining[$colName]); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if (!$remaining) { |
|
|
|
|
|
|
40
|
|
|
break; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return new ArrayRelation($rows, $dataTypes); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param array $rows list: map: column name => value |
|
49
|
|
|
* @param IType[] $dataTypes ordered map: column name => type |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function __construct(array $rows, array $dataTypes) |
|
52
|
|
|
{ |
|
53
|
|
|
parent::__construct(); |
|
54
|
|
|
|
|
55
|
|
|
$this->rows = $rows; |
|
56
|
|
|
$this->numRows = count($rows); |
|
57
|
|
|
$this->buildCols($dataTypes); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
private function buildCols(array $dataTypes) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->cols = []; |
|
63
|
|
|
$this->colNameMap = []; |
|
64
|
|
|
|
|
65
|
|
|
foreach ($dataTypes as $colName => $type) { |
|
66
|
|
|
$colOffset = count($this->cols); |
|
67
|
|
|
$col = new Column($this, $colOffset, $colName, $type); |
|
68
|
|
|
$this->cols[] = $col; |
|
69
|
|
|
$this->colNameMap[$colName] = $colOffset; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function getColumns() |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->cols; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function tuple(int $offset = 0): ITuple |
|
79
|
|
|
{ |
|
80
|
|
View Code Duplication |
if ($offset >= $this->numRows || $offset < -$this->numRows) { |
|
|
|
|
|
|
81
|
|
|
throw new \OutOfBoundsException("Offset $offset is out of the result bounds [0,{$this->numRows})"); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$effectiveOffset = ($offset >= 0 ? $offset : $this->numRows + $offset); |
|
85
|
|
|
if (!isset($this->rows[$effectiveOffset])) { |
|
86
|
|
|
throw new \RuntimeException("Error fetching row at offset $offset"); |
|
87
|
|
|
} |
|
88
|
|
|
$row = $this->rows[$effectiveOffset]; |
|
89
|
|
|
|
|
90
|
|
|
$data = []; |
|
91
|
|
|
foreach ($this->colNameMap as $colName => $_) { |
|
92
|
|
|
$data[] = ($row[$colName] ?? null); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return new Tuple($data, $this->cols, $this->colNameMap); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function count() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->numRows; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function populate() |
|
104
|
|
|
{ |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function flush() |
|
108
|
|
|
{ |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
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.