Skip to content
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

FilterCollectionTrait - hasActiveFilters method #11

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion src/Charcoal/Source/DatabaseSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,13 @@ public function sqlFilters()
}

$criteria = $this->createFilter([
'filters' => $this->filters()
'filters' => array_filter($this->filters(), function ($filter) {
// Check for active subfilters
if ($filter->hasFilters()) {
return $filter->hasActiveFilters();
}
return true;
})
]);

$sql = $criteria->sql();
Expand Down
24 changes: 24 additions & 0 deletions src/Charcoal/Source/FilterCollectionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,30 @@ public function hasFilters()
return !empty($this->filters);
}

/**
* Determine recursively if the object has any active subfilters.
*
* @return boolean
*/
public function hasActiveFilters()
{
$endFilters = [];

// Recusively find filters with no subfilters
$this->traverseFilters(function ($filter) use (&$endFilters) {
if (!$filter->hasFilters()) {
$endFilters[] = $filter;
}
});

// Exclude filters that aren't active
$endFilters = array_filter($endFilters, function ($filter) {
return $filter->active();
});

return !empty($endFilters);
}

/**
* Retrieve the query filters stored in this object.
*
Expand Down
41 changes: 41 additions & 0 deletions tests/Charcoal/Source/FilterCollectionTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,47 @@ public function testHasExpressions()
$this->assertTrue($obj->hasFilters());
}

/**
* Test collection for active filters.
*
* Assertions:
* 1. Empty; Default state
* 2. Populated; Mutated state
* 3. Populated; Added a inactive subfilter
*
* @covers \Charcoal\Source\FilterCollectionTrait::hasActiveFilters
*
* @return void
*/
public function testHasActiveExpressions()
{
$obj = $this->createCollector();

/** 1. Default state */
$this->assertFalse($obj->hasActiveFilters());

/** 2. Mutated state */
$obj->addFilter([
'condition' => '( 1 + 1 = 2 )',
'filters' => $this->dummyItems,
]);

$this->assertTrue($obj->hasActiveFilters());

/** 3. Added a inactive subfilter */
$obj->setFilters([[
'condition' => '( 1 + 1 = 2 )',
'filters' => [
[
'condition' => '( 1 + 1 = 2 )',
'active' => false,
]
]
]]);

$this->assertFalse($obj->hasActiveFilters());
}

/**
* Test the mass assignment of expressions.
*
Expand Down