Exceptions
1 year ago
Connection.php
1 year ago
ConvertParameters.php
1 year ago
Driver.php
1 year ago
Result.php
2 months ago
Statement.php
1 year ago
index.php
1 year ago
ConvertParameters.php
88 lines
| 1 | <?php declare (strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Doctrine\WPDB; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Doctrine\WPDB\Exceptions\MissingParameterException; |
| 9 | use MailPoetVendor\Doctrine\DBAL\ParameterType; |
| 10 | use MailPoetVendor\Doctrine\DBAL\SQL\Parser\Visitor; |
| 11 | |
| 12 | class ConvertParameters implements Visitor { |
| 13 | private const PARAM_TYPE_MAP = [ |
| 14 | ParameterType::STRING => '%s', |
| 15 | ParameterType::INTEGER => '%d', |
| 16 | ParameterType::ASCII => '%s', |
| 17 | ParameterType::BINARY => '%s', |
| 18 | ParameterType::BOOLEAN => '%d', |
| 19 | ParameterType::NULL => '%s', |
| 20 | ParameterType::LARGE_OBJECT => '%s', |
| 21 | ]; |
| 22 | |
| 23 | /** @var list<string> */ |
| 24 | private array $buffer = []; |
| 25 | |
| 26 | /** @var array<array-key, array{0: string, 1: mixed, 2: int}> */ |
| 27 | private array $params; |
| 28 | |
| 29 | private array $values = []; |
| 30 | |
| 31 | private int $cursor = 1; |
| 32 | |
| 33 | public function __construct( |
| 34 | array $params |
| 35 | ) { |
| 36 | $this->params = $params; |
| 37 | } |
| 38 | |
| 39 | public function acceptPositionalParameter(string $sql): void { |
| 40 | $position = $this->cursor++; |
| 41 | $this->acceptParameter($position); |
| 42 | } |
| 43 | |
| 44 | public function acceptNamedParameter(string $sql): void { |
| 45 | $this->acceptParameter(trim($sql, ':')); |
| 46 | } |
| 47 | |
| 48 | public function acceptOther(string $sql): void { |
| 49 | $this->buffer[] = $sql; |
| 50 | } |
| 51 | |
| 52 | public function getSQL(): string { |
| 53 | return implode('', $this->buffer); |
| 54 | } |
| 55 | |
| 56 | public function getValues(): array { |
| 57 | return $this->values; |
| 58 | } |
| 59 | |
| 60 | /** @param array-key $key */ |
| 61 | private function acceptParameter($key): void { |
| 62 | if (!array_key_exists($key, $this->params)) { |
| 63 | throw new MissingParameterException(sprintf("Parameter '%s' was defined in the query, but not provided.", $key)); |
| 64 | } |
| 65 | [, $value, $type] = $this->params[$key]; |
| 66 | |
| 67 | // WPDB doesn't support NULL values. We need to handle them explicitly. |
| 68 | if ($value === null) { |
| 69 | $this->buffer[] = 'NULL'; |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | // WPDB doesn't accept non-scalar values. We need to cast them (PDO-like behavior). |
| 74 | if (!is_scalar($value)) { |
| 75 | if ($type === ParameterType::INTEGER) { |
| 76 | $value = (int)$value; // @phpstan-ignore-line -- cast may fail and that's OK |
| 77 | } elseif ($type === ParameterType::BOOLEAN) { |
| 78 | $value = (bool)$value; |
| 79 | } else { |
| 80 | $value = (string)$value; // @phpstan-ignore-line -- cast may fail and that's OK |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | $this->values[] = $value; |
| 85 | $this->buffer[] = self::PARAM_TYPE_MAP[$type] ?? '%s'; |
| 86 | } |
| 87 | } |
| 88 |