-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathRangeQuery.php
More file actions
79 lines (68 loc) · 2.7 KB
/
Copy pathRangeQuery.php
File metadata and controls
79 lines (68 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
namespace Novaway\ElasticsearchClient\Query\Term;
use Novaway\ElasticsearchClient\Filter\Filter;
use Novaway\ElasticsearchClient\Query\CombiningFactor;
use Novaway\ElasticsearchClient\Query\Query;
use Webmozart\Assert\Assert;
/**
* https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html
*/
class RangeQuery implements Filter, Query
{
/** @deprecated use Novaway\ElasticsearchClient\Query\Term\RangeOperator::GREATER_THAN_OPERATOR instead */
const GREATER_THAN_OPERATOR = 'gt';
/** @deprecated use Novaway\ElasticsearchClient\Query\Term\RangeOperator::GREATER_THAN_OR_EQUAL_OPERATOR instead */
const GREATER_THAN_OR_EQUAL_OPERATOR = 'gte';
/** @deprecated use Novaway\ElasticsearchClient\Query\Term\RangeOperator::LESS_THAN_OPERATOR instead */
const LESS_THAN_OPERATOR = 'lt';
/** @deprecated use Novaway\ElasticsearchClient\Query\Term\RangeOperator::LESS_THAN_OR_EQUAL_OPERATOR instead */
const LESS_THAN_OR_EQUAL_OPERATOR = 'lte';
/** @var string */
private $property;
/** @var mixed */
private $value;
/** @var array */
private $operator;
/** @var string */
private $combiningFactor;
public function __construct(string $property, $value, $operator, string $combiningFactor = CombiningFactor::FILTER)
{
Assert::oneOf($combiningFactor, CombiningFactor::toArray());
if(is_array($value) && !is_array($operator)) {
throw new \InvalidArgumentException("Operator should be an array when range filter value is an array");
}
if(!is_array($value) && is_array($operator)) {
throw new \InvalidArgumentException("Operator can't be an array if range filter is not an array");
}
if(is_array($value) && is_array($operator) && count($value) !== count($operator)) {
throw new \InvalidArgumentException("Number of provided operator does not match number of provided values");
}
$this->property = $property;
$this->value = is_array($value) ? $value : [$value];
$this->operator = is_array($operator) ? $operator : [$operator];
$this->combiningFactor = $combiningFactor;
}
/**
* @return string
*/
public function getCombiningFactor(): string
{
return $this->combiningFactor;
}
/**
* @inheritDoc
*/
public function formatForQuery(): array
{
$rangeConditions = [];
$valueCount = count($this->value);
for ($i = 0; $i < $valueCount; $i++){
$rangeConditions[] = [$this->operator[$i] => $this->value[$i]];
}
return [
'range' => [
$this->property => $rangeConditions
]
];
}
}