Skip to content

Commit 1c73a1c

Browse files
dplewisflovilmart
authored andcommitted
ContainsAllStartingWith Query (#420)
1 parent fc79703 commit 1c73a1c

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/Parse/ParseQuery.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,18 @@ private function quote($s)
356356
return '\\Q'.str_replace('\\E', '\\E\\\\E\\Q', $s).'\\E';
357357
}
358358

359+
/**
360+
* Converts a string into a regex that matches it at the beginning
361+
*
362+
* @param mixed $s The string or array being replaced.
363+
*
364+
* @return string Returns the string converted.
365+
*/
366+
private function regexStartWith($s)
367+
{
368+
return '^' . $this->quote($s);
369+
}
370+
359371
/**
360372
* Add a constraint to the query that requires a particular key's value to
361373
* start with the provided value.
@@ -367,7 +379,7 @@ private function quote($s)
367379
*/
368380
public function startsWith($key, $value)
369381
{
370-
$this->addCondition($key, '$regex', '^'.$this->quote($value));
382+
$this->addCondition($key, '$regex', $this->regexStartWith($value));
371383

372384
return $this;
373385
}
@@ -1197,6 +1209,25 @@ public function containsAll($key, $values)
11971209
return $this;
11981210
}
11991211

1212+
/**
1213+
* Add a constraint to the query that requires a particular key's value to
1214+
* contain each one of the provided list of values starting with the given string.
1215+
*
1216+
* @param string $key The key to check. This key's value must be an array.
1217+
* @param array $values The values that will match as starting string.
1218+
*
1219+
* @return ParseQuery Returns the query, so you can chain this call.
1220+
*/
1221+
public function containsAllStartingWith($key, $values)
1222+
{
1223+
$opts = [];
1224+
for ($i = 0; $i < count($values); $i += 1) {
1225+
$opts[] = ['$regex' => $this->regexStartWith($values[$i])];
1226+
}
1227+
1228+
return $this->containsAll($key, $opts);
1229+
}
1230+
12001231
/**
12011232
* Add a constraint for finding objects that contain the given key.
12021233
*

tests/Parse/ParseQueryTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,6 +1379,27 @@ function ($i) use (&$messageList) {
13791379
);
13801380
}
13811381

1382+
public function testContainsAllStartingWithQueries()
1383+
{
1384+
$obj1 = ParseObject::create('TestObject');
1385+
$obj2 = ParseObject::create('TestObject');
1386+
$obj3 = ParseObject::create('TestObject');
1387+
$obj1->setArray('strings', ['the', 'brown', 'lazy', 'fox', 'jumps']);
1388+
$obj2->setArray('strings', ['the', 'brown', 'fox', 'jumps']);
1389+
$obj3->setArray('strings', ['over', 'the', 'lazy', 'dogs']);
1390+
1391+
ParseObject::saveAll([$obj1, $obj2, $obj3]);
1392+
1393+
$query = new ParseQuery('TestObject');
1394+
$query->containsAllStartingWith('strings', ['the', 'fox', 'lazy']);
1395+
$results = $query->find();
1396+
$this->assertEquals(
1397+
1,
1398+
count($results),
1399+
'Did not return correct number of objects.'
1400+
);
1401+
}
1402+
13821403
public function testContainedInObjectArrayQueries()
13831404
{
13841405
$messageList = [];

0 commit comments

Comments
 (0)