-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathLikeCondition.php
118 lines (99 loc) · 3.38 KB
/
LikeCondition.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
declare(strict_types=1);
namespace Yiisoft\Db\QueryBuilder\Condition;
use Iterator;
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\QueryBuilder\Condition\Interface\LikeConditionInterface;
use function array_key_exists;
use function is_array;
use function is_int;
use function is_string;
/**
* Condition that represents `LIKE` operator.
*/
final class LikeCondition implements LikeConditionInterface
{
protected array|null $escapingReplacements = [];
public function __construct(
private string|ExpressionInterface $column,
private string $operator,
private array|int|string|Iterator|ExpressionInterface|null $value
) {
}
public function getColumn(): string|ExpressionInterface
{
return $this->column;
}
public function getEscapingReplacements(): ?array
{
return $this->escapingReplacements;
}
public function getOperator(): string
{
return $this->operator;
}
public function getValue(): array|int|string|Iterator|ExpressionInterface|null
{
return $this->value;
}
public function setEscapingReplacements(array|null $escapingReplacements): void
{
$this->escapingReplacements = $escapingReplacements;
}
/**
* 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): self
{
if (!isset($operands[0], $operands[1])) {
throw new InvalidArgumentException("Operator '$operator' requires two operands.");
}
$condition = new self(
self::validateColumn($operator, $operands[0]),
$operator,
self::validateValue($operator, $operands[1]),
);
if (array_key_exists(2, $operands) && (is_array($operands[2]) || $operands[2] === null)) {
$condition->setEscapingReplacements($operands[2]);
}
return $condition;
}
/**
* Validates the given column to be `string` or `ExpressionInterface`.
*
* @throws InvalidArgumentException
*/
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 `string`, `array`, `Iterator` or `ExpressionInterface`.
*
* @throws InvalidArgumentException If the values aren't `string`, `array`, `Iterator` or `ExpressionInterface`.
*/
private static function validateValue(
string $operator,
mixed $value
): array|int|string|Iterator|ExpressionInterface|null {
if (
is_string($value) ||
is_array($value) ||
is_int($value) ||
$value instanceof Iterator ||
$value instanceof ExpressionInterface ||
$value === null
) {
return $value;
}
throw new InvalidArgumentException(
"Operator '$operator' requires value to be string, array, Iterator or ExpressionInterface."
);
}
}