Skip to content

Commit

Permalink
Refactor JsonExpressionBuilder.php (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Jul 28, 2023
1 parent 16c4eee commit 79527de
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

## 1.1.1 under development

- Enh #301: Refactor `JsonExpressionBuilder` (@Tigrov)
- Enh #300: Refactor `ArrayExpressionBuilder` (@Tigrov)

## 1.1.0 July 24, 2023
Expand Down
14 changes: 5 additions & 9 deletions src/Builder/JsonExpressionBuilder.php
Expand Up @@ -29,7 +29,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 to build.
* @param JsonExpression $expression The expression to build.
* @param array $params The binding parameters.
*
* @throws Exception
Expand All @@ -39,19 +39,15 @@ public function __construct(private QueryBuilderInterface $queryBuilder)
* @throws NotSupportedException
*
* @return string The raw SQL that won't be additionally escaped or quoted.
*
* @psalm-param JsonExpression $expression
*/
public function build(ExpressionInterface $expression, array &$params = []): string
{
/**
* @psalm-var array|mixed|QueryInterface $value
*/
/** @psalm-var mixed $value */
$value = $expression->getValue();

if ($value instanceof QueryInterface) {
[$sql, $params] = $this->queryBuilder->build($value, $params);
return "($sql)" . $this->getTypecast($expression);
return "($sql)" . $this->getTypeHint($expression);
}

if ($value instanceof ArrayExpression) {
Expand All @@ -60,13 +56,13 @@ public function build(ExpressionInterface $expression, array &$params = []): str
$placeholder = $this->queryBuilder->bindParam(Json::encode($value), $params);
}

return $placeholder . $this->getTypecast($expression);
return $placeholder . $this->getTypeHint($expression);
}

/**
* @return string The typecast expression based on {@see JsonExpression::getType()}.
*/
protected function getTypecast(JsonExpression $expression): string
private function getTypeHint(JsonExpression $expression): string
{
$type = $expression->getType();

Expand Down

0 comments on commit 79527de

Please sign in to comment.