Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ArrayExpressionBuilder.php #300

Merged
merged 12 commits into from
Jul 28, 2023
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 1.1.1 under development

- no changes in this release.
- Enh #300: Refactor `ArrayExpressionBuilder` (@Tigrov)

## 1.1.0 July 24, 2023

Expand Down
32 changes: 12 additions & 20 deletions src/Builder/ArrayExpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Yiisoft\Db\Pgsql\Builder;

use Traversable;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Exception\InvalidConfigException;
Expand All @@ -19,7 +18,7 @@

use function implode;
use function in_array;
use function is_array;
use function is_iterable;
use function str_repeat;

/**
Expand All @@ -34,7 +33,7 @@ public function __construct(private QueryBuilderInterface $queryBuilder)
/**
* The Method builds the raw SQL from the expression that won't be additionally escaped or quoted.
*
* @param ExpressionInterface $expression The expression build.
* @param ArrayExpression $expression The expression build.
* @param array $params The binding parameters.
*
* @throws Exception
Expand All @@ -43,8 +42,6 @@ public function __construct(private QueryBuilderInterface $queryBuilder)
* @throws NotSupportedException
*
* @return string The raw SQL that won't be additionally escaped or quoted.
*
* @psalm-param ArrayExpression $expression
*/
public function build(ExpressionInterface $expression, array &$params = []): string
{
Expand All @@ -63,7 +60,7 @@ public function build(ExpressionInterface $expression, array &$params = []): str
/** @psalm-var string[] $placeholders */
$placeholders = $this->buildPlaceholders($expression, $params);

return 'ARRAY[' . implode(', ', $placeholders) . ']' . $this->getTypehint($expression);
return 'ARRAY[' . implode(', ', $placeholders) . ']' . $this->getTypeHint($expression);
}

/**
Expand All @@ -75,22 +72,20 @@ public function build(ExpressionInterface $expression, array &$params = []): str
* @throws InvalidArgumentException
* @throws InvalidConfigException
* @throws NotSupportedException
*
* @psalm-param ArrayExpression $expression
*/
protected function buildPlaceholders(ExpressionInterface $expression, array &$params): array
private function buildPlaceholders(ArrayExpression $expression, array &$params): array
{
$placeholders = [];

/** @psalm-var mixed $value */
$value = $expression->getValue();

if (!is_array($value) && !$value instanceof Traversable) {
if (!is_iterable($value)) {
return $placeholders;
}

if ($expression->getDimension() > 1) {
/** @psalm-var ExpressionInterface|int $item */
/** @psalm-var mixed $item */
foreach ($value as $item) {
$placeholders[] = $this->build($this->unnestArrayExpression($expression, $item), $params);
}
Expand All @@ -109,10 +104,9 @@ protected function buildPlaceholders(ExpressionInterface $expression, array &$pa

if ($item instanceof ExpressionInterface) {
$placeholders[] = $this->queryBuilder->buildExpression($item, $params);
continue;
} else {
$placeholders[] = $this->queryBuilder->bindParam($item, $params);
}

$placeholders[] = $this->queryBuilder->bindParam($item, $params);
}

return $placeholders;
Expand All @@ -126,7 +120,7 @@ private function unnestArrayExpression(ArrayExpression $expression, mixed $value
/**
* @return string The typecast expression based on {@see type}.
*/
protected function getTypeHint(ArrayExpression $expression): string
private function getTypeHint(ArrayExpression $expression): string
{
$type = $expression->getType();

Expand All @@ -135,10 +129,8 @@ protected function getTypeHint(ArrayExpression $expression): string
}

$dimension = $expression->getDimension();
$result = '::' . $type;
$result .= str_repeat('[]', $dimension);

return $result;
return '::' . $type . str_repeat('[]', $dimension);
}

/**
Expand All @@ -149,15 +141,15 @@ protected function getTypeHint(ArrayExpression $expression): string
*
* @return string The sub-query array expression.
*/
protected function buildSubqueryArray(string $sql, ArrayExpression $expression): string
private function buildSubqueryArray(string $sql, ArrayExpression $expression): string
{
return 'ARRAY(' . $sql . ')' . $this->getTypeHint($expression);
}

/**
* @return array|bool|ExpressionInterface|float|int|JsonExpression|string|null The cast value or expression.
*/
protected function typecastValue(
private function typecastValue(
ArrayExpression $expression,
array|bool|float|int|string|ExpressionInterface|null $value
): array|bool|float|int|string|JsonExpression|ExpressionInterface|null {
Expand Down