Passed
Push — master ( f486d0...d9eeb8 )
by Alexander
08:37 queued 05:48
created

Between::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Data\Reader\Filter;
6
7
use InvalidArgumentException;
8
9
use function get_class;
10
use function gettype;
11
use function is_scalar;
12
use function is_object;
13
use function sprintf;
14
15
final class Between implements FilterInterface
16
{
17
    private string $field;
18
19
    /**
20
     * @var bool|float|int|string
21
     */
22
    private $firstValue;
23
24
    /**
25
     * @var bool|float|int|string
26
     */
27
    private $secondValue;
28
29
    /**
30
     * @param string $field
31
     * @param bool|float|int|string $firstValue
32
     * @param bool|float|int|string $secondValue
33
     */
34
    public function __construct(string $field, $firstValue, $secondValue)
35
    {
36
        $this->field = $field;
37
38
        $this->validateValue($firstValue);
39
        $this->validateValue($secondValue);
40
41
        $this->firstValue = $firstValue;
42
        $this->secondValue = $secondValue;
43
    }
44
45 1
    public function toArray(): array
46
    {
47 1
        return [self::getOperator(), $this->field, $this->firstValue, $this->secondValue];
48
    }
49
50 1
    public static function getOperator(): string
51
    {
52 1
        return 'between';
53
    }
54
55
    /**
56
     * @param mixed $value
57
     */
58
    private function validateValue($value): void
59
    {
60
        if (!is_scalar($value)) {
61
            throw new InvalidArgumentException(sprintf(
62
                'The value should be scalar. The %s is received.',
63
                is_object($value) ? get_class($value) : gettype($value),
64
            ));
65
        }
66
    }
67
}
68