feat: moneta 2.0, MONETA.Assistant payment module for PHPShop 6.7+
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace MonetaPhpShop\host;
|
||||
|
||||
use mysqli;
|
||||
use mysqli_sql_exception;
|
||||
use mysqli_stmt;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Ïîäãîòîâëåííûå çàïðîñû ïîâåðõ ñîåäèíåíèÿ ÿäðà PHPShop (`$PHPShopBase->link_db`).
|
||||
*
|
||||
* `PHPShopOrm` ñîáèðàåò SQL êîíêàòåíàöèåé — äëÿ çàïèñåé êîëáýêà, ãäå â çàïðîñ ïîïàäàþò
|
||||
* äàííûå èçâíå, ìîäóëü õîäèò â ÁÄ ñàì. Îøèáêà çàïðîñà — {@see RuntimeException} ñ êîäîì
|
||||
* îøèáêè MySQL (1062 — äóáëèêàò êëþ÷à) ïðè ëþáîì `mysqli_report()`: â PHP 7.4 ïî óìîë÷àíèþ
|
||||
* mysqli ìîë÷èò, ñ 8.1 áðîñàåò èñêëþ÷åíèÿ.
|
||||
*/
|
||||
final class Database
|
||||
{
|
||||
/** Äóáëèêàò óíèêàëüíîãî êëþ÷à. */
|
||||
public const ER_DUP_ENTRY = 1062;
|
||||
|
||||
/** @var mysqli */
|
||||
private $link;
|
||||
|
||||
public function __construct(mysqli $link)
|
||||
{
|
||||
$this->link = $link;
|
||||
}
|
||||
|
||||
public static function fromHost(): self
|
||||
{
|
||||
// Êîëáýê è âèòðèíà äåðæàò ñîåäèíåíèå â $PHPShopBase, ÿäðî äóáëèðóåò åãî â ãëîáàëüíûé $link_db.
|
||||
$base = $GLOBALS['PHPShopBase'] ?? null;
|
||||
$link = is_object($base) && isset($base->link_db) ? $base->link_db : ($GLOBALS['link_db'] ?? null);
|
||||
if (!$link instanceof mysqli) {
|
||||
throw new RuntimeException('Íåò ñîåäèíåíèÿ ÿäðà PHPShop ñ ÁÄ.');
|
||||
}
|
||||
|
||||
return new self($link);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, int|string|null> $params
|
||||
*
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
public function fetchAll(string $sql, array $params = []): array
|
||||
{
|
||||
$statement = $this->run($sql, $params);
|
||||
try {
|
||||
$result = $statement->get_result();
|
||||
if ($result === false) {
|
||||
throw new RuntimeException('Çàïðîñ íå âåðíóë ñòðîê: ' . $statement->error, $statement->errno);
|
||||
}
|
||||
$rows = [];
|
||||
while (is_array($row = $result->fetch_assoc())) {
|
||||
$rows[] = $row;
|
||||
}
|
||||
|
||||
return $rows;
|
||||
} finally {
|
||||
$statement->close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, int|string|null> $params
|
||||
*
|
||||
* @return mixed ïåðâàÿ êîëîíêà ïåðâîé ñòðîêè èëè null
|
||||
*/
|
||||
public function fetchValue(string $sql, array $params = [])
|
||||
{
|
||||
$rows = $this->fetchAll($sql, $params);
|
||||
if ($rows === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return reset($rows[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, int|string|null> $params
|
||||
*
|
||||
* @return int ÷èñëî çàòðîíóòûõ ñòðîê
|
||||
*/
|
||||
public function execute(string $sql, array $params = []): int
|
||||
{
|
||||
$statement = $this->run($sql, $params);
|
||||
$affected = $statement->affected_rows;
|
||||
$statement->close();
|
||||
|
||||
return (int) $affected;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, int|string|null> $params
|
||||
*/
|
||||
private function run(string $sql, array $params): mysqli_stmt
|
||||
{
|
||||
try {
|
||||
$statement = $this->link->prepare($sql);
|
||||
if ($statement === false) {
|
||||
throw new RuntimeException('Çàïðîñ íå ïîäãîòîâëåí: ' . $this->link->error, $this->link->errno);
|
||||
}
|
||||
if ($params !== []) {
|
||||
$types = '';
|
||||
foreach ($params as $param) {
|
||||
$types .= is_int($param) ? 'i' : 's';
|
||||
}
|
||||
$statement->bind_param($types, ...$params);
|
||||
}
|
||||
if (!$statement->execute()) {
|
||||
$error = new RuntimeException('Çàïðîñ íå âûïîëíåí: ' . $statement->error, $statement->errno);
|
||||
$statement->close();
|
||||
|
||||
throw $error;
|
||||
}
|
||||
|
||||
return $statement;
|
||||
} catch (mysqli_sql_exception $exception) {
|
||||
throw new RuntimeException(
|
||||
'Îøèáêà MySQL: ' . $exception->getMessage(),
|
||||
(int) $exception->getCode(),
|
||||
$exception,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user