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

Create::setBody()   B

Complexity

Conditions 9
Paths 5

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 9
eloc 16
nc 5
nop 1
dl 0
loc 25
rs 8.0555
c 1
b 0
f 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
}