|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\QueryBuilder\Conditions; |
|
6
|
|
|
|
|
7
|
|
|
use Iterator; |
|
8
|
|
|
use Yiisoft\Db\Exception\InvalidArgumentException; |
|
9
|
|
|
use Yiisoft\Db\Expression\Expression; |
|
10
|
|
|
use Yiisoft\Db\Expression\ExpressionInterface; |
|
11
|
|
|
use Yiisoft\Db\QueryBuilder\Conditions\Interface\LikeConditionInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class LikeCondition represents a `LIKE` condition. |
|
15
|
|
|
*/ |
|
16
|
|
|
final class LikeCondition implements LikeConditionInterface |
|
17
|
|
|
{ |
|
18
|
|
|
protected array|null $escapingReplacements = []; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct( |
|
21
|
|
|
private string|Expression $column, |
|
22
|
|
|
private string $operator, |
|
23
|
|
|
private array|int|string|Iterator|ExpressionInterface|null $value |
|
24
|
|
|
) { |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function getColumn(): string|Expression |
|
28
|
|
|
{ |
|
29
|
|
|
return $this->column; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function getEscapingReplacements(): ?array |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->escapingReplacements; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function getOperator(): string |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->operator; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function getValue(): array|int|string|Iterator|ExpressionInterface|null |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->value; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function setEscapingReplacements(array|null $escapingReplacements): void |
|
48
|
|
|
{ |
|
49
|
|
|
$this->escapingReplacements = $escapingReplacements; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @throws InvalidArgumentException |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function fromArrayDefinition(string $operator, array $operands): self |
|
56
|
|
|
{ |
|
57
|
|
|
if (!isset($operands[0], $operands[1])) { |
|
58
|
|
|
throw new InvalidArgumentException("Operator '$operator' requires two operands."); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$condition = new self( |
|
62
|
|
|
self::validateColumn($operator, $operands[0]), |
|
63
|
|
|
$operator, |
|
64
|
|
|
self::validateValue($operator, $operands[1]), |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
if (array_key_exists(2, $operands) && is_array($operands[2])) { |
|
68
|
|
|
$condition->setEscapingReplacements($operands[2]); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $condition; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public static function validateColumn(string $operator, mixed $operand): string|Expression |
|
75
|
|
|
{ |
|
76
|
|
|
if (!is_string($operand) && !$operand instanceof Expression) { |
|
77
|
|
|
throw new InvalidArgumentException("Operator '$operator' requires column to be string or Expression."); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $operand; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public static function validateValue( |
|
84
|
|
|
string $operator, |
|
85
|
|
|
mixed $operand |
|
86
|
|
|
): array|int|string|Iterator|ExpressionInterface|null { |
|
87
|
|
|
if (!is_string($operand) && !is_array($operand) && !$operand instanceof Iterator && !$operand instanceof ExpressionInterface) { |
|
88
|
|
|
throw new InvalidArgumentException( |
|
89
|
|
|
"Operator '$operator' requires value to be string, array, Iterator or ExpressionInterface." |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $operand; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|