From 003334444ec2cc862911b3c662660756e93f87d2 Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Thu, 4 Oct 2018 10:26:48 -0500 Subject: [PATCH] ContainsAllStartingWith Query --- src/Parse/ParseQuery.php | 33 ++++++++++++++++++++++++++++++++- tests/Parse/ParseQueryTest.php | 21 +++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/Parse/ParseQuery.php b/src/Parse/ParseQuery.php index 29f1e2c1..b0b35e09 100755 --- a/src/Parse/ParseQuery.php +++ b/src/Parse/ParseQuery.php @@ -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. @@ -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; } @@ -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. * diff --git a/tests/Parse/ParseQueryTest.php b/tests/Parse/ParseQueryTest.php index 3c3a59b0..40f8bbba 100644 --- a/tests/Parse/ParseQueryTest.php +++ b/tests/Parse/ParseQueryTest.php @@ -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 = [];