Skip to content

[12.x] Add whereLike and whereNotLike methods to Collection #56112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/Illuminate/Collections/Traits/EnumeratesValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,44 @@ public function whereNotInStrict($key, $values)
return $this->whereNotIn($key, $values, true);
}

/**
* Filter items such that the given string matches the value of the given key using SQL LIKE pattern matching.
*
* @param string $key
* @param string $pattern
* @param bool $caseSensitive
* @return static
*/
public function whereLike($key, $pattern, $caseSensitive = false)
{
$regex = $this->parseSqlLikeRegex($pattern, $caseSensitive);

return $this->filter(function ($item) use ($key, $regex) {
$value = data_get($item, $key);

return is_string($value) && preg_match($regex, $value);
});
}

/**
* Filter items such that the given string does not match the value of the given key using SQL LIKE pattern matching.
*
* @param string $key
* @param string $pattern
* @param bool $caseSensitive
* @return static
*/
public function whereNotLike($key, $pattern, $caseSensitive = false)
{
$regex = $this->parseSqlLikeRegex($pattern, $caseSensitive);

return $this->reject(function ($item) use ($key, $regex) {
$value = data_get($item, $key);

return is_string($value) && preg_match($regex, $value);
});
}

/**
* Filter the items, removing any items that don't match the given type(s).
*
Expand Down Expand Up @@ -1177,4 +1215,30 @@ protected function identity()
{
return fn ($value) => $value;
}

/**
* Convert the pattern containing SQL LIKE wildcards to valid regular expressions, while respecting escaped wildcards.
*
* @param string $pattern
* @param bool $caseSensitive
* @return string
*/
protected function parseSqlLikeRegex($pattern, $caseSensitive)
{
$pattern = preg_quote($pattern, '/');

$pattern = preg_replace_callback('/\\\\[%_]|(?<!\\\\)[%_]/', function ($matches) {
return match ($matches[0]) {
"\%" => '%',
"\_" => '_',
'%' => '.*',
'_' => '.',
default => $matches[0]
};
}, $pattern);

$regex = '/^'.$pattern.'$/'.($caseSensitive ? '' : 'i');

return $regex;
}
}
44 changes: 44 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,50 @@ public function testWhereNotInStrict($collection)
$this->assertEquals([['v' => 2], ['v' => '3'], ['v' => 4]], $c->whereNotInStrict('v', [1, 3])->values()->all());
}

#[DataProvider('collectionClassProvider')]
public function testWhereLike($collection)
{
$c = new $collection([['email' => 'test@example.com'], ['email' => 'test@example.org'], ['email' => 'test@example.net']]);
$this->assertEquals([['email' => 'test@example.com']], $c->whereLike('email', '%@example.com%')->values()->all());
$this->assertEquals([['email' => 'test@example.org']], $c->whereLike('email', '%@ex__ple.o_g%')->values()->all());
$this->assertEquals([['email' => 'test@example.net']], $c->whereLike('email', 'test@example.net')->values()->all());

$c = new $collection([['name' => 'Taylor Otwell'], ['name' => 'taylor otWeLl']]);
$this->assertEquals([['name' => 'Taylor Otwell'], ['name' => 'taylor otWeLl']], $c->whereLike('name', '%otwell%', false)->values()->all());
$this->assertEquals([['name' => 'taylor otWeLl']], $c->whereLike('name', '%taylor%', true)->values()->all());

$c = new $collection([['name' => 'Taylor Otwell', 'contact' => ['email' => 'test@example.com']], ['name' => 'Abigail Otwell', 'contact' => ['email' => 'test_1@example.org']], ['name' => 'Joe Dixon', 'contact' => ['email' => 'test@example.net']]]);
$this->assertEquals([['name' => 'Taylor Otwell', 'contact' => ['email' => 'test@example.com']]], $c->whereLike('contact.email', 'test@%.co_')->values()->all());
$this->assertEquals([['name' => 'Abigail Otwell', 'contact' => ['email' => 'test_1@example.org']]], $c->whereLike('contact.email', 'test\_1@ex_mple.%')->values()->all());

$c = new $collection([['percent' => '99%'], ['percent' => '70.70%'], ['percent' => '59%']]);
$this->assertEquals([['percent' => '99%']], $c->whereLike('percent', '99\%')->values()->all());
$this->assertEquals([['percent' => '99%'], ['percent' => '59%']], $c->whereLike('percent', '_9\%')->values()->all());
$this->assertEquals([['percent' => '70.70%']], $c->whereLike('percent', '70.__\%')->values()->all());
}

