From 32ce347d0997cd845aec1184910a3776a35e6728 Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Wed, 3 Oct 2018 19:51:14 -0500 Subject: [PATCH] ContainedBy Query --- src/Parse/ParseQuery.php | 18 +++++++++++++++++- tests/Parse/ParseQueryTest.php | 25 ++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/Parse/ParseQuery.php b/src/Parse/ParseQuery.php index c024aaf6..9a317092 100755 --- a/src/Parse/ParseQuery.php +++ b/src/Parse/ParseQuery.php @@ -388,7 +388,7 @@ public function endsWith($key, $value) return $this; } - /** + /** * Adds a constraint for finding string values that contain a provided * string. This may be slow for large datasets. * @@ -404,6 +404,22 @@ public function contains($key, $value) return $this; } + /** + * Adds a constraint to the query that requires a particular key's value to + * be contained by the provided list of values. Get objects where all array elements match. + * + * @param string $key The key to check. + * @param mixed $value The values that will match. + * + * @return ParseQuery Returns this query, so you can chain this call. + */ + public function containedBy($key, $value) + { + $this->addCondition($key, '$containedBy', $value); + + return $this; + } + /** * Adds a constraint for finding string values that contain a provided * string using Full Text Search diff --git a/tests/Parse/ParseQueryTest.php b/tests/Parse/ParseQueryTest.php index d773bb9c..8e78190d 100644 --- a/tests/Parse/ParseQueryTest.php +++ b/tests/Parse/ParseQueryTest.php @@ -1275,6 +1275,29 @@ public function testContainsAllDateArrayQueries() ); } + public function testContainedByQuery() + { + Helper::clearClass('NumberSet'); + $obj1 = ParseObject::create('TestObject'); + $obj2 = ParseObject::create('TestObject'); + $obj3 = ParseObject::create('TestObject'); + $obj1->setArray('numbers', [0, 1, 2]); + $obj2->setArray('numbers', [2, 0]); + $obj3->setArray('numbers', [1, 2, 3, 4]); + $numberSet = [$obj1, $obj2, $obj3]; + + ParseObject::saveAll($numberSet); + + $query = new ParseQuery('TestObject'); + $query->containedBy('numbers', [1, 2, 3, 4, 5]); + $results = $query->find(); + $this->assertEquals( + 1, + count($results), + 'Did not return correct number of objects.' + ); + } + public function testContainsAllObjectArrayQueries() { Helper::clearClass('MessageSet'); @@ -2338,7 +2361,7 @@ public function testUnknownCondition() '\Parse\ParseException', 'Unknown condition to set' ); - + $query = new ParseQuery('TestObject'); $query->_setConditions([ 'unrecognized' => 1