-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathTestDriver.php
156 lines (142 loc) · 3.86 KB
/
TestDriver.php
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
<?php
namespace Aternos\Model\Driver\Test;
use Aternos\Model\Driver\Driver;
use Aternos\Model\Driver\Features\CRUDAbleInterface;
use Aternos\Model\Driver\Features\CRUDQueryableInterface;
use Aternos\Model\ModelInterface;
use Aternos\Model\ModelRegistry;
use Aternos\Model\Query\Query;
use Aternos\Model\Query\QueryResult;
use Exception;
class TestDriver extends Driver implements CRUDAbleInterface, CRUDQueryableInterface
{
public const ID = "test";
protected string $id = self::ID;
/**
* @var TestTable[]
*/
protected array $tables = [];
/**
* @param array $tables
* @return $this
*/
public function addTables(array $tables): static
{
foreach ($tables as $name => $entries) {
$this->addTable($name, $entries);
}
return $this;
}
/**
* @param string $name
* @param array $entries
* @return $this
*/
public function addTable(string $name, array $entries): static
{
$this->tables[$name] = new TestTable($name);
$this->tables[$name]->addArrayEntries($entries);
return $this;
}
/**
* @param string $name
* @return TestTable
*/
public function getTable(string $name): TestTable
{
if (!isset($this->tables[$name])) {
$this->tables[$name] = new TestTable($name);
}
return $this->tables[$name];
}
/**
* @param string $tableName
* @param array $entry
* @return $this
*/
public function addEntry(string $tableName, array $entry): static
{
$table = $this->getTable($tableName);
$table->addEntry(new TestTableEntry($entry));
return $this;
}
/**
* @return $this
*/
public function clearTables(): static
{
ModelRegistry::getInstance()->clearAll();
$this->tables = [];
return $this;
}
/**
* @param string $tableName
* @return $this
*/
public function clearEntries(string $tableName): static
{
if (isset($this->tables[$tableName])) {
$this->tables[$tableName]->clear();
}
return $this;
}
/**
* @param ModelInterface $model
* @return bool
* @throws Exception
*/
public function delete(ModelInterface $model): bool
{
$table = $this->getTable($model::getName());
$entry = $table->getById($model->getId(), $model::getIdField());
if (!$entry) {
return false;
}
$table->deleteEntry($entry);
return true;
}
/**
* @param class-string<ModelInterface> $modelClass
* @param mixed $id
* @param ModelInterface|null $model
* @return ModelInterface|null
* @throws Exception
*/
public function get(string $modelClass, mixed $id, ?ModelInterface $model = null): ?ModelInterface
{
$entry = $this->getTable($modelClass::getName())->getById($id, $modelClass::getIdField());
if (!$entry) {
return null;
}
if ($model) {
return $entry->applyToModel($model);
}
return $modelClass::getModelFromData($entry->getData());
}
/**
* @param ModelInterface $model
* @return bool
* @throws Exception
*/
public function save(ModelInterface $model): bool
{
$table = $this->getTable($model::getName());
$entry = $table->getById($model->getId(), $model::getIdField());
if (!$entry) {
$table->addEntry((new TestTableEntry(get_object_vars($model))));
return true;
}
$entry->applyFromModel($model);
return true;
}
/**
* @param Query $query
* @return QueryResult
* @throws Exception
*/
public function query(Query $query): QueryResult
{
$table = $this->getTable($query->modelClassName::getName());
return $table->query($query);
}
}