-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add array and json overlaps conditions (#855)
- Loading branch information
Showing
11 changed files
with
268 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\QueryBuilder\Condition; | ||
|
||
use Yiisoft\Db\Exception\InvalidArgumentException; | ||
use Yiisoft\Db\Expression\ExpressionInterface; | ||
use Yiisoft\Db\QueryBuilder\Condition\Interface\OverlapsConditionInterface; | ||
|
||
use function is_iterable; | ||
use function is_string; | ||
|
||
/** | ||
* The base class for classes representing the array and JSON overlaps conditions. | ||
*/ | ||
abstract class AbstractOverlapsCondition implements OverlapsConditionInterface | ||
{ | ||
public function __construct( | ||
private string|ExpressionInterface $column, | ||
private iterable|ExpressionInterface $values, | ||
) { | ||
} | ||
|
||
public function getColumn(): string|ExpressionInterface | ||
{ | ||
return $this->column; | ||
} | ||
|
||
public function getValues(): iterable|ExpressionInterface | ||
{ | ||
return $this->values; | ||
} | ||
|
||
/** | ||
* Creates a condition based on the given operator and operands. | ||
* | ||
* @throws InvalidArgumentException If the number of operands isn't 2. | ||
*/ | ||
public static function fromArrayDefinition(string $operator, array $operands): static | ||
{ | ||
if (!isset($operands[0], $operands[1])) { | ||
throw new InvalidArgumentException("Operator \"$operator\" requires two operands."); | ||
} | ||
|
||
/** @psalm-suppress UnsafeInstantiation */ | ||
return new static( | ||
self::validateColumn($operator, $operands[0]), | ||
self::validateValues($operator, $operands[1]) | ||
); | ||
} | ||
|
||
/** | ||
* Validates the given column to be string or `ExpressionInterface`. | ||
* | ||
* @throws InvalidArgumentException If the column isn't a string or `ExpressionInterface`. | ||
*/ | ||
private static function validateColumn(string $operator, mixed $column): string|ExpressionInterface | ||
{ | ||
if (is_string($column) || $column instanceof ExpressionInterface) { | ||
return $column; | ||
} | ||
|
||
throw new InvalidArgumentException( | ||
"Operator \"$operator\" requires column to be string or ExpressionInterface." | ||
); | ||
} | ||
|
||
/** | ||
* Validates the given values to be `iterable` or `ExpressionInterface`. | ||
* | ||
* @throws InvalidArgumentException If the values aren't an `iterable` or `ExpressionInterface`. | ||
*/ | ||
private static function validateValues(string $operator, mixed $values): iterable|ExpressionInterface | ||
{ | ||
if (is_iterable($values) || $values instanceof ExpressionInterface) { | ||
return $values; | ||
} | ||
|
||
throw new InvalidArgumentException( | ||
"Operator \"$operator\" requires values to be iterable or ExpressionInterface." | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\QueryBuilder\Condition; | ||
|
||
/** | ||
* Condition that represents `ARRAY OVERLAPS` operator is used to check if a column of array type overlaps another array. | ||
*/ | ||
final class ArrayOverlapsCondition extends AbstractOverlapsCondition | ||
{ | ||
} |
28 changes: 28 additions & 0 deletions
28
src/QueryBuilder/Condition/Builder/AbstractOverlapsConditionBuilder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\QueryBuilder\Condition\Builder; | ||
|
||
use Yiisoft\Db\Expression\ExpressionBuilderInterface; | ||
use Yiisoft\Db\Expression\ExpressionInterface; | ||
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; | ||
|
||
/** | ||
* The base class for classes building SQL expressions for array and JSON overlaps conditions. | ||
*/ | ||
abstract class AbstractOverlapsConditionBuilder implements ExpressionBuilderInterface | ||
{ | ||
public function __construct(protected QueryBuilderInterface $queryBuilder) | ||
{ | ||
} | ||
|
||
protected function prepareColumn(ExpressionInterface|string $column): string | ||
{ | ||
if ($column instanceof ExpressionInterface) { | ||
return $this->queryBuilder->buildExpression($column); | ||
} | ||
|
||
return $this->queryBuilder->quoter()->quoteColumnName($column); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/QueryBuilder/Condition/Interface/OverlapsConditionInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\QueryBuilder\Condition\Interface; | ||
|
||
use Yiisoft\Db\Expression\ExpressionInterface; | ||
|
||
/** | ||
* Represents array and JSON overlaps conditions. | ||
*/ | ||
interface OverlapsConditionInterface extends ConditionInterface | ||
{ | ||
/** | ||
* @return ExpressionInterface|string The column name or an Expression. | ||
*/ | ||
public function getColumn(): string|ExpressionInterface; | ||
|
||
/** | ||
* @return ExpressionInterface|iterable An array of values that {@see columns} value should overlap. | ||
*/ | ||
public function getValues(): iterable|ExpressionInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\QueryBuilder\Condition; | ||
|
||
/** | ||
* Condition that represents `JSON OVERLAPS` operator and is used to check if a column of JSON type overlaps an array. | ||
*/ | ||
final class JsonOverlapsCondition extends AbstractOverlapsCondition | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters