ArrayParameters
2 years ago
Cache
9 months ago
Driver
9 months ago
Event
2 years ago
Exception
9 months ago
Id
2 years ago
Logging
2 years ago
Platforms
9 months ago
Portability
2 years ago
Query
2 years ago
SQL
9 months ago
Schema
8 months ago
Types
9 months ago
ArrayParameterType.php
2 years ago
ColumnCase.php
2 years ago
Configuration.php
9 months ago
Connection.php
9 months ago
ConnectionException.php
2 years ago
Driver.php
2 years ago
DriverManager.php
2 years ago
Events.php
2 years ago
Exception.php
2 years ago
ExpandArrayParameters.php
2 years ago
FetchMode.php
2 years ago
LockMode.php
2 years ago
ParameterType.php
2 years ago
Query.php
2 years ago
Result.php
2 years ago
Statement.php
2 years ago
TransactionIsolationLevel.php
2 years ago
VersionAwarePlatformDriver.php
2 years ago
index.php
2 years ago
ExpandArrayParameters.php
88 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Doctrine\DBAL; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Doctrine\DBAL\ArrayParameters\Exception\MissingNamedParameter; |
| 5 | use MailPoetVendor\Doctrine\DBAL\ArrayParameters\Exception\MissingPositionalParameter; |
| 6 | use MailPoetVendor\Doctrine\DBAL\SQL\Parser\Visitor; |
| 7 | use MailPoetVendor\Doctrine\DBAL\Types\Type; |
| 8 | use function array_fill; |
| 9 | use function array_key_exists; |
| 10 | use function count; |
| 11 | use function implode; |
| 12 | use function substr; |
| 13 | final class ExpandArrayParameters implements Visitor |
| 14 | { |
| 15 | private array $originalParameters; |
| 16 | private array $originalTypes; |
| 17 | private int $originalParameterIndex = 0; |
| 18 | private array $convertedSQL = []; |
| 19 | private array $convertedParameters = []; |
| 20 | private array $convertedTypes = []; |
| 21 | public function __construct(array $parameters, array $types) |
| 22 | { |
| 23 | $this->originalParameters = $parameters; |
| 24 | $this->originalTypes = $types; |
| 25 | } |
| 26 | public function acceptPositionalParameter(string $sql) : void |
| 27 | { |
| 28 | $index = $this->originalParameterIndex; |
| 29 | if (!array_key_exists($index, $this->originalParameters)) { |
| 30 | throw MissingPositionalParameter::new($index); |
| 31 | } |
| 32 | $this->acceptParameter($index, $this->originalParameters[$index]); |
| 33 | $this->originalParameterIndex++; |
| 34 | } |
| 35 | public function acceptNamedParameter(string $sql) : void |
| 36 | { |
| 37 | $name = substr($sql, 1); |
| 38 | if (!array_key_exists($name, $this->originalParameters)) { |
| 39 | throw MissingNamedParameter::new($name); |
| 40 | } |
| 41 | $this->acceptParameter($name, $this->originalParameters[$name]); |
| 42 | } |
| 43 | public function acceptOther(string $sql) : void |
| 44 | { |
| 45 | $this->convertedSQL[] = $sql; |
| 46 | } |
| 47 | public function getSQL() : string |
| 48 | { |
| 49 | return implode('', $this->convertedSQL); |
| 50 | } |
| 51 | public function getParameters() : array |
| 52 | { |
| 53 | return $this->convertedParameters; |
| 54 | } |
| 55 | private function acceptParameter($key, $value) : void |
| 56 | { |
| 57 | if (!isset($this->originalTypes[$key])) { |
| 58 | $this->convertedSQL[] = '?'; |
| 59 | $this->convertedParameters[] = $value; |
| 60 | return; |
| 61 | } |
| 62 | $type = $this->originalTypes[$key]; |
| 63 | if ($type !== ArrayParameterType::INTEGER && $type !== ArrayParameterType::STRING && $type !== ArrayParameterType::ASCII && $type !== ArrayParameterType::BINARY) { |
| 64 | $this->appendTypedParameter([$value], $type); |
| 65 | return; |
| 66 | } |
| 67 | if (count($value) === 0) { |
| 68 | $this->convertedSQL[] = 'NULL'; |
| 69 | return; |
| 70 | } |
| 71 | $this->appendTypedParameter($value, ArrayParameterType::toElementParameterType($type)); |
| 72 | } |
| 73 | public function getTypes() : array |
| 74 | { |
| 75 | return $this->convertedTypes; |
| 76 | } |
| 77 | private function appendTypedParameter(array $values, $type) : void |
| 78 | { |
| 79 | $this->convertedSQL[] = implode(', ', array_fill(0, count($values), '?')); |
| 80 | $index = count($this->convertedParameters); |
| 81 | foreach ($values as $value) { |
| 82 | $this->convertedParameters[] = $value; |
| 83 | $this->convertedTypes[$index] = $type; |
| 84 | $index++; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 |