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

Commit 8e1b602

Browse files
committed
Optimize DatabasePresenceVerifier
1 parent 22a8bce commit 8e1b602

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

src/Query/Builder.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,8 +1075,6 @@ protected function compileWhereBasic(array $where): array
10751075
$flags = end($e);
10761076
// Extract the regex string between the delimiters
10771077
$regstr = substr($value, 1, -1 - strlen($flags));
1078-
// Unescape forward slashes
1079-
$regstr = stripslashes($regstr);
10801078
$value = new Regex($regstr, $flags);
10811079
}
10821080

src/Validation/DatabasePresenceVerifier.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Jenssegers\Mongodb\Validation;
44

5+
use MongoDB\BSON\Regex;
6+
57
class DatabasePresenceVerifier extends \Illuminate\Validation\DatabasePresenceVerifier
68
{
79
/**
@@ -17,7 +19,7 @@ class DatabasePresenceVerifier extends \Illuminate\Validation\DatabasePresenceVe
1719
*/
1820
public function getCount($collection, $column, $value, $excludeId = null, $idColumn = null, array $extra = [])
1921
{
20-
$query = $this->table($collection)->where($column, 'regex', '/'.preg_quote($value).'/i');
22+
$query = $this->table($collection)->where($column, new Regex('^'.preg_quote($value).'$', '/i'));
2123

2224
if ($excludeId !== null && $excludeId != 'NULL') {
2325
$query->where($idColumn ?: 'id', '<>', $excludeId);

tests/Query/BuilderTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -635,9 +635,9 @@ function (Builder $builder) {
635635
fn (Builder $builder) => $builder->where('name', 'regex', '#^acme$#si'),
636636
];
637637

638-
yield 'where regex with escaped forward slash' => [
639-
['find' => [['name' => new Regex('ac/me', '')], []]],
640-
fn (Builder $builder) => $builder->where('name', 'regex', '/ac\/me/'),
638+
yield 'where regex with escaped characters' => [
639+
['find' => [['name' => new Regex('a\.c\/m\+e', '')], []]],
640+
fn (Builder $builder) => $builder->where('name', 'regex', '/a\.c\/m\+e/'),
641641
];
642642

643643
yield 'where not regex' => [

tests/ValidationTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public function testUnique(): void
4848
);
4949
$this->assertFalse($validator->fails());
5050

51+
$validator = Validator::make(
52+
['name' => 'John'],
53+
['name' => 'required|unique:users']
54+
);
55+
$this->assertFalse($validator->fails());
56+
5157
User::create(['name' => 'Johnny Cash', 'email' => 'johnny.cash+200@gmail.com']);
5258

5359
$validator = Validator::make(

0 commit comments

Comments
 (0)