Search   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 145
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0
wmc 16

10 Methods

Rating   Name   Duplication   Size   Complexity  
A after() 0 5 1
A order() 0 11 3
A jsonSerialize() 0 3 1
A matchNone() 0 3 1
A __construct() 0 3 1
A __toString() 0 3 1
A body() 0 19 4
A match() 0 3 1
A matchAll() 0 9 2
A limit() 0 5 1
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;
14
15
class Search implements Request
16
{
17
    /**
18
     * @var Query
19
     */
20
    private $query;
21
22
    /**
23
     * @var int
24
     */
25
    private $limit;
26
27
    /**
28
     * @var array
29
     */
30
    private $sort = [];
31
32
    /**
33
     * @var array
34
     */
35
    private $after = [];
36
37
    /**
38
     * @param Query $query
39
     */
40 9
    private function __construct(Query $query)
41
    {
42 9
        $this->query = $query;
43 9
    }
44
45
    /**
46
     * @param Query $query
47
     *
48
     * @return self
49
     */
50 3
    public static function match(Query $query): self
51
    {
52 3
        return new self($query);
53
    }
54
55
    /**
56
     * @param float|null $boost
57
     *
58
     * @return self
59
     */
60 5
    public static function matchAll(float $boost = null): self
61
    {
62 5
        $query = new Query\MatchAll();
63
64 5
        if ($boost !== null) {
65 1
            $query->boost($boost);
66
        }
67
68 5
        return new self($query);
69
    }
70
71
    /**
72
     * @return self
73
     */
74 1
    public static function matchNone(): self
75
    {
76 1
        return new self(new Query\MatchNone());
77
    }
78
79
    /**
80
     * @param int $limit
81
     *
82
     * @return self
83
     */
84 1
    public function limit(int $limit): self
85
    {
86 1
        $this->limit = $limit;
87
88 1
        return $this;
89
    }
90
91
    /**
92
     * @param string $field
93
     * @param string $direction
94
     *
95
     * @return self
96
     */
97 2
    public function order(string $field, string $direction): self
98
    {
99 2
        $direction = \strtolower($direction);
100
101 2
        if ($direction !== \ELASTICS_SORT_ASC && $direction !== \ELASTICS_SORT_DESC) {
102 1
            throw new Exception\InvalidSortDirection($direction);
103
        }
104
105 1
        $this->sort[$field] = $direction;
106
107 1
        return $this;
108
    }
109
110
    /**
111
     * @param array $after
112
     *
113
     * @return self
114
     */
115 1
    public function after(array $after): self
116
    {
117 1
        $this->after = $after;
118
119 1
        return $this;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 8
    public function body(): array
126
    {
127
        $search = [
128 8
            'query' => \elastics_compile($this->query),
129
        ];
130
131 8
        if ($this->limit !== null) {
132 1
            $search['size'] = $this->limit;
133
        }
134
135 8
        if (!empty($this->sort)) {
136 1
            $search['sort'] = $this->sort;
137
        }
138
139 8
        if (!empty($this->after)) {
140 1
            $search['search_after'] = $this->after;
141
        }
142
143 8
        return $search;
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149 2
    public function jsonSerialize()
150
    {
151 2
        return $this->body();
152
    }
153
154
    /**
155
     * @return string
156
     */
157 1
    public function __toString()
158
    {
159 1
        return \elastics_encode($this->jsonSerialize());
160
    }
161
}
162