Skip to content

Commit

Permalink
getResults() fix when a black array is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
mauretto78 committed Oct 22, 2019
1 parent 941b979 commit 25fd806
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 26 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions .idea/array-query.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 0 additions & 15 deletions src/Exceptions/EmptyArrayException.php

This file was deleted.

19 changes: 13 additions & 6 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@

namespace ArrayQuery;

use ArrayQuery\Exceptions\EmptyArrayException;
use ArrayQuery\Exceptions\InvalidArrayException;
use ArrayQuery\Exceptions\NotConsistentDataException;
use ArrayQuery\Exceptions\NotExistingElementException;
use ArrayQuery\Exceptions\NotValidCriterionOperatorException;
use ArrayQuery\Exceptions\NotValidKeyElementInArrayException;
use ArrayQuery\Exceptions\NotValidLimitsOfArrayException;
use ArrayQuery\Exceptions\NotValidSortingOperatorException;
use ArrayQuery\Filters\CriterionFilter;
use ArrayQuery\Filters\JoinFilter;
use ArrayQuery\Filters\SortingFilter;
use ArrayQuery\Filters\LimitFilter;
use ArrayQuery\Filters\SortingFilter;
use ArrayQuery\Helpers\ArrayHelper;
use ArrayQuery\Helpers\ConsistencyChecker;

Expand Down Expand Up @@ -54,7 +51,10 @@ class QueryBuilder

/**
* QueryBuilder constructor.
*
* @param array $array
*
* @throws NotConsistentDataException
*/
public function __construct(array $array)
{
Expand All @@ -63,7 +63,9 @@ public function __construct(array $array)

/**
* @param array $array
*
* @return static
* @throws NotConsistentDataException
*/
public static function create(array $array)
{
Expand All @@ -72,7 +74,8 @@ public static function create(array $array)

/**
* @param array $array
* @throws EmptyArrayException
*
* @return array
* @throws NotConsistentDataException
*/
private function setArray(array $array)
Expand Down Expand Up @@ -237,6 +240,10 @@ public function join($array, $arrayName, $parentKey, $foreignKey)
*/
public function getResults()
{
if(empty($this->array)){
return [];
}

$results = $this->applySortingFilter($this->applyLimitFilter($this->applyCriteriaFilter($this->applyJoinFilter())));

return array_map([ArrayHelper::class, 'convertToPlainArray'], $results);
Expand Down Expand Up @@ -317,7 +324,7 @@ private function applyJoinFilter()
*/
private function applyCriteriaFilter(array $array)
{
if (count($this->criteria) === 0) {
if (empty($this->criteria) or count($this->criteria) === 0) {
return $array;
}

Expand Down
9 changes: 4 additions & 5 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public function setUp()

/**
* @test
* @expectedException \ArrayQuery\Exceptions\EmptyArrayException
* @expectedExceptionMessage Empty array provided.
*/
public function it_throws_EmptyCollectionException_if_an_empty_array_is_provided()
public function it_return_a_void_array_if_an_empty_array_is_provided()
{
QueryBuilder::create([]);
$qb = QueryBuilder::create([]);

$this->assertEquals([], $qb->getResults());
}

/**
Expand Down Expand Up @@ -195,7 +195,6 @@ public function it_should_get_results_with_no_criteria_applied()
{
foreach ($this->usersArrays as $array) {
$qb = QueryBuilder::create($array);

$this->assertCount(10, $qb->getResults());
}
}
Expand Down

0 comments on commit 25fd806

Please sign in to comment.