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

Compare::withValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
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