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

prevent mix keys in filters (#936) #939

Merged
merged 3 commits into from
Sep 18, 2015
Merged
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
37 changes: 30 additions & 7 deletions lib/Elastica/Aggregation/Filters.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Elastica\Aggregation;

use Elastica\Exception\InvalidException;
use Elastica\Filter\AbstractFilter;

/**
Expand All @@ -10,6 +11,14 @@
*/
class Filters extends AbstractAggregation
{
const NAMED_TYPE = 1;
const ANONYMOUS_TYPE = 2;

/**
* @var int Type of bucket keys - named, or anonymous
*/
private $_type = null;

/**
* Add a filter.
*
Expand All @@ -22,14 +31,30 @@ class Filters extends AbstractAggregation
*/
public function addFilter(AbstractFilter $filter, $name = null)
{
if (null !== $name && !is_string($name)) {
throw new InvalidException('Name must be a string');
}

$filterArray = array();

if (is_string($name)) {
$filterArray[$name] = $filter;
} else {
$type = self::NAMED_TYPE;

if (null === $name) {
$filterArray[] = $filter;
$type = self::ANONYMOUS_TYPE;
} else {
$filterArray[$name] = $filter;
}

if ($this->hasParam('filters')
&& count($this->getParam('filters'))
&& $this->_type !== $type
) {
throw new InvalidException('Mix named and anonymous keys are not allowed');
}

$this->_type = $type;

return $this->addParam('filters', $filterArray);
}

Expand All @@ -42,10 +67,8 @@ public function toArray()
$filters = $this->getParam('filters');

foreach ($filters as $filter) {
// Detect between anonymous filters and named ones
$key = key($filter);

if (is_string($key)) {
if (self::NAMED_TYPE === $this->_type) {
$key = key($filter);
$array['filters']['filters'][$key] = current($filter)->toArray();
} else {
$array['filters']['filters'][] = current($filter)->toArray();
Expand Down
35 changes: 35 additions & 0 deletions test/lib/Elastica/Test/Aggregation/FiltersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,41 @@ public function testToArrayUsingNamedFilters()
$this->assertEquals($expected, $agg->toArray());
}

/**
* @group unit
* @expectedException Elastica\Exception\InvalidException
* @expectedExceptionMessage Name must be a string
*/
public function testWrongName()
{
$agg = new Filters('by_color');
$agg->addFilter(new Term(array('color' => '0')), 0);
}

/**
* @group unit
* @expectedException Elastica\Exception\InvalidException
* @expectedExceptionMessage Mix named and anonymous keys are not allowed
*/
public function testMixNamedAndAnonymousFilters()
{
$agg = new Filters('by_color');
$agg->addFilter(new Term(array('color' => '0')), '0');
$agg->addFilter(new Term(array('color' => '0')));
}

/**
* @group unit
* @expectedException Elastica\Exception\InvalidException
* @expectedExceptionMessage Mix named and anonymous keys are not allowed
*/
public function testMixAnonymousAndNamedFilters()
{
$agg = new Filters('by_color');
$agg->addFilter(new Term(array('color' => '0')));
$agg->addFilter(new Term(array('color' => '0')), '0');
}

/**
* @group unit
*/
Expand Down