-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicTableGateway.php
90 lines (83 loc) · 2.6 KB
/
BasicTableGateway.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
<?php
abstract class BasicTableGateway
{
protected $db;
protected $table;
protected $fields;
protected $primary = 'id';
public function __construct(\PDO $db)
{
$this->db = $db;
}
/* Generic methods */
public function sql(string $sql, array $params): \PDOStatement
{
$stmt = $this->db->prepare($sql);
$stmt->execute($params);
return $stmt;
}
public function listBySql(string $sql, array $params = [], int $mode = PDO::FETCH_ASSOC): array
{
return $this->sql($sql, $params)->fetchAll($mode);
}
public function getBySql(string $sql, array $params = [], int $mode = PDO::FETCH_ASSOC): array|bool
{
return $this->sql($sql, $params)->fetch($mode);
}
public function getByField(string $fieldName, string $value, int $mode = PDO::FETCH_ASSOC): array|bool
{
$this->validate([$fieldName => $value]);
$sql = "SELECT * FROM `$this->table` WHERE `$fieldName`=?";
return $this->getBySql($sql, [$value], $mode);
}
/* CRUD methods */
public function create($data): int
{
$fields = $this->makeFieldList($data);
$placeholders = str_repeat('?,', count($data) - 1) . '?';
$sql = "INSERT INTO `$this->table` ($fields) VALUES ($placeholders)";
$this->sql($sql,array_values($data));
return $this->db->lastInsertId();
}
public function read($id): array|bool
{
return $this->getByField($this->primary, $id);
}
public function update(array $data, int $id): void
{
[$params, $set] = $this->makeSET($data);
$params[] = $id;
$sql = "UPDATE `$this->table` SET $set WHERE `$this->primary`=?";
$this->db->prepare($sql)->execute($params);
}
public function delete($id)
{
$sql = "DELETE FROM `$this->table` WHERE `$this->primary`=?";
$this->sql($sql,[$id]);
}
/* Service methods */
protected function validate($data)
{
$diff = array_diff(array_keys($data), [$this->primary, ...$this->fields]);
if ($diff) {
throw new \InvalidArgumentException("Unknown field(s): ". implode(",", $diff));
}
}
protected function makeFieldList($data)
{
$this->validate($data);
return '`'.implode("`,`", array_keys($data)).'`';
}
protected function makeSET($data)
{
$this->validate($data);
$params = [];
$set = "";
foreach($data as $key => $value)
{
$set .= ($set ? "," : "") . "`$key` = ?";
$params[] = $value;
}
return [$params, $set];
}
}