|
16 | 16 | use PhpDb\Sql\Argument\Value; |
17 | 17 | use PhpDb\Sql\Argument\Values; |
18 | 18 | use PhpDb\Sql\Platform\PlatformDecoratorInterface; |
| 19 | +use Stringable; |
19 | 20 |
|
20 | 21 | use function array_key_exists; |
21 | 22 | use function array_key_first; |
|
27 | 28 | use function preg_match; |
28 | 29 | use function preg_replace; |
29 | 30 | use function rtrim; |
| 31 | +use function sprintf; |
30 | 32 | use function str_replace; |
31 | 33 | use function strtoupper; |
32 | 34 | use function vsprintf; |
|
36 | 38 | * |
37 | 39 | * @psalm-type ParameterSpecification = array<int|string, string>|null |
38 | 40 | * @psalm-type Specification = string|array<string, list<ParameterSpecification>> |
39 | | - * @psalm-type SqlParameters = array<array-key, string|array<array-key, string|array<array-key, string>>> |
| 41 | + * @psalm-type SqlParameterValue = string|int|float|bool|Stringable|null |
| 42 | + * @psalm-type SqlParameters = array< |
| 43 | + * array-key, |
| 44 | + * SqlParameterValue|array<array-key, SqlParameterValue|array<array-key, SqlParameterValue>>, |
| 45 | + * > |
40 | 46 | * @psalm-type ColumnValue = Select|ExpressionInterface|string|int|float|bool|null |
41 | 47 | * @psalm-type ColumnReference = array{ |
42 | 48 | * column: ColumnValue, |
@@ -572,27 +578,27 @@ protected function resolveTable( |
572 | 578 | } |
573 | 579 |
|
574 | 580 | /** |
575 | | - * @param array<array-key, string|array<array-key, mixed>> $values |
| 581 | + * @param array<array-key, SqlParameterValue|array<array-key, mixed>> $values |
576 | 582 | * @return list<string> |
577 | | - * @throws Exception\RuntimeException When a value is a nested list rather than a string. |
| 583 | + * @throws Exception\RuntimeException When a value is a nested list rather than a scalar. |
578 | 584 | */ |
579 | 585 | private function flattenParameterList(array $values): array |
580 | 586 | { |
581 | 587 | $strings = []; |
582 | 588 | foreach ($values as $value) { |
583 | | - if (! is_string($value)) { |
| 589 | + if (is_array($value)) { |
584 | 590 | throw Exception\RuntimeException::forUnsupportedParameterCountOf(count($value)); |
585 | 591 | } |
586 | 592 |
|
587 | | - $strings[] = $value; |
| 593 | + $strings[] = $this->stringifyParameter($value); |
588 | 594 | } |
589 | 595 |
|
590 | 596 | return $strings; |
591 | 597 | } |
592 | 598 |
|
593 | 599 | /** |
594 | 600 | * @param array<int|string, string> $parameterSpecification |
595 | | - * @param array<array-key, string|array<array-key, mixed>> $values |
| 601 | + * @param array<array-key, SqlParameterValue|array<array-key, mixed>> $values |
596 | 602 | * @throws Exception\RuntimeException When no format accepts the number of values. |
597 | 603 | */ |
598 | 604 | private function formatParameterList(array $parameterSpecification, array $values): string |
@@ -633,27 +639,42 @@ private function quoteJoinTableIdentifier(TableIdentifier $identifier, PlatformI |
633 | 639 |
|
634 | 640 | /** |
635 | 641 | * @param ParameterSpecification $parameterSpecification |
636 | | - * @param string|array<array-key, string|array<array-key, string>> $parameter |
| 642 | + * @param SqlParameterValue|array<array-key, SqlParameterValue|array<array-key, SqlParameterValue>> $parameter |
637 | 643 | * @throws Exception\RuntimeException |
638 | 644 | */ |
639 | | - private function renderParameter(?array $parameterSpecification, string|array $parameter): string |
640 | | - { |
| 645 | + private function renderParameter( |
| 646 | + ?array $parameterSpecification, |
| 647 | + string|int|float|bool|Stringable|array|null $parameter, |
| 648 | + ): string { |
641 | 649 | if (null === $parameterSpecification) { |
642 | | - return is_string($parameter) |
643 | | - ? $parameter |
644 | | - : throw Exception\RuntimeException::forUnsupportedParameterCountOf(count($parameter)); |
| 650 | + return is_array($parameter) |
| 651 | + ? throw Exception\RuntimeException::forUnsupportedParameterCountOf(count($parameter)) |
| 652 | + : $this->stringifyParameter($parameter); |
645 | 653 | } |
646 | 654 |
|
| 655 | + $parameters = is_array($parameter) ? $parameter : [$parameter]; |
| 656 | + |
647 | 657 | $combinedBy = $parameterSpecification['combinedby'] ?? null; |
648 | 658 | if (null === $combinedBy) { |
649 | | - return $this->formatParameterList($parameterSpecification, (array) $parameter); |
| 659 | + return $this->formatParameterList($parameterSpecification, $parameters); |
650 | 660 | } |
651 | 661 |
|
652 | 662 | $rendered = []; |
653 | | - foreach ((array) $parameter as $values) { |
654 | | - $rendered[] = $this->formatParameterList($parameterSpecification, (array) $values); |
| 663 | + foreach ($parameters as $values) { |
| 664 | + $rendered[] = $this->formatParameterList( |
| 665 | + $parameterSpecification, |
| 666 | + is_array($values) ? $values : [$values], |
| 667 | + ); |
655 | 668 | } |
656 | 669 |
|
657 | 670 | return implode($combinedBy, $rendered); |
658 | 671 | } |
| 672 | + |
| 673 | + /** |
| 674 | + * Converts a scalar clause parameter to the text vsprintf's %s conversion would produce. |
| 675 | + */ |
| 676 | + private function stringifyParameter(string|int|float|bool|Stringable|null $value): string |
| 677 | + { |
| 678 | + return is_string($value) ? $value : sprintf('%s', $value); |
| 679 | + } |
659 | 680 | } |
0 commit comments