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\ContentListItem as Entity; |
9
|
|
|
use Opulence\Orm\DataMappers\SqlDataMapper; |
10
|
|
|
use Opulence\QueryBuilders\MySql\QueryBuilder; |
11
|
|
|
use Opulence\QueryBuilders\MySql\SelectQuery; |
12
|
|
|
use PDO; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @phan-file-suppress PhanTypeMismatchArgument |
16
|
|
|
*/ |
17
|
|
|
class ContentListItemSqlDataMapper extends SqlDataMapper implements IContentListItemDataMapper |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param IStringerEntity $entity |
21
|
|
|
*/ |
22
|
|
|
public function add($entity) |
23
|
|
|
{ |
24
|
|
|
assert($entity instanceof Entity, new \InvalidArgumentException()); |
25
|
|
|
|
26
|
|
|
$query = (new QueryBuilder()) |
27
|
|
|
->insert( |
28
|
|
|
'list_items', |
29
|
|
|
[ |
30
|
|
|
'id' => [$entity->getId(), PDO::PARAM_STR], |
31
|
|
|
'list_id' => [$entity->getListId(), PDO::PARAM_STR], |
32
|
|
|
'name' => [$entity->getName(), PDO::PARAM_STR], |
33
|
|
|
'name_href' => [$entity->getNameHref(), PDO::PARAM_STR], |
34
|
|
|
'body' => [$entity->getBody(), PDO::PARAM_STR], |
35
|
|
|
'body_href' => [$entity->getBodyHref(), PDO::PARAM_STR], |
36
|
|
|
'img_src' => [$entity->getImgSrc(), PDO::PARAM_STR], |
37
|
|
|
'img_href' => [$entity->getImgHref(), PDO::PARAM_STR], |
38
|
|
|
'img_alt' => [$entity->getImgAlt(), PDO::PARAM_STR], |
39
|
|
|
] |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
$statement = $this->writeConnection->prepare($query->getSql()); |
43
|
|
|
$statement->bindValues($query->getParameters()); |
44
|
|
|
$statement->execute(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param IStringerEntity $entity |
49
|
|
|
* |
50
|
|
|
* @throws \Opulence\QueryBuilders\InvalidQueryException |
51
|
|
|
*/ |
52
|
|
|
public function delete($entity) |
53
|
|
|
{ |
54
|
|
|
assert($entity instanceof Entity, new \InvalidArgumentException()); |
55
|
|
|
|
56
|
|
|
$query = (new QueryBuilder()) |
57
|
|
|
->update('list_items', 'list_items', ['deleted' => [1, PDO::PARAM_INT]]) |
58
|
|
|
->where('id = ?') |
59
|
|
|
->addUnnamedPlaceholderValue($entity->getId(), PDO::PARAM_STR); |
60
|
|
|
|
61
|
|
|
$statement = $this->writeConnection->prepare($query->getSql()); |
62
|
|
|
$statement->bindValues($query->getParameters()); |
63
|
|
|
$statement->execute(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return Entity[] |
68
|
|
|
* @throws \Opulence\Orm\OrmException |
69
|
|
|
*/ |
70
|
|
|
public function getAll(): array |
71
|
|
|
{ |
72
|
|
|
$query = $this->getBaseQuery(); |
73
|
|
|
|
74
|
|
|
return $this->read($query->getSql(), [], self::VALUE_TYPE_ARRAY); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param int $limitFrom |
79
|
|
|
* @param int $pageSize |
80
|
|
|
* @param string[] $orders |
81
|
|
|
* @param array $conditions |
82
|
|
|
* @param array $params |
83
|
|
|
* |
84
|
|
|
* @return Entity[] |
85
|
|
|
* @throws \Opulence\Orm\OrmException |
86
|
|
|
*/ |
87
|
|
|
public function getPage(int $limitFrom, int $pageSize, array $orders, array $conditions, array $params): array |
88
|
|
|
{ |
89
|
|
|
$query = $this->getBaseQuery() |
90
|
|
|
->limit($pageSize) |
91
|
|
|
->offset($limitFrom); |
92
|
|
|
|
93
|
|
|
foreach ($orders as $order) { |
94
|
|
|
$query->addOrderBy($order); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
foreach ($conditions as $condition) { |
98
|
|
|
$query->andWhere($condition); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$replaceCount = 1; |
102
|
|
|
|
103
|
|
|
$sql = $query->getSql(); |
104
|
|
|
$sql = str_replace('SELECT', 'SELECT SQL_CALC_FOUND_ROWS', $sql, $replaceCount); |
105
|
|
|
|
106
|
|
|
return $this->read($sql, $params, self::VALUE_TYPE_ARRAY); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param int|string $id |
111
|
|
|
* |
112
|
|
|
* @return Entity|null |
113
|
|
|
* @throws \Opulence\Orm\OrmException |
114
|
|
|
*/ |
115
|
|
|
public function getById($id) |
116
|
|
|
{ |
117
|
|
|
$query = $this->getBaseQuery()->andWhere('list_items.id = :list_item_id'); |
118
|
|
|
|
119
|
|
|
$parameters = [ |
120
|
|
|
'list_item_id' => [$id, PDO::PARAM_STR], |
121
|
|
|
]; |
122
|
|
|
|
123
|
|
|
$sql = $query->getSql(); |
124
|
|
|
|
125
|
|
|
return $this->read($sql, $parameters, self::VALUE_TYPE_ENTITY, true); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $listId |
130
|
|
|
* |
131
|
|
|
* @return array |
132
|
|
|
* @throws \Opulence\Orm\OrmException |
133
|
|
|
*/ |
134
|
|
|
public function getByListId(string $listId): array |
135
|
|
|
{ |
136
|
|
|
$query = $this->getBaseQuery()->andWhere('list_items.list_id = :list_id'); |
137
|
|
|
|
138
|
|
|
$parameters = [ |
139
|
|
|
'list_id' => [$listId, PDO::PARAM_STR], |
140
|
|
|
]; |
141
|
|
|
|
142
|
|
|
return $this->read($query->getSql(), $parameters, self::VALUE_TYPE_ARRAY); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param IStringerEntity $entity |
147
|
|
|
* |
148
|
|
|
* @throws \Opulence\QueryBuilders\InvalidQueryException |
149
|
|
|
*/ |
150
|
|
|
public function update($entity) |
151
|
|
|
{ |
152
|
|
|
assert($entity instanceof Entity, new \InvalidArgumentException()); |
153
|
|
|
|
154
|
|
|
$query = (new QueryBuilder()) |
155
|
|
|
->update( |
156
|
|
|
'list_items', |
157
|
|
|
'list_items', |
158
|
|
|
[ |
159
|
|
|
'list_id' => [$entity->getListId(), PDO::PARAM_STR], |
160
|
|
|
'name' => [$entity->getName(), PDO::PARAM_STR], |
161
|
|
|
'name_href' => [$entity->getNameHref(), PDO::PARAM_STR], |
162
|
|
|
'body' => [$entity->getBody(), PDO::PARAM_STR], |
163
|
|
|
'body_href' => [$entity->getBodyHref(), PDO::PARAM_STR], |
164
|
|
|
'img_src' => [$entity->getImgSrc(), PDO::PARAM_STR], |
165
|
|
|
'img_href' => [$entity->getImgHref(), PDO::PARAM_STR], |
166
|
|
|
'img_alt' => [$entity->getImgAlt(), PDO::PARAM_STR], |
167
|
|
|
] |
168
|
|
|
) |
169
|
|
|
->where('id = ?') |
170
|
|
|
->andWhere('list_items.deleted_at IS NULL') |
171
|
|
|
->addUnnamedPlaceholderValue($entity->getId(), PDO::PARAM_STR); |
172
|
|
|
|
173
|
|
|
$statement = $this->writeConnection->prepare($query->getSql()); |
174
|
|
|
$statement->bindValues($query->getParameters()); |
175
|
|
|
$statement->execute(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param array $hash |
180
|
|
|
* |
181
|
|
|
* @return Entity |
182
|
|
|
*/ |
183
|
|
|
protected function loadEntity(array $hash) |
184
|
|
|
{ |
185
|
|
|
return new Entity( |
186
|
|
|
$hash['id'], |
187
|
|
|
$hash['list_id'], |
188
|
|
|
$hash['name'], |
189
|
|
|
$hash['name_href'], |
190
|
|
|
$hash['body'], |
191
|
|
|
$hash['body_href'], |
192
|
|
|
$hash['img_src'], |
193
|
|
|
$hash['img_href'], |
194
|
|
|
$hash['img_alt'] |
195
|
|
|
); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return SelectQuery |
200
|
|
|
*/ |
201
|
|
|
private function getBaseQuery(): SelectQuery |
202
|
|
|
{ |
203
|
|
|
/** @var SelectQuery $query */ |
204
|
|
|
$query = (new QueryBuilder()) |
205
|
|
|
->select( |
206
|
|
|
'list_items.id', |
207
|
|
|
'list_items.list_id', |
208
|
|
|
'list_items.name', |
209
|
|
|
'list_items.name_href', |
210
|
|
|
'list_items.body', |
211
|
|
|
'list_items.body_href', |
212
|
|
|
'list_items.img_src', |
213
|
|
|
'list_items.img_href', |
214
|
|
|
'list_items.img_alt' |
215
|
|
|
) |
216
|
|
|
->from('list_items') |
217
|
|
|
->where('list_items.deleted_at IS NULL'); |
218
|
|
|
|
219
|
|
|
return $query; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|