1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace DrMVC\Orm; |
4
|
|
|
|
5
|
|
|
use PDO; |
6
|
|
|
use PDOStatement; |
7
|
|
|
|
8
|
|
|
class Orm |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var PDO |
12
|
|
|
*/ |
13
|
|
|
private $pdo; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var Builder |
17
|
|
|
*/ |
18
|
|
|
private $builder; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Orm constructor. |
22
|
|
|
* @param string $table |
23
|
|
|
* @param PDO $pdo |
24
|
|
|
*/ |
25
|
1 |
|
public function __construct(string $table, PDO $pdo) |
26
|
|
|
{ |
27
|
1 |
|
$this->setInstance($pdo); |
28
|
1 |
|
$this->setBuilder(new Builder($table)); |
29
|
1 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param PDO $instance |
33
|
|
|
*/ |
34
|
1 |
|
private function setInstance(PDO $instance) |
35
|
|
|
{ |
36
|
1 |
|
$this->pdo = $instance; |
37
|
1 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Builder $builder |
41
|
|
|
*/ |
42
|
1 |
|
private function setBuilder(Builder $builder) |
43
|
|
|
{ |
44
|
1 |
|
$this->builder = $builder; |
45
|
1 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Save or update |
49
|
|
|
* |
50
|
|
|
* @param Entity $entity |
51
|
|
|
* @return PDOStatement |
52
|
|
|
*/ |
53
|
2 |
|
public function saveEntity(Entity $entity): PDOStatement |
54
|
|
|
{ |
55
|
2 |
|
$data = $entity->getData(); |
56
|
2 |
|
$id = (int)$entity->id; |
57
|
2 |
|
if ($id > 0 && $this->findById($id)) { |
58
|
1 |
|
$result = $this->exec( |
59
|
1 |
|
(string)$this->getBuilder()->update($data)->byId($id), |
60
|
1 |
|
$this->getBuilder()->getPlaceholders() |
61
|
|
|
); |
62
|
|
|
} else { |
63
|
1 |
|
$result = $this->exec( |
64
|
1 |
|
(string)$this->getBuilder()->insert($data), |
65
|
1 |
|
$this->getBuilder()->getPlaceholders() |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
return $result; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param int $id |
74
|
|
|
* @return Entity|null |
75
|
|
|
*/ |
76
|
3 |
|
public function findById(int $id)//: ?Entity |
77
|
|
|
{ |
78
|
3 |
|
$entity = $this->getEntity( |
79
|
3 |
|
(string)$this->getBuilder()->select()->byId($id), |
80
|
3 |
|
$this->getBuilder()->getPlaceholders() |
81
|
|
|
); |
82
|
|
|
|
83
|
3 |
|
return $entity[0] ?? null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $sql |
88
|
|
|
* @param array $placeholders |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
4 |
|
private function getEntity(string $sql, array $placeholders): array |
92
|
|
|
{ |
93
|
4 |
|
$stmt = $this->exec($sql, $placeholders); |
94
|
4 |
|
$result = []; |
95
|
4 |
|
if ($stmt) { |
96
|
4 |
|
while ($entity = $stmt->fetchObject(Entity::class)) { |
97
|
4 |
|
$result[] = $entity; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
4 |
|
return $result; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $sql |
106
|
|
|
* @param array $placeholders |
107
|
|
|
* @return PDOStatement |
108
|
|
|
*/ |
109
|
5 |
|
private function exec(string $sql, array $placeholders): PDOStatement |
110
|
|
|
{ |
111
|
5 |
|
$stmt = $this->getInstance()->prepare($sql); |
112
|
5 |
|
$stmt->execute($placeholders); |
113
|
|
|
|
114
|
5 |
|
return $stmt; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return PDO |
119
|
|
|
*/ |
120
|
5 |
|
private function getInstance(): PDO |
121
|
|
|
{ |
122
|
5 |
|
return $this->pdo; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return Builder |
127
|
|
|
*/ |
128
|
5 |
|
private function getBuilder(): Builder |
129
|
|
|
{ |
130
|
5 |
|
return $this->builder; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param Entity $entity |
135
|
|
|
* @return int |
136
|
|
|
*/ |
137
|
1 |
|
public function deleteEntity(Entity $entity): int |
138
|
|
|
{ |
139
|
1 |
|
return $this->exec((string)$this->getBuilder()->delete()->byId((int)$entity->id), |
140
|
1 |
|
$this->getBuilder()->getPlaceholders())->rowCount(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** Array Entities |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
1 |
|
public function findAll(): array |
147
|
|
|
{ |
148
|
1 |
|
return $this->getEntity( |
149
|
1 |
|
(string)$this->getBuilder()->select(), |
150
|
1 |
|
$this->getBuilder()->getPlaceholders() |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|