Skip to content
This repository was archived by the owner on Aug 22, 2023. It is now read-only.

Commit 0c12693

Browse files
committed
Remove ilike and regexp operators
1 parent 01db243 commit 0c12693

File tree

4 files changed

+38
-77
lines changed

4 files changed

+38
-77
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ All notable changes to this project will be documented in this file.
1111
- Throw an exception when `Query\Builder::orderBy()` is used with invalid direction [#7](https://github.com/GromNaN/laravel-mongodb-private/pull/7) by [@GromNaN](https://github.com/GromNaN).
1212
- Throw an exception when `Query\Builder::push()` is used incorrectly [#5](https://github.com/GromNaN/laravel-mongodb-private/pull/5) by [@GromNaN](https://github.com/GromNaN).
1313
- Remove public property `Query\Builder::$paginating` [#15](https://github.com/GromNaN/laravel-mongodb-private/pull/15) by [@GromNaN](https://github.com/GromNaN).
14-
- Update `like` operators to be case-sensitive and improve support for `ilike`, `regex`, `regexp` and their `not` variants [#17](https://github.com/GromNaN/laravel-mongodb-private/pull/17) by [@GromNaN](https://github.com/GromNaN).
14+
- Remove `ilike` and `regexp` operators, use `like` and `regex` instead [#17](https://github.com/GromNaN/laravel-mongodb-private/pull/17) by [@GromNaN](https://github.com/GromNaN).
1515

1616
## [3.9.2] - 2022-09-01
1717

src/Query/Builder.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,12 @@ class Builder extends BaseBuilder
7575
'like',
7676
'not like',
7777
'between',
78-
'ilike',
79-
'not ilike',
8078
'&',
8179
'|',
8280
'^',
8381
'<<',
8482
'>>',
8583
'rlike',
86-
'regexp',
87-
'not regexp',
8884
'exists',
8985
'type',
9086
'mod',
@@ -124,6 +120,13 @@ class Builder extends BaseBuilder
124120
'>=' => '$gte',
125121
];
126122

123+
private $removedOperators = [
124+
'regexp' => 'regex',
125+
'not regexp' => 'not regex',
126+
'ilike' => 'like',
127+
'not ilike' => 'not like',
128+
];
129+
127130
/**
128131
* @inheritdoc
129132
*/
@@ -916,13 +919,17 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
916919
{
917920
$params = func_get_args();
918921

919-
// Remove the leading $ from operators.
920922
if (func_num_args() == 3) {
921923
$operator = &$params[1];
922924

925+
// Remove the leading $ from operators.
923926
if (Str::startsWith($operator, '$')) {
924927
$operator = substr($operator, 1);
925928
}
929+
930+
if (isset($this->removedOperators[$operator])) {
931+
throw new \InvalidArgumentException(sprintf('Operator "%s" is not supported. Use "%s" instead.', $operator, $this->removedOperators[$operator]));
932+
}
926933
}
927934

928935
return parent::where(...$params);
@@ -948,8 +955,6 @@ protected function compileWheres(): array
948955

949956
// Operator conversions
950957
$convert = [
951-
'regexp' => 'regex',
952-
'not regexp' => 'not regex',
953958
'elemmatch' => 'elemMatch',
954959
'geointersects' => 'geoIntersects',
955960
'geowithin' => 'geoWithin',
@@ -1061,8 +1066,7 @@ protected function compileWhereBasic(array $where): array
10611066
extract($where);
10621067

10631068
// Replace like with a Regex instance.
1064-
if (in_array($operator, ['like', 'not like', 'ilike', 'not ilike'])) {
1065-
$flags = str_ends_with($operator, 'ilike') ? 'i' : '';
1069+
if (in_array($operator, ['like', 'not like'])) {
10661070
if (str_starts_with($operator, 'not')) {
10671071
$operator = 'not';
10681072
} else {
@@ -1080,7 +1084,7 @@ protected function compileWhereBasic(array $where): array
10801084
$regex .= '$';
10811085
}
10821086

1083-
$value = new Regex($regex, $flags);
1087+
$value = new Regex($regex, 'i');
10841088
} // Manipulate regex operations.
10851089
elseif (in_array($operator, ['regex', 'not regex'])) {
10861090
// Automatically convert regular expression strings to Regex objects.

tests/Query/BuilderTest.php

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -441,35 +441,20 @@ function (Builder $builder) {
441441
];
442442

443443
yield 'where like' => [
444-
['find' => [['name' => new Regex('^acme$', '')], []]],
444+
['find' => [['name' => new Regex('^acme$', 'i')], []]],
445445
fn (Builder $builder) => $builder->where('name', 'like', 'acme'),
446446
];
447447

448448
yield 'where like escape' => [
449-
['find' => [['name' => new Regex('^\^acme\$$', '')], []]],
449+
['find' => [['name' => new Regex('^\^acme\$$', 'i')], []]],
450450
fn (Builder $builder) => $builder->where('name', 'like', '^acme$'),
451451
];
452452

453453
yield 'where like %' => [
454-
['find' => [['name' => new Regex('.*acme.*', '')], []]],
454+
['find' => [['name' => new Regex('.*acme.*', 'i')], []]],
455455
fn (Builder $builder) => $builder->where('name', 'like', '%acme%'),
456456
];
457457

458-
yield 'where ilike' => [
459-
['find' => [['name' => new Regex('^acme$', 'i')], []]],
460-
fn (Builder $builder) => $builder->where('name', 'ilike', 'acme'),
461-
];
462-
463-
yield 'where not like' => [
464-
['find' => [['name' => ['$not' => new Regex('^acme$', '')]], []]],
465-
fn (Builder $builder) => $builder->where('name', 'not like', 'acme'),
466-
];
467-
468-
yield 'where not ilike' => [
469-
['find' => [['name' => ['$not' => new Regex('^acme$', 'i')]], []]],
470-
fn (Builder $builder) => $builder->where('name', 'not ilike', 'acme'),
471-
];
472-
473458
$regex = new Regex('^acme$', 'si');
474459
yield 'where BSON\Regex' => [
475460
['find' => [['name' => $regex], []]],
@@ -486,16 +471,6 @@ function (Builder $builder) {
486471
fn (Builder $builder) => $builder->where('name', 'not regex', '/^acme$/si'),
487472
];
488473

489-
yield 'where regexp' => [
490-
['find' => [['name' => $regex], []]],
491-
fn (Builder $builder) => $builder->where('name', 'regexp', '/^acme$/si'),
492-
];
493-
494-
yield 'where not regexp' => [
495-
['find' => [['name' => ['$not' => $regex]], []]],
496-
fn (Builder $builder) => $builder->where('name', 'not regexp', '/^acme$/si'),
497-
];
498-
499474
yield 'distinct' => [
500475
['distinct' => ['foo', [], []]],
501476
fn (Builder $builder) => $builder->distinct('foo'),
@@ -572,6 +547,18 @@ public static function provideExceptions(): iterable
572547
'Regular expressions must be surrounded by slashes "/". Got "/^acme$"',
573548
fn (Builder $builder) => $builder->where('name', 'regex', '/^acme$'),
574549
];
550+
551+
yield 'where regexp not supported' => [
552+
\InvalidArgumentException::class,
553+
'Operator "regexp" is not supported. Use "regex" instead.',
554+
fn (Builder $builder) => $builder->where('name', 'regexp', '/^acme$/'),
555+
];
556+
557+
yield 'where ilike not supported' => [
558+
\InvalidArgumentException::class,
559+
'Operator "ilike" is not supported. Use "like" instead.',
560+
fn (Builder $builder) => $builder->where('name', 'ilike', 'acme'),
561+
];
575562
}
576563

577564
/** @dataProvider getEloquentMethodsNotSupported */

tests/QueryTest.php

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -72,61 +72,31 @@ public function testAndWhere(): void
7272

7373
public function testLike(): void
7474
{
75-
$users = User::where('name', 'like', '%Doe')->get();
75+
$users = User::where('name', 'like', '%doe')->get();
7676
$this->assertCount(2, $users);
7777

7878
$users = User::where('name', 'like', '%y%')->get();
79-
$this->assertCount(2, $users);
79+
$this->assertCount(3, $users);
8080

8181
$users = User::where('name', 'LIKE', '%y%')->get();
82-
$this->assertCount(2, $users);
82+
$this->assertCount(3, $users);
8383

84-
$users = User::where('name', 'like', 'T%')->get();
84+
$users = User::where('name', 'like', 't%')->get();
8585
$this->assertCount(1, $users);
8686
}
8787

8888
public function testNotLike(): void
8989
{
9090
$users = User::where('name', 'not like', '%doe')->get();
91-
$this->assertCount(9, $users);
92-
93-
$users = User::where('name', 'not like', '%y%')->get();
94-
$this->assertCount(7, $users);
95-
96-
$users = User::where('name', 'not LIKE', '%y%')->get();
97-
$this->assertCount(7, $users);
98-
99-
$users = User::where('name', 'not like', 't%')->get();
100-
$this->assertCount(9, $users);
101-
}
102-
103-
public function testIlike(): void
104-
{
105-
$users = User::where('name', 'ilike', '%doe')->get();
106-
$this->assertCount(2, $users);
107-
108-
$users = User::where('name', 'ilike', '%y%')->get();
109-
$this->assertCount(3, $users);
110-
111-
$users = User::where('name', 'ILIKE', '%y%')->get();
112-
$this->assertCount(3, $users);
113-
114-
$users = User::where('name', 'ilike', 't%')->get();
115-
$this->assertCount(1, $users);
116-
}
117-
118-
public function testNotIlike(): void
119-
{
120-
$users = User::where('name', 'not ilike', '%doe')->get();
12191
$this->assertCount(7, $users);
12292

123-
$users = User::where('name', 'not ilike', '%y%')->get();
93+
$users = User::where('name', 'not like', '%y%')->get();
12494
$this->assertCount(6, $users);
12595

126-
$users = User::where('name', 'not ILIKE', '%y%')->get();
96+
$users = User::where('name', 'not LIKE', '%y%')->get();
12797
$this->assertCount(6, $users);
12898

129-
$users = User::where('name', 'not ilike', 't%')->get();
99+
$users = User::where('name', 'not like', 't%')->get();
130100
$this->assertCount(8, $users);
131101
}
132102

@@ -344,7 +314,7 @@ public function testSubQuery(): void
344314

345315
$users = User::where('title', 'user')->where(function ($query) {
346316
$query->where('age', 35)
347-
->orWhere('name', 'like', '%Harry%');
317+
->orWhere('name', 'like', '%harry%');
348318
})
349319
->get();
350320

0 commit comments

Comments
 (0)