1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Licensed to CRATE Technology GmbH("Crate") under one or more contributor |
4
|
|
|
* license agreements. See the NOTICE file distributed with this work for |
5
|
|
|
* additional information regarding copyright ownership. Crate licenses |
6
|
|
|
* this file to you under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. You may |
8
|
|
|
* obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
15
|
|
|
* License for the specific language governing permissions and limitations |
16
|
|
|
* under the License. |
17
|
|
|
* |
18
|
|
|
* However, if you have executed another commercial license agreement |
19
|
|
|
* with Crate these terms will supersede the license and you may use the |
20
|
|
|
* software solely pursuant to the terms of the relevant commercial agreement. |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
declare(strict_types=1); |
24
|
|
|
|
25
|
|
|
namespace Crate\Stdlib; |
26
|
|
|
|
27
|
|
|
final class Collection implements CollectionInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $rows; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string[] |
36
|
|
|
*/ |
37
|
|
|
private $columnsAsKeys; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string[] |
41
|
|
|
*/ |
42
|
|
|
private $columnsAsValues; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var int |
46
|
|
|
*/ |
47
|
|
|
private $duration; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var int |
51
|
|
|
*/ |
52
|
|
|
private $rowCount; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param array $rows |
56
|
|
|
* @param string[] $columns |
57
|
|
|
* @param int $duration |
58
|
|
|
* @param int $rowCount |
59
|
|
|
*/ |
60
|
24 |
|
public function __construct(array $rows, array $columns, $duration, $rowCount) |
61
|
|
|
{ |
62
|
24 |
|
$this->rows = $rows; |
63
|
24 |
|
$this->columnsAsKeys = array_flip($columns); |
64
|
24 |
|
$this->columnsAsValues = $columns; |
65
|
24 |
|
$this->duration = $duration; |
66
|
24 |
|
$this->rowCount = $rowCount; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@Inheritdoc} |
71
|
|
|
*/ |
72
|
2 |
|
public function map(callable $callback) |
73
|
|
|
{ |
74
|
2 |
|
return array_map($callback, $this->rows); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@Inheritdoc} |
79
|
|
|
*/ |
80
|
2 |
|
public function getColumnIndex($column) |
81
|
|
|
{ |
82
|
2 |
|
if (isset($this->columnsAsKeys[$column])) { |
83
|
2 |
|
return $this->columnsAsKeys[$column]; |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@Inheritdoc} |
91
|
|
|
*/ |
92
|
6 |
|
public function getColumns($columnsAsKeys = true) |
93
|
|
|
{ |
94
|
6 |
|
return $columnsAsKeys ? $this->columnsAsKeys : $this->columnsAsValues; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@Inheritdoc} |
99
|
|
|
*/ |
100
|
2 |
|
public function getRows() |
101
|
|
|
{ |
102
|
2 |
|
return $this->rows; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@Inheritdoc} |
107
|
|
|
*/ |
108
|
2 |
|
#[\ReturnTypeWillChange] |
109
|
2 |
|
public function current() |
110
|
|
|
{ |
111
|
4 |
|
return current($this->rows); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@Inheritdoc} |
116
|
|
|
*/ |
117
|
4 |
|
public function next(): void |
118
|
|
|
{ |
119
|
4 |
|
next($this->rows); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@Inheritdoc} |
124
|
|
|
*/ |
125
|
2 |
|
#[\ReturnTypeWillChange] |
126
|
2 |
|
public function key() |
127
|
|
|
{ |
128
|
4 |
|
return key($this->rows); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@Inheritdoc} |
133
|
|
|
*/ |
134
|
4 |
|
public function valid(): bool |
135
|
|
|
{ |
136
|
4 |
|
return $this->key() !== null; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@Inheritdoc} |
141
|
|
|
*/ |
142
|
2 |
|
public function rewind(): void |
143
|
|
|
{ |
144
|
2 |
|
reset($this->rows); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@Inheritdoc} |
149
|
|
|
*/ |
150
|
2 |
|
public function count(): int |
151
|
|
|
{ |
152
|
2 |
|
return $this->rowCount; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|