1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Igni\Storage\Driver\Pdo; |
4
|
|
|
|
5
|
|
|
use Igni\Storage\Entity; |
6
|
|
|
use Igni\Storage\Manager; |
7
|
|
|
use Igni\Storage\Exception\RepositoryException; |
8
|
|
|
use Igni\Storage\Repository as RepositoryInterface; |
9
|
|
|
|
10
|
|
|
abstract class Repository implements RepositoryInterface |
11
|
|
|
{ |
12
|
|
|
protected $connection; |
13
|
|
|
protected $entityManager; |
14
|
|
|
protected $hydrator; |
15
|
|
|
protected $metaData; |
16
|
|
|
|
17
|
14 |
|
final public function __construct(Connection $connection, Manager $entityManager) |
18
|
|
|
{ |
19
|
14 |
|
$this->connection = $connection; |
20
|
14 |
|
$this->entityManager = $entityManager; |
21
|
14 |
|
$this->metaData = $this->entityManager->getMetaData($this->getEntityClass()); |
22
|
14 |
|
$this->hydrator = $this->entityManager->getHydrator($this->getEntityClass()); |
23
|
14 |
|
} |
24
|
|
|
|
25
|
7 |
|
public function get($id): Entity |
26
|
|
|
{ |
27
|
7 |
|
if ($this->entityManager->has($this->getEntityClass(), $id)) { |
28
|
1 |
|
return $this->entityManager->get($this->getEntityClass(), $id); |
29
|
|
|
} |
30
|
7 |
|
$query = sprintf( |
31
|
7 |
|
'SELECT *FROM %s WHERE %s = :__id__', |
32
|
7 |
|
$this->metaData->getSource(), |
33
|
7 |
|
$this->metaData->getIdentifier()->getFieldName() |
34
|
|
|
); |
35
|
|
|
|
36
|
7 |
|
$cursor = $this->connection->execute($query, ['__id__' => $id]); |
37
|
7 |
|
$cursor->setHydrator($this->hydrator); |
38
|
7 |
|
$entity = $cursor->current(); |
39
|
|
|
|
40
|
7 |
|
if (!$entity instanceof Entity) { |
41
|
|
|
throw RepositoryException::forNotFound($id); |
42
|
|
|
} |
43
|
|
|
|
44
|
7 |
|
return $entity; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function create(Entity $entity): Entity |
48
|
|
|
{ |
49
|
|
|
// Execute id auto-generation. |
50
|
|
|
$entity->getId(); |
51
|
|
|
$data = $this->hydrator->extract($entity); |
52
|
|
|
$fields = array_keys($data); |
53
|
|
|
$binds = []; |
54
|
|
|
$columns = []; |
55
|
|
|
foreach ($fields as $columnName) { |
56
|
|
|
$columns[] = "\"${columnName}\""; |
57
|
|
|
$binds[] = ":${columnName}"; |
58
|
|
|
} |
59
|
|
|
$sql = sprintf( |
60
|
|
|
'INSERT INTO %s (%s) VALUES(%s)', |
61
|
|
|
$this->metaData->getSource(), |
62
|
|
|
implode(',', $columns), |
63
|
|
|
implode(',', $binds) |
64
|
|
|
); |
65
|
|
|
$cursor = $this->connection->execute($sql, $data); |
66
|
|
|
$cursor->execute(); |
67
|
|
|
|
68
|
|
|
return $entity; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function remove(Entity $entity): Entity |
72
|
|
|
{ |
73
|
2 |
|
$query = sprintf( |
74
|
2 |
|
'DELETE FROM %s WHERE %s = :__id__', |
75
|
2 |
|
$this->metaData->getSource(), |
76
|
2 |
|
$this->metaData->getIdentifier()->getFieldName() |
77
|
|
|
); |
78
|
2 |
|
$cursor = $this->connection->execute($query, ['__id__' => $entity->getId()]); |
79
|
2 |
|
$cursor->execute(); |
80
|
|
|
|
81
|
2 |
|
return $entity; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function update(Entity $entity): Entity |
85
|
|
|
{ |
86
|
2 |
|
$data = $this->hydrator->extract($entity); |
87
|
2 |
|
$fields = array_keys($data); |
88
|
2 |
|
$columns = []; |
89
|
2 |
|
foreach ($fields as $columnName) { |
90
|
2 |
|
$columns[] = "\"${columnName}\" = :${columnName}"; |
91
|
|
|
} |
92
|
2 |
|
$sql = sprintf( |
93
|
2 |
|
'UPDATE %s SET %s WHERE %s = :__id__', |
94
|
2 |
|
$this->metaData->getSource(), |
95
|
2 |
|
implode(', ', $columns), |
96
|
2 |
|
$this->metaData->getIdentifier()->getFieldName() |
97
|
|
|
); |
98
|
2 |
|
$cursor = $this->connection->execute($sql, array_merge($data, ['__id__' => $entity->getId()])); |
99
|
2 |
|
$cursor->execute(); |
100
|
2 |
|
$cursor->close(); |
101
|
|
|
|
102
|
2 |
|
return $entity; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
protected function query($query, array $parameters = []): Cursor |
106
|
|
|
{ |
107
|
7 |
|
$cursor = $this->connection->execute($query, $parameters); |
108
|
7 |
|
$cursor->setHydrator($this->hydrator); |
109
|
|
|
|
110
|
7 |
|
return $cursor; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
abstract public function getEntityClass(): string; |
114
|
|
|
} |
115
|
|
|
|