Skip to content

Commit 942dcc2

Browse files
authored
Support query with count (#448)
* Support query with count * lint!!!
1 parent c6a158f commit 942dcc2

File tree

2 files changed

+165
-2
lines changed

2 files changed

+165
-2
lines changed

src/Parse/ParseQuery.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ public function _getOptions()
541541
* Execute a query to get only the first result.
542542
*
543543
* @param bool $useMasterKey If the query should use the master key
544-
* @param bool $decodeObjects If set to false, will not return raw data instead of ParseObject instances
544+
* @param bool $decodeObjects If set to false, will return raw data instead of ParseObject instances
545545
*
546546
* @return array|ParseObject Returns the first object or an empty array
547547
*/
@@ -600,6 +600,23 @@ public function count($useMasterKey = false)
600600
return $result['count'];
601601
}
602602

603+
/**
604+
* The response will include the total number of objects satisfying this query,
605+
* dispite limit / skip. Might be useful for pagination.
606+
*
607+
* Note: the results will be an object
608+
* `results`: holding {ParseObject} array and `count`: integer holding total number
609+
*
610+
* @param bool $includeCount If response should include count, true by default.
611+
*
612+
* @return ParseQuery Returns this query, so you can chain this call.
613+
*/
614+
public function withCount($includeCount = true)
615+
{
616+
$this->count = (int)$includeCount;
617+
return $this;
618+
}
619+
603620
/**
604621
* Execute a distinct query and return unique values.
605622
*
@@ -663,7 +680,7 @@ public function aggregate($pipeline)
663680
* Execute a find query and return the results.
664681
*
665682
* @param bool $useMasterKey
666-
* @param bool $decodeObjects If set to false, will not return raw data instead of ParseObject instances
683+
* @param bool $decodeObjects If set to false, will return raw data instead of ParseObject instances
667684
*
668685
* @return ParseObject[]
669686
*/
@@ -681,6 +698,27 @@ public function find($useMasterKey = false, $decodeObjects = true)
681698
null,
682699
$useMasterKey
683700
);
701+
702+
$response = [];
703+
if (isset($result['count'])) {
704+
$response['count'] = $result['count'];
705+
$response['results'] = $this->handleQueryResult($result, $decodeObjects);
706+
return $response;
707+
}
708+
709+
return $this->handleQueryResult($result, $decodeObjects);
710+
}
711+
712+
/**
713+
* Handles result from ParseClient::_request
714+
*
715+
* @param array $result Array of ParseObject raw data.
716+
* @param bool $decodeObjects If set to false, will return raw data instead of ParseObject instances
717+
*
718+
* @return Array Array of ParseObjects or raw data.
719+
*/
720+
public function handleQueryResult($result, $decodeObjects)
721+
{
684722
if (!isset($result['results'])) {
685723
return [];
686724
}

tests/Parse/ParseQueryTest.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,131 @@ function ($i) {
813813
);
814814
}
815815

816+
/**
817+
* @group withCount
818+
*/
819+
public function testWithCount()
820+
{
821+
Helper::clearClass('BoxedNumber');
822+
$this->saveObjects(
823+
3,
824+
function ($i) {
825+
$boxedNumber = ParseObject::create('BoxedNumber');
826+
$boxedNumber->set('x', $i + 1);
827+
828+
return $boxedNumber;
829+
}
830+
);
831+
$query = new ParseQuery('BoxedNumber');
832+
$query->withCount();
833+
$response = $query->find();
834+
$this->assertEquals($response['count'], 3);
835+
$this->assertEquals(count($response['results']), 3);
836+
}
837+
838+
/**
839+
* @group withCount
840+
*/
841+
public function testWithCountDestructure()
842+
{
843+
Helper::clearClass('BoxedNumber');
844+
$this->saveObjects(
845+
3,
846+
function ($i) {
847+
$boxedNumber = ParseObject::create('BoxedNumber');
848+
$boxedNumber->set('x', $i + 1);
849+
850+
return $boxedNumber;
851+
}
852+
);
853+
$query = new ParseQuery('BoxedNumber');
854+
$query->withCount();
855+
['count' => $count, 'results' => $results] = $query->find();
856+
$this->assertEquals($count, 3);
857+
$this->assertEquals(count($results), 3);
858+
}
859+
860+
/**
861+
* @group withCount
862+
*/
863+
public function testWithCountFalse()
864+
{
865+
Helper::clearClass('BoxedNumber');
866+
$this->saveObjects(
867+
3,
868+
function ($i) {
869+
$boxedNumber = ParseObject::create('BoxedNumber');
870+
$boxedNumber->set('x', $i + 1);
871+
872+
return $boxedNumber;
873+
}
874+
);
875+
$query = new ParseQuery('BoxedNumber');
876+
$query->withCount(false);
877+
$response = $query->find();
878+
$this->assertEquals(isset($response['count']), false);
879+
$this->assertEquals(count($response), 3);
880+
}
881+
882+
/**
883+
* @group withCount
884+
*/
885+
public function testWithCountEmptyClass()
886+
{
887+
Helper::clearClass('BoxedNumber');
888+
$query = new ParseQuery('BoxedNumber');
889+
$query->withCount();
890+
$response = $query->find();
891+
$this->assertEquals($response['count'], 0);
892+
$this->assertEquals(count($response['results']), 0);
893+
}
894+
895+
/**
896+
* @group withCount
897+
*/
898+
public function testWithCountAndLimit()
899+
{
900+
Helper::clearClass('BoxedNumber');
901+
$this->saveObjects(
902+
4,
903+
function ($i) {
904+
$boxedNumber = ParseObject::create('BoxedNumber');
905+
$boxedNumber->set('x', $i + 1);
906+
907+
return $boxedNumber;
908+
}
909+
);
910+
$query = new ParseQuery('BoxedNumber');
911+
$query->withCount();
912+
$query->limit(2);
913+
$response = $query->find();
914+
$this->assertEquals($response['count'], 4);
915+
$this->assertEquals(count($response['results']), 2);
916+
}
917+
918+
/**
919+
* @group withCount
920+
*/
921+
public function testWithCountAndSkip()
922+
{
923+
Helper::clearClass('BoxedNumber');
924+
$this->saveObjects(
925+
4,
926+
function ($i) {
927+
$boxedNumber = ParseObject::create('BoxedNumber');
928+
$boxedNumber->set('x', $i + 1);
929+
930+
return $boxedNumber;
931+
}
932+
);
933+
$query = new ParseQuery('BoxedNumber');
934+
$query->withCount();
935+
$query->skip(3);
936+
$response = $query->find();
937+
$this->assertEquals($response['count'], 4);
938+
$this->assertEquals(count($response['results']), 1);
939+
}
940+
816941
public function testCountError()
817942
{
818943
$query = new ParseQuery('Test');

0 commit comments

Comments
 (0)