Skip to content

Commit

Permalink
Allow to configure a subkey when adding a param (#2030)
Browse files Browse the repository at this point in the history
* Allow to configure a subkey when adding a param

* Add changelog
  • Loading branch information
deguif authored Dec 3, 2021
1 parent 2e9b223 commit 256b1ba
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 67 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added `bytes` processor [#2008](https://github.com/ruflin/Elastica/pull/2008)
* Added `indices_boost` option to `Elastica\Query` [#2018](https://github.com/ruflin/Elastica/pull/2018)
* Added `Elastica\Query\Terms::setBoost()` method to configure boost [#2035](https://github.com/ruflin/Elastica/pull/2035)
* Allowed to configure a sub key when adding a param with `Elastica\Param::addParam()` [#2030](https://github.com/ruflin/Elastica/pull/2030)
### Changed
* Triggered deprecation in `Elastica\Result::getType()` method [#2016](https://github.com/ruflin/Elastica/pull/2016)
* Updated `php-cs-fixer` to `3.3.2` [#2022](https://github.com/ruflin/Elastica/pull/2022)
Expand Down
26 changes: 1 addition & 25 deletions src/Aggregation/AdjacencyMatrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ class AdjacencyMatrix extends AbstractAggregation
*/
public function addFilter(AbstractQuery $filter, string $name): self
{
$filterArray = [];
$filterArray[$name] = $filter;

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

/**
Expand All @@ -26,25 +23,4 @@ public function setSeparator(string $separator = '&'): self
{
return $this->setParam('separator', $separator);
}

public function toArray(): array
{
$array = [];
$filters = $this->getParam('filters');

foreach ($filters as $filter) {
$key = \key($filter);
$array['adjacency_matrix']['filters'][$key] = \current($filter)->toArray();
}

if ($this->hasParam('separator')) {
$array['adjacency_matrix']['separator'] = $this->getParam('separator');
}

if ($this->_aggs) {
$array['aggs'] = $this->_convertArrayable($this->_aggs);
}

return $array;
}
}
42 changes: 2 additions & 40 deletions src/Aggregation/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,7 @@ class Filters extends AbstractAggregation
*/
public function addFilter(AbstractQuery $filter, ?string $name = null): self
{
$filterArray = [];

$type = self::NAMED_TYPE;

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

if ($this->hasParam('filters')
&& \count($this->getParam('filters'))
Expand All @@ -49,7 +40,7 @@ public function addFilter(AbstractQuery $filter, ?string $name = null): self

$this->_type = $type;

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

/**
Expand All @@ -67,33 +58,4 @@ public function setOtherBucketKey(string $otherBucketKey): self
{
return $this->setParam('other_bucket_key', $otherBucketKey);
}

public function toArray(): array
{
$array = [];
$filters = $this->getParam('filters');

foreach ($filters as $filter) {
if (self::NAMED_TYPE === $this->_type) {
$key = \key($filter);
$array['filters']['filters'][$key] = \current($filter)->toArray();
} else {
$array['filters']['filters'][] = \current($filter)->toArray();
}
}

if ($this->hasParam('other_bucket')) {
$array['filters']['other_bucket'] = $this->getParam('other_bucket');
}

if ($this->hasParam('other_bucket_key')) {
$array['filters']['other_bucket_key'] = $this->getParam('other_bucket_key');
}

if ($this->_aggs) {
$array['aggs'] = $this->_convertArrayable($this->_aggs);
}

return $array;
}
}
8 changes: 6 additions & 2 deletions src/Param.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,13 @@ public function setParams(array $params)
*
* @return $this
*/
public function addParam($key, $value)
public function addParam($key, $value, ?string $subKey = null)
{
$this->_params[$key][] = $value;
if (null !== $subKey) {
$this->_params[$key][$subKey] = $value;
} else {
$this->_params[$key][] = $value;
}

return $this;
}
Expand Down

0 comments on commit 256b1ba

Please sign in to comment.