1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Website\Orm\DataMappers; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Domain\Entities\IStringerEntity; |
8
|
|
|
use AbterPhp\Website\Domain\Entities\BlockLayout as Entity; |
9
|
|
|
use Opulence\Orm\DataMappers\SqlDataMapper; |
10
|
|
|
use Opulence\QueryBuilders\Expression; |
11
|
|
|
use Opulence\QueryBuilders\MySql\QueryBuilder; |
12
|
|
|
use Opulence\QueryBuilders\MySql\SelectQuery; |
13
|
|
|
|
14
|
|
|
/** @phan-file-suppress PhanTypeMismatchArgument */ |
15
|
|
|
class BlockLayoutSqlDataMapper extends SqlDataMapper implements IBlockLayoutDataMapper |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param IStringerEntity $entity |
19
|
|
|
*/ |
20
|
|
|
public function add($entity) |
21
|
|
|
{ |
22
|
|
|
assert($entity instanceof Entity, new \InvalidArgumentException()); |
23
|
|
|
|
24
|
|
|
$query = (new QueryBuilder()) |
25
|
|
|
->insert( |
26
|
|
|
'block_layouts', |
27
|
|
|
[ |
28
|
|
|
'id' => $entity->getId(), |
29
|
|
|
'name' => $entity->getName(), |
30
|
|
|
'identifier' => $entity->getIdentifier(), |
31
|
|
|
'body' => $entity->getBody(), |
32
|
|
|
] |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
$statement = $this->writeConnection->prepare($query->getSql()); |
36
|
|
|
$statement->bindValues($query->getParameters()); |
37
|
|
|
$statement->execute(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param IStringerEntity $entity |
42
|
|
|
*/ |
43
|
|
|
public function delete($entity) |
44
|
|
|
{ |
45
|
|
|
assert($entity instanceof Entity, new \InvalidArgumentException()); |
46
|
|
|
|
47
|
|
|
$query = (new QueryBuilder()) |
48
|
|
|
->update('block_layouts', 'block_layouts', ['deleted_at' => new Expression('NOW()')]) |
49
|
|
|
->where('id = ?') |
50
|
|
|
->addUnnamedPlaceholderValue($entity->getId(), \PDO::PARAM_STR); |
51
|
|
|
|
52
|
|
|
$statement = $this->writeConnection->prepare($query->getSql()); |
53
|
|
|
$statement->bindValues($query->getParameters()); |
54
|
|
|
$statement->execute(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return Entity[] |
59
|
|
|
*/ |
60
|
|
|
public function getAll(): array |
61
|
|
|
{ |
62
|
|
|
$query = $this->getBaseQuery(); |
63
|
|
|
|
64
|
|
|
return $this->read($query->getSql(), [], self::VALUE_TYPE_ARRAY); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param int $limitFrom |
69
|
|
|
* @param int $pageSize |
70
|
|
|
* @param string[] $orders |
71
|
|
|
* @param array $conditions |
72
|
|
|
* @param array $params |
73
|
|
|
* |
74
|
|
|
* @return Entity[] |
75
|
|
|
*/ |
76
|
|
|
public function getPage(int $limitFrom, int $pageSize, array $orders, array $conditions, array $params): array |
77
|
|
|
{ |
78
|
|
|
$query = $this->getBaseQuery() |
79
|
|
|
->limit($pageSize) |
80
|
|
|
->offset($limitFrom); |
81
|
|
|
|
82
|
|
|
if (!$orders) { |
|
|
|
|
83
|
|
|
$query->orderBy('name ASC'); |
84
|
|
|
} |
85
|
|
|
foreach ($orders as $order) { |
86
|
|
|
$query->addOrderBy($order); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
foreach ($conditions as $condition) { |
90
|
|
|
$query->andWhere($condition); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$replaceCount = 1; |
94
|
|
|
|
95
|
|
|
$sql = $query->getSql(); |
96
|
|
|
$sql = str_replace('SELECT', 'SELECT SQL_CALC_FOUND_ROWS', $sql, $replaceCount); |
97
|
|
|
|
98
|
|
|
return $this->read($sql, $params, self::VALUE_TYPE_ARRAY); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param int|string $id |
103
|
|
|
* |
104
|
|
|
* @return Entity|null |
105
|
|
|
*/ |
106
|
|
|
public function getById($id) |
107
|
|
|
{ |
108
|
|
|
$query = $this->getBaseQuery()->andWhere('block_layouts.id = :layout_id'); |
109
|
|
|
|
110
|
|
|
$parameters = [ |
111
|
|
|
'layout_id' => [$id, \PDO::PARAM_STR], |
112
|
|
|
]; |
113
|
|
|
|
114
|
|
|
return $this->read($query->getSql(), $parameters, self::VALUE_TYPE_ENTITY, true); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $identifier |
119
|
|
|
* |
120
|
|
|
* @return Entity|null |
121
|
|
|
* @throws \Opulence\Orm\OrmException |
122
|
|
|
*/ |
123
|
|
|
public function getByIdentifier(string $identifier): ?Entity |
124
|
|
|
{ |
125
|
|
|
$query = $this->getBaseQuery()->andWhere('identifier = :identifier'); |
126
|
|
|
|
127
|
|
|
$parameters = [ |
128
|
|
|
'identifier' => $identifier, |
129
|
|
|
]; |
130
|
|
|
|
131
|
|
|
return $this->read($query->getSql(), $parameters, self::VALUE_TYPE_ENTITY, true); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param IStringerEntity $entity |
136
|
|
|
*/ |
137
|
|
|
public function update($entity) |
138
|
|
|
{ |
139
|
|
|
assert($entity instanceof Entity, new \InvalidArgumentException()); |
140
|
|
|
|
141
|
|
|
$query = (new QueryBuilder()) |
142
|
|
|
->update( |
143
|
|
|
'block_layouts', |
144
|
|
|
'block_layouts', |
145
|
|
|
[ |
146
|
|
|
'name' => [$entity->getName(), \PDO::PARAM_STR], |
147
|
|
|
'identifier' => [$entity->getIdentifier(), \PDO::PARAM_STR], |
148
|
|
|
'body' => [$entity->getBody(), \PDO::PARAM_STR], |
149
|
|
|
] |
150
|
|
|
) |
151
|
|
|
->where('id = ?') |
152
|
|
|
->andWhere('deleted_at IS NULL') |
153
|
|
|
->addUnnamedPlaceholderValue($entity->getId(), \PDO::PARAM_STR); |
154
|
|
|
|
155
|
|
|
$statement = $this->writeConnection->prepare($query->getSql()); |
156
|
|
|
$statement->bindValues($query->getParameters()); |
157
|
|
|
$statement->execute(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param array $hash |
162
|
|
|
* |
163
|
|
|
* @return Entity |
164
|
|
|
*/ |
165
|
|
|
protected function loadEntity(array $hash) |
166
|
|
|
{ |
167
|
|
|
return new Entity( |
168
|
|
|
$hash['id'], |
169
|
|
|
$hash['name'], |
170
|
|
|
$hash['identifier'], |
171
|
|
|
$hash['body'] |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return SelectQuery |
177
|
|
|
*/ |
178
|
|
|
private function getBaseQuery() |
179
|
|
|
{ |
180
|
|
|
/** @var SelectQuery $query */ |
181
|
|
|
$query = (new QueryBuilder()) |
182
|
|
|
->select( |
183
|
|
|
'block_layouts.id', |
184
|
|
|
'block_layouts.name', |
185
|
|
|
'block_layouts.identifier', |
186
|
|
|
'block_layouts.body' |
187
|
|
|
) |
188
|
|
|
->from('block_layouts') |
189
|
|
|
->where('block_layouts.deleted_at IS NULL'); |
190
|
|
|
|
191
|
|
|
return $query; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|