Skip to content

Commit aee553b

Browse files
committed
Add contains operator to expressions
1 parent cf3f639 commit aee553b

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/AST/Comparison.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function visit(Executor $param, $currentObject, $root): bool
3232
'==' => $param->evaluateEqualsComparison($root, $currentObject, $this->left, $this->right),
3333
'!=' => $param->evaluateNotEqualsComparison($root, $currentObject, $this->left, $this->right),
3434
'starts_with' => $param->evaluateStartsWithComparison($root, $currentObject, $this->left, $this->right),
35+
'contains' => $param->evaluateContainsComparison($root, $currentObject, $this->left, $this->right),
3536
default => throw new InvalidArgumentException(),
3637
};
3738
}

src/Executor.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,32 @@ public function evaluateStartsWithComparison(
8787
return str_starts_with((string) $leftValue, (string) $rightValue);
8888
}
8989

90+
public function evaluateContainsComparison(
91+
mixed $root,
92+
mixed $currentObject,
93+
QueryNode $left,
94+
QueryNode $right,
95+
): bool {
96+
$leftValue = $this->toValue($this->evaluate($left, $currentObject, $root));
97+
$rightValue = $this->toValue($this->evaluate($right, $currentObject, $root));
98+
99+
if (is_iterable($leftValue)) {
100+
foreach ($leftValue as $value) {
101+
if (is_string($rightValue) && ((string) $value) === $rightValue) {
102+
return true;
103+
}
104+
105+
if ($value === $rightValue) {
106+
return true;
107+
}
108+
}
109+
110+
return false;
111+
}
112+
113+
return false;
114+
}
115+
90116
/**
91117
* @param Generator<mixed>|mixed $value
92118
*

src/Parser/ParserBuilder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ private static function expression(): Parser
9898
string('=='),
9999
string('!='),
100100
string('starts_with'),
101+
string('contains'),
101102
);
102103

103104
$value = choice(

0 commit comments

Comments
 (0)