-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
FilterCompare.php
48 lines (43 loc) · 1.19 KB
/
FilterCompare.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
<?php
/**
* @link https://github.com/illuminatech
* @copyright Copyright (c) 2019 Illuminatech
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
*/
namespace Illuminatech\DataProvider\Filters;
/**
* FilterCompare performs attribute comparison using specified comparison operator like '<', '>', '<=', '>=' and so on.
*
* Usage example:
*
* ```php
* DataProvider(Item::class)
* ->filters([
* 'price_from' => new FilterCompare('price', '>='),
* 'price_to' => new FilterCompare('price', '<='),
* ]);
* ```
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 1.0
*/
class FilterCompare extends FilterRelatedRecursive
{
/**
* @var string operator to be used for filter value comparison.
* For example: '<', '>', '<=', '>=' and so on.
*/
public $operator;
public function __construct(string $target, string $operator)
{
$this->operator = $operator;
parent::__construct($target);
}
/**
* {@inheritdoc}
*/
protected function applyInternal(object $source, string $target, string $name, $value): object
{
return $source->where($target, $this->operator, $value);
}
}