Completed
Push — master ( 4d6fed...0fcf63 )
by Adrian
02:39
created

Create   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 21
dl 0
loc 48
rs 10
c 1
b 0
f 1
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setIndex() 0 3 1
B setBody() 0 25 9
A getIndex() 0 3 1
1
<?php
2
3
namespace Manticoresearch\Endpoints\Indices;
4
5
use Manticoresearch\Endpoints\EmulateBySql;
6
use Manticoresearch\Exceptions\RuntimeException;
7
use Manticoresearch\Utils;
8
9
/**
10
 * Class Create
11
 * @package Manticoresearch\Endpoints\Indices
12
 */
13
class Create extends EmulateBySql
14
{
15
    use Utils;
16
    /**
17
     * @var string
18
     */
19
    protected $_index;
20
21
    public function setBody($params = null)
22
    {
23
        if (isset($this->_index)) {
24
            $columns = [];
25
            if(isset($params['columns'] )) {
26
                foreach ($params['columns'] as $name => $settings) {
27
                    $column = $name . ' ' . $settings['type'];
28
                    if (isset($settings['options']) && count($settings['options']) > 0) {
29
                        $column .= ' ' . implode(' ', $settings['options']);
30
                    }
31
                    $columns[] = $column;
32
                }
33
            }
34
            $options = "";
35
            if(isset($params['settings'] )) {
36
                foreach($params['settings'] as $name=>$value) {
37
                    $options.=" ".$name." = ".$value;
38
                }
39
40
            }
41
            return parent::setBody(['query' => "CREATE TABLE ".$this->_index.
42
                (count($columns)>0?"(".implode(",",$columns).")":" ")
43
                .$options]);
44
        }
45
        throw new RuntimeException('Index name is missing.');
46
    }
47
    /**
48
     * @return mixed
49
     */
50
    public function getIndex()
51
    {
52
        return $this->_index;
53
    }
54
55
    /**
56
     * @param mixed $index
57
     */
58
    public function setIndex($index)
59
    {
60
        $this->_index = $index;
61
    }
62
63
}