|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace DataSource\TableDataGateway; |
|
6
|
|
|
|
|
7
|
|
|
use ArrayAccess; |
|
8
|
|
|
use DataSource\Connection; |
|
9
|
|
|
use BasePatterns\RecordSet\Row; |
|
10
|
|
|
use BasePatterns\RecordSet\Table; |
|
11
|
|
|
use BasePatterns\RecordSet\RecordSet; |
|
12
|
|
|
|
|
13
|
|
|
abstract class DataGateway implements ArrayAccess |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var DataSetHolder |
|
17
|
|
|
*/ |
|
18
|
|
|
public $holder; |
|
19
|
|
|
|
|
20
|
|
|
abstract public function getTableName(): string; |
|
21
|
3 |
|
|
|
22
|
|
|
public function __construct(Connection $connection) |
|
23
|
3 |
|
{ |
|
24
|
3 |
|
$this->holder = new DataSetHolder($connection); |
|
25
|
3 |
|
} |
|
26
|
|
|
|
|
27
|
3 |
|
public function getData(): RecordSet |
|
28
|
|
|
{ |
|
29
|
3 |
|
return $this->holder->data; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
3 |
|
public function getTable(): Table |
|
33
|
|
|
{ |
|
34
|
3 |
|
return $this->getData()->getTable($this->getTableName()); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
public function loadAll(): void |
|
38
|
|
|
{ |
|
39
|
1 |
|
$commandString = sprintf("SELECT * FROM `%s`", $this->getTableName()); |
|
40
|
1 |
|
$this->holder->fillData($commandString, $this->getTableName()); |
|
41
|
1 |
|
} |
|
42
|
|
|
|
|
43
|
2 |
|
public function loadWhere(string $whereClause, array $params): void |
|
44
|
|
|
{ |
|
45
|
2 |
|
$commandString = sprintf("SELECT * FROM `%s` WHERE %s", $this->getTableName(), $whereClause); |
|
46
|
2 |
|
$this->holder->fillData($commandString, $this->getTableName(), $params); |
|
47
|
2 |
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
public function update(): void |
|
50
|
|
|
{ |
|
51
|
1 |
|
$this->holder->update(); |
|
52
|
1 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function offsetExists($offset): bool |
|
55
|
|
|
{ |
|
56
|
|
|
return array_key_exists($offset, $this->holder->data[$this->getTableName()]); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
3 |
|
public function offsetGet($offset) |
|
60
|
|
|
{ |
|
61
|
3 |
|
$rows = $this->getTable()->getRows(); |
|
62
|
|
|
$hit = array_filter($rows, function (Row $row) use ($offset) { |
|
63
|
3 |
|
return $row->id == $offset; |
|
|
|
|
|
|
64
|
3 |
|
}); |
|
65
|
|
|
|
|
66
|
3 |
|
return reset($hit) ?: null; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function offsetSet($offset, $value): void |
|
70
|
|
|
{ |
|
71
|
|
|
false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function offsetUnset($offset): void |
|
75
|
|
|
{ |
|
76
|
|
|
false; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|