Passed
Pull Request — master (#153)
by Alexander
05:35 queued 02:55
created

Compare::setValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
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 81
    public function __construct(private string $field, mixed $value)
24
    {
25 81
        $this->setValue($value);
26
    }
27
28
    /**
29
     * @param bool|DateTimeInterface|float|int|string $value Value to compare to.
30
     */
31 1
    final public function withValue(mixed $value): static
32
    {
33 1
        $new = clone $this;
34 1
        $new->setValue($value);
35 1
        return $new;
36
    }
37
38 55
    public function toCriteriaArray(): array
39
    {
40 55
        return [static::getOperator(), $this->field, $this->value];
41
    }
42
43 81
    private function setValue(mixed $value): void
44
    {
45 81
        FilterAssert::isScalarOrInstanceOfDateTimeInterface($value);
46 61
        $this->value = $value;
47
    }
48
}
49