-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTestTable.php
More file actions
228 lines (206 loc) · 6.24 KB
/
TestTable.php
File metadata and controls
228 lines (206 loc) · 6.24 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
namespace Aternos\Model\Driver\Test;
use Aternos\Model\ModelInterface;
use Aternos\Model\ModelRegistry;
use Aternos\Model\Query\DeleteQuery;
use Aternos\Model\Query\Direction;
use Aternos\Model\Query\GroupField;
use Aternos\Model\Query\OrderField;
use Aternos\Model\Query\Query;
use Aternos\Model\Query\QueryResult;
use Aternos\Model\Query\SelectField;
use Aternos\Model\Query\SelectQuery;
use Aternos\Model\Query\UpdateQuery;
use Aternos\Model\Query\WhereGroup;
class TestTable
{
/**
* @var TestTableEntry[]
*/
protected array $entries = [];
/**
* @param string $name
*/
public function __construct(protected string $name)
{
}
/**
* @param array $entries
* @return $this
*/
public function addArrayEntries(array $entries): static
{
foreach ($entries as $entry) {
$this->addEntry(new TestTableEntry($entry));
}
return $this;
}
/**
* @param TestTableEntry ...$entry
* @return $this
*/
public function addEntry(TestTableEntry ...$entry): static
{
$this->entries = array_merge($this->entries, $entry);
return $this;
}
/**
* @param Query $query
* @return QueryResult
*/
public function query(Query $query): QueryResult
{
$entries = $this->findEntries($query->getWhere(), $query->getLimit()?->start, $query->getLimit()?->length, $query->getOrder());
if ($query instanceof SelectQuery) {
$clonedEntries = [];
foreach ($entries as $entry) {
$clonedEntries[] = clone $entry;
}
$entries = $this->groupAndAggregateEntries($clonedEntries, $query->getGroup(), $query->getFields());
}
$queryResult = new QueryResult();
foreach ($entries as $entry) {
if ($query instanceof SelectQuery) {
/** @var class-string<ModelInterface> $modelClass */
$modelClass = $query->modelClassName;
$model = $modelClass::getModelFromData($entry->getDataForFields($query->getFields()));
$queryResult->add($model);
}
if ($query instanceof UpdateQuery) {
$entry->update($query->getFields());
}
if ($query instanceof DeleteQuery) {
$this->deleteEntry($entry);
}
}
if ($query instanceof UpdateQuery || $query instanceof DeleteQuery) {
$queryResult->setAffectedRows(count($entries));
}
return $queryResult;
}
/**
* @param TestTableEntry[] $entries
* @param GroupField[]|null $group
* @param SelectField[]|null $fields
* @return TestTableEntry[]
*/
public function groupAndAggregateEntries(array $entries, ?array $group, ?array $fields): array
{
$groups = [];
foreach ($entries as $entry) {
foreach ($groups as $currentGroup) {
if ($currentGroup->matches($entry)) {
$currentGroup->addEntry($entry);
continue 2;
}
}
$currentGroup = new TestTableEntryGroup($entry, $group);
$groups[] = $currentGroup;
}
foreach ($groups as $currentGroup) {
$currentGroup->aggregateAndAlias($fields, !empty($group));
}
$newEntries = [];
foreach ($groups as $currentGroup) {
foreach ($currentGroup->getEntries() as $entry) {
$newEntries[] = $entry;
}
}
return $newEntries;
}
/**
* @param WhereGroup|null $where
* @param int|null $offset
* @param int|null $limit
* @param array|null $order
* @return TestTableEntry[]
*/
protected function findEntries(?WhereGroup $where, ?int $offset = null, ?int $limit = null, ?array $order = null): array
{
$entries = [];
if ($offset === null) {
$offset = 0;
}
foreach ($this->entries as $entry) {
if (!$entry->matchesWhereGroup($where)) {
continue;
}
$entries[] = $entry;
}
if ($order !== null) {
$entries = $this->orderEntries($entries, $order);
}
return array_slice($entries, $offset, $limit);
}
/**
* @param TestTableEntry $a
* @param TestTableEntry $b
* @param OrderField[] $order
* @return int
*/
protected function compareEntries(TestTableEntry $a, TestTableEntry $b, array $order): int
{
foreach ($order as $orderField) {
$aValue = $a->getField($orderField->field);
$bValue = $b->getField($orderField->field);
if ($aValue === $bValue) {
continue;
}
if ($orderField->direction === Direction::ASCENDING) {
return $aValue > $bValue ? 1 : -1;
} else {
return $aValue < $bValue ? 1 : -1;
}
}
return 0;
}
/**
* @param array $entries
* @param OrderField[] $order
* @return TestTableEntry[]
*/
protected function orderEntries(array $entries, array $order): array
{
usort($entries, function (TestTableEntry $a, TestTableEntry $b) use ($order) {
return $this->compareEntries($a, $b, $order);
});
return $entries;
}
/**
* @return $this
*/
public function clear(): static
{
$this->entries = [];
ModelRegistry::getInstance()->clearModel($this->name);
return $this;
}
/**
* @param mixed $id
* @param string $idField
* @return TestTableEntry|null
*/
public function getById(mixed $id, string $idField = "id"): ?TestTableEntry
{
foreach ($this->entries as $entry) {
if ($entry->hasId($id, $idField)) {
return $entry;
}
}
return null;
}
/**
* @param TestTableEntry $entry
* @return $this
*/
public function deleteEntry(TestTableEntry $entry): static
{
foreach ($this->entries as $key => $tableEntry) {
if ($tableEntry === $entry) {
unset($this->entries[$key]);
break;
}
}
return $this;
}
}