Skip to content

Commit 143b632

Browse files
committed
PHPORM-232 Support whereLike and whereNotLike
1 parent ce05d9d commit 143b632

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
44
## [4.8.0] - next
55

66
* Add `Query\Builder::incrementEach()` and `decrementEach()` methods by @SmallRuralDog in [#2550](https://github.com/mongodb/laravel-mongodb/pull/2550)
7+
* Add `Query\Builder::whereLike()` and `whereNotLike()` methods by @GromNaN in [#3108](https://github.com/mongodb/laravel-mongodb/pull/3108)
78
* Deprecate `Connection::collection()` and `Schema\Builder::collection()` methods by @GromNaN in [#3062](https://github.com/mongodb/laravel-mongodb/pull/3062)
89
* Deprecate `Model::$collection` property to customize collection name. Use `$table` instead by @GromNaN in [#3064](https://github.com/mongodb/laravel-mongodb/pull/3064)
910

src/Query/Builder.php

+9-1
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

+30
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)