diff options
Diffstat (limited to 'vendor/doctrine/dbal/src/ExpandArrayParameters.php')
-rw-r--r-- | vendor/doctrine/dbal/src/ExpandArrayParameters.php | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/vendor/doctrine/dbal/src/ExpandArrayParameters.php b/vendor/doctrine/dbal/src/ExpandArrayParameters.php new file mode 100644 index 0000000..253861c --- /dev/null +++ b/vendor/doctrine/dbal/src/ExpandArrayParameters.php | |||
@@ -0,0 +1,128 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\DBAL; | ||
6 | |||
7 | use Doctrine\DBAL\ArrayParameters\Exception\MissingNamedParameter; | ||
8 | use Doctrine\DBAL\ArrayParameters\Exception\MissingPositionalParameter; | ||
9 | use Doctrine\DBAL\SQL\Parser\Visitor; | ||
10 | use Doctrine\DBAL\Types\Type; | ||
11 | |||
12 | use function array_fill; | ||
13 | use function array_key_exists; | ||
14 | use function count; | ||
15 | use function implode; | ||
16 | use function substr; | ||
17 | |||
18 | /** @psalm-import-type WrapperParameterTypeArray from Connection */ | ||
19 | final class ExpandArrayParameters implements Visitor | ||
20 | { | ||
21 | private int $originalParameterIndex = 0; | ||
22 | |||
23 | /** @var list<string> */ | ||
24 | private array $convertedSQL = []; | ||
25 | |||
26 | /** @var list<mixed> */ | ||
27 | private array $convertedParameters = []; | ||
28 | |||
29 | /** @var array<int<0, max>,string|ParameterType|Type> */ | ||
30 | private array $convertedTypes = []; | ||
31 | |||
32 | /** | ||
33 | * @param array<int, mixed>|array<string, mixed> $parameters | ||
34 | * @psalm-param WrapperParameterTypeArray $types | ||
35 | */ | ||
36 | public function __construct( | ||
37 | private readonly array $parameters, | ||
38 | private readonly array $types, | ||
39 | ) { | ||
40 | } | ||
41 | |||
42 | public function acceptPositionalParameter(string $sql): void | ||
43 | { | ||
44 | $index = $this->originalParameterIndex; | ||
45 | |||
46 | if (! array_key_exists($index, $this->parameters)) { | ||
47 | throw MissingPositionalParameter::new($index); | ||
48 | } | ||
49 | |||
50 | $this->acceptParameter($index, $this->parameters[$index]); | ||
51 | |||
52 | $this->originalParameterIndex++; | ||
53 | } | ||
54 | |||
55 | public function acceptNamedParameter(string $sql): void | ||
56 | { | ||
57 | $name = substr($sql, 1); | ||
58 | |||
59 | if (! array_key_exists($name, $this->parameters)) { | ||
60 | throw MissingNamedParameter::new($name); | ||
61 | } | ||
62 | |||
63 | $this->acceptParameter($name, $this->parameters[$name]); | ||
64 | } | ||
65 | |||
66 | public function acceptOther(string $sql): void | ||
67 | { | ||
68 | $this->convertedSQL[] = $sql; | ||
69 | } | ||
70 | |||
71 | public function getSQL(): string | ||
72 | { | ||
73 | return implode('', $this->convertedSQL); | ||
74 | } | ||
75 | |||
76 | /** @return list<mixed> */ | ||
77 | public function getParameters(): array | ||
78 | { | ||
79 | return $this->convertedParameters; | ||
80 | } | ||
81 | |||
82 | private function acceptParameter(int|string $key, mixed $value): void | ||
83 | { | ||
84 | if (! isset($this->types[$key])) { | ||
85 | $this->convertedSQL[] = '?'; | ||
86 | $this->convertedParameters[] = $value; | ||
87 | |||
88 | return; | ||
89 | } | ||
90 | |||
91 | $type = $this->types[$key]; | ||
92 | |||
93 | if (! $type instanceof ArrayParameterType) { | ||
94 | $this->appendTypedParameter([$value], $type); | ||
95 | |||
96 | return; | ||
97 | } | ||
98 | |||
99 | if (count($value) === 0) { | ||
100 | $this->convertedSQL[] = 'NULL'; | ||
101 | |||
102 | return; | ||
103 | } | ||
104 | |||
105 | $this->appendTypedParameter($value, ArrayParameterType::toElementParameterType($type)); | ||
106 | } | ||
107 | |||
108 | /** @return array<int<0, max>,string|ParameterType|Type> */ | ||
109 | public function getTypes(): array | ||
110 | { | ||
111 | return $this->convertedTypes; | ||
112 | } | ||
113 | |||
114 | /** @param list<mixed> $values */ | ||
115 | private function appendTypedParameter(array $values, string|ParameterType|Type $type): void | ||
116 | { | ||
117 | $this->convertedSQL[] = implode(', ', array_fill(0, count($values), '?')); | ||
118 | |||
119 | $index = count($this->convertedParameters); | ||
120 | |||
121 | foreach ($values as $value) { | ||
122 | $this->convertedParameters[] = $value; | ||
123 | $this->convertedTypes[$index] = $type; | ||
124 | |||
125 | $index++; | ||
126 | } | ||
127 | } | ||
128 | } | ||