Range::lte()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of PHPinnacle/Elastics.
4
 *
5
 * (c) PHPinnacle Team <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace PHPinnacle\Elastics\Query;
14
15
use PHPinnacle\Elastics\Query;
16
use PHPinnacle\Elastics\Traits;
17
18
class Range implements Query
19
{
20
    use Traits\Boost;
21
22
    /**
23
     * @var string
24
     */
25
    private $field;
26
27
    /**
28
     * @var array
29
     */
30
    private $left;
31
32
    /**
33
     * @var array
34
     */
35
    private $right;
36
37
    /**
38
     * @param string $field
39
     * @param mixed  $gte
40
     * @param mixed  $lte
41
     */
42 6
    public function __construct(string $field, $gte = null, $lte = null)
43
    {
44 6
        $this->field = $field;
45
46 6
        $this->gte($gte);
47 6
        $this->lte($lte);
48 6
    }
49
50
    /**
51
     * @param mixed $value
52
     *
53
     * @return self
54
     */
55 1
    public function gt($value): self
56
    {
57 1
        $this->limit('left', 'gt', $value);
58
59 1
        return $this;
60
    }
61
62
    /**
63
     * @param mixed $value
64
     *
65
     * @return self
66
     */
67 6
    public function gte($value): self
68
    {
69 6
        $this->limit('left', 'gte', $value);
70
71 6
        return $this;
72
    }
73
74
    /**
75
     * @param mixed $value
76
     *
77
     * @return self
78
     */
79 1
    public function lt($value): self
80
    {
81 1
        $this->limit('right', 'lt', $value);
82
83 1
        return $this;
84
    }
85
86
    /**
87
     * @param mixed $value
88
     *
89
     * @return self
90
     */
91 6
    public function lte($value): self
92
    {
93 6
        $this->limit('right', 'lte', $value);
94
95 6
        return $this;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 1
    public function name(): string
102
    {
103 1
        return 'range';
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 5
    public function compile(): array
110
    {
111 5
        $query = (array) $this->left + (array) $this->right;
112
113 5
        if ($this->boost) {
114 1
            $query['boost'] = $this->boost;
115
        }
116
117
        return [
118 5
            $this->field => $query,
119
        ];
120
    }
121
122
    /**
123
     * @param $type
124
     * @param $value
125
     * @param $field
126
     */
127 6
    private function limit(string $field, string $type, $value)
128
    {
129 6
        if ($value === null) {
130 4
            $this->{$field} = null;
131 4
        } elseif ($value instanceof \DateTimeInterface) {
132 1
            $this->{$field} = [
133 1
                $type => $value->format(\DATE_RFC3339),
134
            ];
135 3
        } elseif (\is_scalar($value)) {
136 3
            $this->{$field} = [
137 3
                $type => $value,
138
            ];
139
        }
140 6
    }
141
}
142