Skip to content

ContainsAllStartingWith Query #420

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

Merged
merged 1 commit into from
Nov 13, 2018
Merged
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
33 changes: 32 additions & 1 deletion src/Parse/ParseQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,18 @@ private function quote($s)
return '\\Q'.str_replace('\\E', '\\E\\\\E\\Q', $s).'\\E';
}

/**
* Converts a string into a regex that matches it at the beginning
*
* @param mixed $s The string or array being replaced.
*
* @return string Returns the string converted.
*/
private function regexStartWith($s)
{
return '^' . $this->quote($s);
}

/**
* Add a constraint to the query that requires a particular key's value to
* start with the provided value.
Expand All @@ -367,7 +379,7 @@ private function quote($s)
*/
public function startsWith($key, $value)
{
$this->addCondition($key, '$regex', '^'.$this->quote($value));
$this->addCondition($key, '$regex', $this->regexStartWith($value));

return $this;
}
Expand Down Expand Up @@ -1197,6 +1209,25 @@ public function containsAll($key, $values)
return $this;
}

/**
* Add a constraint to the query that requires a particular key's value to
* contain each one of the provided list of values starting with the given string.
*
* @param string $key The key to check. This key's value must be an array.
* @param array $values The values that will match as starting string.
*
* @return ParseQuery Returns the query, so you can chain this call.
*/
public function containsAllStartingWith($key, $values)
{
$opts = [];
for ($i = 0; $i < count($values); $i += 1) {
$opts[] = ['$regex' => $this->regexStartWith($values[$i])];
}

return $this->containsAll($key, $opts);
}

/**
* Add a constraint for finding objects that contain the given key.
*
Expand Down
21 changes: 21 additions & 0 deletions tests/Parse/ParseQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1379,6 +1379,27 @@ function ($i) use (&$messageList) {
);
}

public function testContainsAllStartingWithQueries()
{
$obj1 = ParseObject::create('TestObject');
$obj2 = ParseObject::create('TestObject');
$obj3 = ParseObject::create('TestObject');
$obj1->setArray('strings', ['the', 'brown', 'lazy', 'fox', 'jumps']);
$obj2->setArray('strings', ['the', 'brown', 'fox', 'jumps']);
$obj3->setArray('strings', ['over', 'the', 'lazy', 'dogs']);

ParseObject::saveAll([$obj1, $obj2, $obj3]);

$query = new ParseQuery('TestObject');
$query->containsAllStartingWith('strings', ['the', 'fox', 'lazy']);
$results = $query->find();
$this->assertEquals(
1,
count($results),
'Did not return correct number of objects.'
);
}

public function testContainedInObjectArrayQueries()
{
$messageList = [];
Expand Down