#[DataProvider('collectionClassProvider')]
public function testWhereNotLike($collection)
{
$c = new $collection([['email' => 'test@example.com'], ['email' => 'test@example.org'], ['email' => 'test@example.net']]);
$this->assertEquals([['email' => 'test@example.org'], ['email' => 'test@example.net']], $c->whereNotLike('email', '%@example.com%')->values()->all());
$this->assertEquals([['email' => 'test@example.com'], ['email' => 'test@example.net']], $c->whereNotLike('email', '%@ex__ple.o_g%')->values()->all());
$this->assertEquals([['email' => 'test@example.com'], ['email' => 'test@example.org']], $c->whereNotLike('email', 'test@example.net')->values()->all());

$c = new $collection([['name' => 'Taylor Otwell'], ['name' => 'taylor otWeLl']]);
$this->assertEquals([], $c->whereNotLike('name', '%otwell%', false)->values()->all());
$this->assertEquals([['name' => 'Taylor Otwell']], $c->whereNotLike('name', '%taylor%', true)->values()->all());

$c = new $collection([['name' => 'Taylor Otwell', 'contact' => ['email' => 'test@example.com']], ['name' => 'Abigail Otwell', 'contact' => ['email' => 'test_1@example.org']], ['name' => 'Joe Dixon', 'contact' => ['email' => 'test@example.net']]]);
$this->assertEquals([['name' => 'Abigail Otwell', 'contact' => ['email' => 'test_1@example.org']], ['name' => 'Joe Dixon', 'contact' => ['email' => 'test@example.net']]], $c->whereNotLike('contact.email', 'test@%.co_')->values()->all());
$this->assertEquals([['name' => 'Taylor Otwell', 'contact' => ['email' => 'test@example.com']], ['name' => 'Joe Dixon', 'contact' => ['email' => 'test@example.net']]], $c->whereNotLike('contact.email', 'test\_1@ex_mple.%')->values()->all());

$c = new $collection([['percent' => '99%'], ['percent' => '70.70%'], ['percent' => '59%']]);
$this->assertEquals([['percent' => '70.70%'], ['percent' => '59%']], $c->whereNotLike('percent', '99\%')->values()->all());
$this->assertEquals([['percent' => '70.70%']], $c->whereNotLike('percent', '_9\%')->values()->all());
$this->assertEquals([['percent' => '99%'], ['percent' => '59%']], $c->whereNotLike('percent', '70.__\%')->values()->all());
}

#[DataProvider('collectionClassProvider')]
public function testValues($collection)
{
Expand Down
26 changes: 26 additions & 0 deletions tests/Support/SupportLazyCollectionIsLazyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,32 @@ public function testWhereNotInStrictIsLazy()
});
}

public function testWhereLikeIsLazy()
{
$data = $this->make([['name' => 'Taylor Otwell'], ['name' => 'Abigail Otwell'], ['name' => 'Joe Dixon'], ['name' => 'John Doe']]);

$this->assertDoesNotEnumerateCollection($data, function ($collection) {
$collection->whereLike('name', '%Otw_ll%');
});

$this->assertEnumeratesCollection($data, 3, function ($collection) {
$collection->whereLike('name', 'Jo%')->take(1)->all();
});
}

public function testWhereNotLikeIsLazy()
{
$data = $this->make([['name' => 'Taylor Otwell'], ['name' => 'Abigail Otwell'], ['name' => 'Joe Dixon'], ['name' => 'John Doe']]);

$this->assertDoesNotEnumerateCollection($data, function ($collection) {
$collection->whereNotLike('name', '%Otw_ll%');
});

$this->assertEnumeratesCollection($data, 2, function ($collection) {
$collection->whereNotLike('name', '%Taylor%')->take(1)->all();
});
}

public function testWhereNotNullIsLazy()
{
$data = $this->make([['a' => 1], ['a' => null], ['a' => 2], ['a' => 3]]);
Expand Down
6 changes: 6 additions & 0 deletions types/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,12 @@ function ($collection, $count) {
assertType("Illuminate\Support\Collection<int, array{string: int}>", $collection::make([['string' => 2]])
->whereNotInStrict('string', [2]));

assertType("Illuminate\Support\Collection<int, array{string: string}>", $collection::make([['string' => 'string']])
->whereLike('string', '%str%'));

assertType("Illuminate\Support\Collection<int, array{string: string}>", $collection::make([['string' => 'string']])
->whereNotLike('string', '%int%'));

assertType('Illuminate\Support\Collection<int, User>', $collection::make([new User, 1])
->whereInstanceOf(User::class));

Expand Down
6 changes: 6 additions & 0 deletions types/Support/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,12 @@ public function toArray(): array
assertType("Illuminate\Support\LazyCollection<int, array{string: int}>", $collection::make([['string' => 2]])
->whereNotInStrict('string', [2]));

assertType("Illuminate\Support\LazyCollection<int, array{string: string}>", $collection::make([['string' => 'string']])
->whereLike('string', '%str%'));

assertType("Illuminate\Support\LazyCollection<int, array{string: string}>", $collection::make([['string' => 'string']])
->whereNotLike('string', '%int%'));

assertType('Illuminate\Support\LazyCollection<int, User>', $collection::make([new User, 1])
->whereInstanceOf(User::class));

Expand Down