Skip to content

Commit 0d5ffe1

Browse files
committed
PHPORM-232 Support whereLike and whereNotLike
1 parent ce05d9d commit 0d5ffe1

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/Query/Builder.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,8 @@ protected function compileWhereBasic(array $where): array
12661266
// All backslashes are converted to \\, which are needed in matching regexes.
12671267
preg_quote($value),
12681268
);
1269-
$value = new Regex('^' . $regex . '$', 'i');
1269+
$flags = ($where['caseSensitive'] ?? false) ? '' : 'i';
1270+
$value = new Regex('^' . $regex . '$', $flags);
12701271

12711272
// For inverse like operations, we can just use the $not operator with the Regex
12721273
$operator = $operator === 'like' ? '=' : 'not';
@@ -1324,6 +1325,13 @@ protected function compileWhereNotIn(array $where): array
13241325
return [$where['column'] => ['$nin' => array_values($where['values'])]];
13251326
}
13261327

1328+
protected function compileWhereLike(array $where): array
1329+
{
1330+
$where['operator'] = $where['not'] ? 'not like' : 'like';
1331+
1332+
return $this->compileWhereBasic($where);
1333+
}
1334+
13271335
protected function compileWhereNull(array $where): array
13281336
{
13291337
$where['operator'] = '=';

tests/Query/BuilderTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,36 @@ function (Builder $builder) {
748748
fn (Builder $builder) => $builder->where('name', 'like', '_ac__me_'),
749749
];
750750

751+
yield 'whereLike' => [
752+
['find' => [['name' => new Regex('^1$', 'i')], []]],
753+
fn (Builder $builder) => $builder->whereLike('name', '1'),
754+
];
755+
756+
yield 'whereLike case not sensitive' => [
757+
['find' => [['name' => new Regex('^1$', 'i')], []]],
758+
fn (Builder $builder) => $builder->whereLike('name', '1', false),
759+
];
760+
761+
yield 'whereLike case sensitive' => [
762+
['find' => [['name' => new Regex('^1$', '')], []]],
763+
fn (Builder $builder) => $builder->whereLike('name', '1', true),
764+
];
765+
766+
yield 'whereNotLike' => [
767+
['find' => [['name' => ['$not' => new Regex('^1$', 'i')]], []]],
768+
fn (Builder $builder) => $builder->whereNotLike('name', '1'),
769+
];
770+
771+
yield 'whereNotLike case not sensitive' => [
772+
['find' => [['name' => ['$not' => new Regex('^1$', 'i')]], []]],
773+
fn (Builder $builder) => $builder->whereNotLike('name', '1', false),
774+
];
775+
776+
yield 'whereNotLike case sensitive' => [
777+
['find' => [['name' => ['$not' => new Regex('^1$', '')]], []]],
778+
fn (Builder $builder) => $builder->whereNotLike('name', '1', true),
779+
];
780+
751781
$regex = new Regex('^acme$', 'si');
752782
yield 'where BSON\Regex' => [
753783
['find' => [['name' => $regex], []]],

0 commit comments

Comments
 (0)