|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\QueryBuilder\Conditions\Builder; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Db\Exception\Exception; |
|
8
|
|
|
use Yiisoft\Db\Exception\InvalidArgumentException; |
|
9
|
|
|
use Yiisoft\Db\Exception\InvalidConfigException; |
|
10
|
|
|
use Yiisoft\Db\Exception\NotSupportedException; |
|
11
|
|
|
use Yiisoft\Db\Expression\ExpressionBuilderInterface; |
|
12
|
|
|
use Yiisoft\Db\Expression\ExpressionInterface; |
|
13
|
|
|
use Yiisoft\Db\QueryBuilder\Conditions\ConjunctionCondition; |
|
14
|
|
|
use Yiisoft\Db\QueryBuilder\Conditions\Interface\ConjunctionConditionInterface; |
|
15
|
|
|
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; |
|
16
|
|
|
|
|
17
|
|
|
use function count; |
|
18
|
|
|
use function implode; |
|
19
|
|
|
use function is_array; |
|
20
|
|
|
use function reset; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class ConjunctionConditionBuilder builds objects of abstract class {@see ConjunctionCondition}. |
|
24
|
|
|
*/ |
|
25
|
|
|
class ConjunctionConditionBuilder implements ExpressionBuilderInterface |
|
26
|
|
|
{ |
|
27
|
|
|
public function __construct(private QueryBuilderInterface $queryBuilder) |
|
28
|
|
|
{ |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException |
|
33
|
|
|
*/ |
|
34
|
|
|
public function build(ConjunctionConditionInterface $expression, array &$params = []): string |
|
35
|
|
|
{ |
|
36
|
|
|
/** @psalm-var string[] */ |
|
37
|
|
|
$parts = $this->buildExpressionsFrom($expression, $params); |
|
38
|
|
|
|
|
39
|
|
|
if (empty($parts)) { |
|
40
|
|
|
return ''; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if (count($parts) === 1) { |
|
44
|
|
|
return reset($parts); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return '(' . implode(") {$expression->getOperator()} (", $parts) . ')'; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Builds expressions, that are stored in $condition. |
|
52
|
|
|
* |
|
53
|
|
|
* @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException |
|
54
|
|
|
*/ |
|
55
|
|
|
private function buildExpressionsFrom(ConjunctionConditionInterface $condition, array &$params = []): array |
|
56
|
|
|
{ |
|
57
|
|
|
$parts = []; |
|
58
|
|
|
|
|
59
|
|
|
/** @psalm-var array<array-key, array|ExpressionInterface|string> $expressions */ |
|
60
|
|
|
$expressions = $condition->getExpressions(); |
|
61
|
|
|
|
|
62
|
|
|
foreach ($expressions as $conditionValue) { |
|
63
|
|
|
if (is_array($conditionValue)) { |
|
64
|
|
|
$conditionValue = $this->queryBuilder->buildCondition($conditionValue, $params); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ($conditionValue instanceof ExpressionInterface) { |
|
68
|
|
|
$conditionValue = $this->queryBuilder->buildExpression($conditionValue, $params); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if ($conditionValue !== '') { |
|
72
|
|
|
$parts[] = $conditionValue; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $parts; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|