Passed
Push — master ( 41f0d3...562c33 )
by Sergei
02:47
created

Compare   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 32
ccs 11
cts 11
cp 1
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setValue() 0 4 1
A withValue() 0 5 1
A toCriteriaArray() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Data\Reader\Filter;
6
7
use DateTimeInterface;
8
use Yiisoft\Data\Reader\FilterAssert;
9
use Yiisoft\Data\Reader\FilterInterface;
10
11
/**
12
 * `Compare` filter is a base class that defines a criteria for comparing field value with a given value.
13
 * The operator is defined by child classes.
14
 */
15
abstract class Compare implements FilterInterface
16
{
17
    private bool|DateTimeInterface|float|int|string $value;
18
19
    /**
20
     * @param string $field Name of the field to compare.
21
     * @param bool|DateTimeInterface|float|int|string $value Value to compare to.
22
     */
23 116
    public function __construct(private string $field, mixed $value)
24
    {
25 116
        $this->setValue($value);
26
    }
27
28
    /**
29
     * @param bool|DateTimeInterface|float|int|string $value Value to compare to.
30
     */
31 2
    final public function withValue(mixed $value): static
32
    {
33 2
        $new = clone $this;
34 2
        $new->setValue($value);
35 2
        return $new;
36
    }
37
38 90
    public function toCriteriaArray(): array
39
    {
40 90
        return [static::getOperator(), $this->field, $this->value];
41
    }
42
43 116
    private function setValue(mixed $value): void
44
    {
45 116
        FilterAssert::isScalarOrInstanceOfDateTimeInterface($value);
46 96
        $this->value = $value;
47
    }
48
}
49