-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractBaseProcessor.php
More file actions
321 lines (277 loc) · 9.52 KB
/
AbstractBaseProcessor.php
File metadata and controls
321 lines (277 loc) · 9.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
/**
* TechDivision\Import\Dbal\Actions\Processors\AbstractBaseProcessor
*
* PHP version 7
*
* @author Tim Wagner <t.wagner@techdivision.com>
* @copyright 2021 TechDivision GmbH <info@techdivision.com>
* @license https://opensource.org/licenses/MIT
* @link https://github.com/techdivision/import-dbal-collection
* @link http://www.techdivision.com
*/
namespace TechDivision\Import\Dbal\Collection\Actions\Processors;
use ArrayObject;
use TechDivision\Import\Dbal\Utils\EntityStatus;
use TechDivision\Import\Dbal\Connection\ConnectionInterface;
use TechDivision\Import\Dbal\Repositories\SqlStatementRepositoryInterface;
use TechDivision\Import\Dbal\Utils\SanitizerInterface;
/**
* An abstract processor implementation provide basic CRUD functionality.
*
* @author Tim Wagner <t.wagner@techdivision.com>
* @copyright 2021 TechDivision GmbH <info@techdivision.com>
* @license https://opensource.org/licenses/MIT
* @link https://github.com/techdivision/import-dbal-collection
* @link http://www.techdivision.com
*/
abstract class AbstractBaseProcessor extends AbstractProcessor
{
/**
* The array with the statements to be prepared.
*
* @var array
*/
protected $statements = array();
/**
* The array with the prepared statements.
*
* @var array
*/
protected $preparedStatements = array();
/**
* The array holding row data sanitizer.
*
* @var ArrayObject
*/
protected $sanitizers;
/**
* The default statement name.
*
* @var string
*/
protected $defaultStatementName;
/**
* Initialize the processor with the passed connection and utility class name, as well as optional sanitizers.
* .
*
* @param \TechDivision\Import\Dbal\Connection\ConnectionInterface $connection The connection instance
* @param \TechDivision\Import\Dbal\Repositories\SqlStatementRepositoryInterface $sqlStatementRepository The repository instance
* @param ArrayObject|null $sanitizers The array with the sanitizer instances
*/
public function __construct(
ConnectionInterface $connection,
SqlStatementRepositoryInterface $sqlStatementRepository,
?ArrayObject $sanitizers = null
) {
// pass the connection and the SQL statement repository to the parent class
parent::__construct($connection, $sqlStatementRepository);
// set the sanititzes, if available
$this->setSanitizers($sanitizers ?? new ArrayObject());
}
/**
* Return's the array with the SQL statements that has to be prepared.
*
* @return array The SQL statements to be prepared
*/
protected function getStatements()
{
return $this->statements;
}
/**
* Add's the prepared statement.
*
* @param string $name The unique name of the prepared statement
* @param \PDOStatement $preparedStatement The prepared statement
*
* @return void
*/
protected function addPreparedStatement($name, \PDOStatement $preparedStatement)
{
$this->preparedStatements[$name] = $preparedStatement;
}
/**
* Return's the prepared statement with the passed name or the default one.
*
* @param string|null $name The name of the prepared statement to return
* @param string|null $defaultName The name of the default prepared statement
*
* @return \PDOStatement The prepared statement
*/
protected function getPreparedStatement($name = null, $defaultName = null)
{
// try to load the prepared statement, or use the default one
if (isset($this->preparedStatements[$name])) {
return $this->preparedStatements[$name];
}
// return the default prepared statement
return $this->preparedStatements[$defaultName];
}
/**
* Gets sanitizers list.
*
* @return ArrayObject The sanitizers
*/
public function getSanitizers(): ArrayObject
{
return $this->sanitizers;
}
/**
* Sets sanitizers list.
*
* @param ArrayObject $sanitizers The sanitizers
*
* @return void
*/
public function setSanitizers(ArrayObject $sanitizers): void
{
$this->sanitizers = $sanitizers;
}
/**
* Qeuery whether or not the prepared statement is available or not.
*
* @param string $name The nqme of the prepared statement
*
* @return boolean TRUE if the prepared statement is available, else FALSE
*/
protected function hasPreparedStatement($name)
{
return isset($this->preparedStatements[$name]);
}
/**
* The array with the prepared statements.
*
* @return array The prepared statments
*/
protected function getPreparedStatements()
{
return $this->preparedStatements;
}
/**
* Prepare's and return's the passed row by removing the
* entity status.
*
* @param array $row The row to prepare
* @param string $statement The statement string
*
* @return array The prepared row
*/
protected function prepareRow(array $row, $statement = '')
{
// remove the entity status
unset($row[EntityStatus::MEMBER_NAME]);
// Remove unused rows from statement
if (!empty($statement)) {
foreach ($row as $key => $value) {
if (!preg_match('/(:'.$key.'[^a-zA-Z_])|(:'.$key.'$)/', $statement)) {
// remove the entity column is not use in statement
unset($row[$key]);
}
}
}
// return the prepared row
return $row;
}
/**
* Return's the name of the processor's default statement.
*
* @return string The statement name
*/
public function getDefaultStatementName()
{
return $this->defaultStatementName;
}
/**
* Implements the CRUD functionality the processor is responsible for,
* can be one of CREATE, READ, UPDATE or DELETE a entity.
*
* @param array $row The row to persist
* @param string|null $name The name of the prepared statement that has to be executed
* @param string|null $primaryKeyMemberName The primary key member name of the entity to use
*
* @return void
*/
public function execute($row, $name = null, $primaryKeyMemberName = null)
{
$statement = $this->getPreparedStatement($name, $this->getDefaultStatementName());
$row = $this->sanitize($row, $statement);
try {
// finally execute the prepared statement
$statement->execute($this->prepareRow($row, $statement->queryString));
} catch (\PDOException $pdoe) {
// initialize the SQL statement with the placeholders
$sql = $statement->queryString;
// replace the placeholders with the values
foreach ($row as $key => $value) {
// Set null and empty string as string for better readability
if ($value === null) {
$value = "null";
} elseif ($value === "") {
$value = "''";
}
$sql = str_replace(sprintf(':%s', $key), $value, $sql);
}
// prepare the error message itself
$message = sprintf('%s when executing SQL "%s"', $pdoe->getMessage(), preg_replace('/\r\n\s\s+/', ' ', $sql));
// re-throw the exception with a more detailed error message
throw new \PDOException($message);
}
}
/**
* Initializes the proceessor with the prepared statements.
*
* @return void
*/
public function init()
{
// load the statements
$statements = $this->getStatements();
// initialize the default statement name
$this->defaultStatementName = $this->firstKey($statements);
foreach ($statements as $name => $statement) {
$this->addPreparedStatement($name, $this->getConnection()->prepare($statement));
}
}
/**
* Returns the first key of the passed array.
*
* This method has been used instead of the PHP function array_key_first, because
* this function will be available with PHP >= 7.3.0.
*
* @param array $array The array to return the first key for
*
* @return mixed|NULL The first key or NULL
* @link https://www.php.net/array_key_first
*/
private function firstKey(array $array)
{
// load the array keys
$keys = array_keys($array);
// try to load and return the first key
foreach ($keys as $key) {
return $key;
}
// return NULL otherwise
return null;
}
/**
* Passes row data and statement to sanitizers list.
*
* @param array $row The row that has to be sanitized
* @param \PDOStatement $statement The statement that has to be sanitzied
*
* @return array The sanitized row
*/
protected function sanitize(array $row, \PDOStatement $statement) : array
{
// load the raw statement that has to be sanitized
$rawStatement = $statement->queryString;
// invoke the registered sanitizers on the statement
/** @var SanitizerInterface $sanitizer */
foreach ($this->sanitizers as $sanitizer) {
$row = $sanitizer->execute($row, $rawStatement);
}
// return the sanizized row
return $row;
}
}