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