-
Notifications
You must be signed in to change notification settings - Fork 732
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for custom range key and refactor addRange in a trait (#2227
) IpRange and GeoDistance aggregations were missing the ability to specify a custom key for a range. As it was already done for the Range aggregation, I moved that to a trait that is now used everywhere we need `addRange`. For the IpRange aggregation, I also added the key option to the `addMaskRange` method. The only drawback with this refactoring is that the arguments of the trait have to allow string|int|float|null to be usable everywhere and we loose some phpdoc details on the arguments. In my opinion, people using this should already know what format is expected and can always check the doc so I personally prefer to have the code deduplicated, but if you disagree I can change it back to add the key everywhere without using a common trait.
- Loading branch information
Showing
7 changed files
with
119 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Elastica\Aggregation\Traits; | ||
|
||
use Elastica\Exception\InvalidException; | ||
|
||
trait RangeTrait | ||
{ | ||
/** | ||
* Add a range to this aggregation. | ||
* | ||
* @param float|int|string|null $fromValue low end of this range, exclusive (greater than or equal to) | ||
* @param float|int|string|null $toValue high end of this range, exclusive (less than) | ||
* @param string|null $key customized key value | ||
* | ||
* @throws InvalidException | ||
* | ||
* @return $this | ||
*/ | ||
public function addRange($fromValue = null, $toValue = null, ?string $key = null): self | ||
{ | ||
if (null === $fromValue && null === $toValue) { | ||
throw new InvalidException('Either fromValue or toValue must be set. Both cannot be null.'); | ||
} | ||
|
||
$range = []; | ||
|
||
if (null !== $fromValue) { | ||
$range['from'] = $fromValue; | ||
} | ||
|
||
if (null !== $toValue) { | ||
$range['to'] = $toValue; | ||
} | ||
|
||
if (null !== $key) { | ||
$range['key'] = $key; | ||
} | ||
|
||
return $this->addParam('ranges', $range); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters