Completed
Pull Request — master (#7)
by Gordon
02:13
created

Suggest::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
4
namespace Manticoresearch\Endpoints;
5
6
7
use Manticoresearch\Exceptions\RuntimeException;
8
use Manticoresearch\Utils;
9
10
class Suggest extends EmulateBySql
11
{
12
    use Utils;
13
    protected $_index;
14
15
    public function __construct($params = [])
16
    {
17
        if (isset($params['index'])) {
18
            $this->setIndex($params['index']);
19
        }
20
21
        parent::__construct($params);
22
    }
23
24
    public function setBody($params = null)
25
    {
26
        if (isset($this->_index)) {
27
            $binds =[];
28
            $binds[] = "'" . Utils::escape($params['query']) . "'";
29
            $binds[] = "'" . $this->_index . "'";
30
            if (count($params['options']) > 0) {
31
                foreach ($params['options'] as $name => $value) {
32
                    $binds[] = "$value AS $name";
33
                }
34
            }
35
            $this->_body = ['query' => "CALL SUGGEST(" . implode(",", $binds) . ")"];
36
        } else {
37
            throw new RuntimeException('Index name is missing.');
38
        }
39
40
    }
41
    /**
42
     * @return mixed
43
     */
44
    public function getIndex()
45
    {
46
        return $this->_index;
47
    }
48
49
    /**
50
     * @param mixed $index
51
     */
52
    public function setIndex($index)
53
    {
54
        $this->_index = $index;
55
    }
56
}
57