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

Between   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 25%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 49
ccs 4
cts 16
cp 0.25
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validateValue() 0 6 3
A toArray() 0 3 1
A __construct() 0 9 1
A getOperator() 0 3 1
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