Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
taka-oyama committed Aug 16, 2023
1 parent 84f253f commit 611523d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,14 +428,27 @@ public function prepareBindings(array $bindings)
*/
public function escape($value, $binary = false)
{
if (is_array($value)) {
if (array_is_list($value)) {
$escaped = array_map(fn (mixed $v): string => $this->escape($v, $binary), $value);
return '[' . implode(', ', $escaped) . ']';
}
throw new LogicException('Associative arrays are not supported');
return is_array($value)
? $this->escapeArray($value, $binary)
: parent::escape($value, $binary);
}

/**
* @param array<array-key, mixed> $value
* @param bool $binary
* @return string
*/
protected function escapeArray(array $value, bool $binary): string
{
if (array_is_list($value)) {
$escaped = array_map(function (mixed $v) use ($binary): string {
return !is_array($v)
? $this->escape($v, $binary)
: throw new LogicException('Nested arrays are not supported by Cloud Spanner');
}, $value);
return '[' . implode(', ', $escaped) . ']';
}
return parent::escape($value, $binary);
throw new LogicException('Associative arrays are not supported');
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,4 +502,13 @@ public function test_escape(): void
self::assertSame('[1]', $conn->escape([1]));
self::assertSame('[1.1]', $conn->escape([1.1]));
}

public function test_escape_nested_array(): void
{
$this->expectExceptionMessage('Nested arrays are not supported by Cloud Spanner');
$this->expectException(LogicException::class);

$conn = $this->getDefaultConnection();
self::assertSame('[]', $conn->escape([[]]));
}
}

0 comments on commit 611523d

Please sign in to comment.