/
Compare.php
59 lines (50 loc) · 1.29 KB
/
Compare.php
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
<?php
declare(strict_types=1);
namespace Search\Model\Filter;
use InvalidArgumentException;
class Compare extends Base
{
/**
* Default configuration.
*
* @var array<string, mixed>
*/
protected array $_defaultConfig = [
'operator' => '>=',
'mode' => 'AND',
];
/**
* Allowed operators.
*
* @var array<string>
*/
protected array $_operators = [
'>=', '<=', '<', '>',
];
/**
* Process a comparison-based condition (e.g. $field <= $value).
*
* @return bool
* @throws \InvalidArgumentException
*/
public function process(): bool
{
$conditions = [];
if (!in_array($this->getConfig('operator'), $this->_operators, true)) {
throw new InvalidArgumentException(sprintf(
'The operator %s is invalid!',
$this->getConfig('operator')
));
}
$value = $this->value();
if (!is_scalar($value)) {
return false;
}
foreach ($this->fields() as $field) {
$left = $field . ' ' . $this->getConfig('operator');
$conditions[] = [$left => $value];
}
$this->getQuery()->andWhere([$this->getConfig('mode') => $conditions]);
return true;
}
}