Passed
Push — master ( 5403d4...38a665 )
by Ferry
04:29
created

SelectQuery   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 40
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A options() 0 8 1
A optionsFromQuery() 0 22 4
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: User
5
 * Date: 1/26/2019
6
 * Time: 6:00 PM
7
 */
8
9
namespace crocodicstudio\crudbooster\types;
10
11
use crocodicstudio\crudbooster\controllers\scaffolding\traits\DefaultOption;
12
use crocodicstudio\crudbooster\controllers\scaffolding\traits\Join;
13
use crocodicstudio\crudbooster\models\ColumnModel;
14
use crocodicstudio\crudbooster\types\select_query\SelectQueryModel;
15
use Illuminate\Support\Facades\DB;
16
17
class SelectQuery
18
{
19
    use DefaultOption, Join;
20
21
    /**
22
     * @param callable|string $query DB Query Builder|SQL RAW
23
     * @return $this
24
     */
25
    public function optionsFromQuery($query) {
26
        $data = columnSingleton()->getColumn($this->index);
27
        /** @var $data SelectQueryModel */
28
        $data->setOptionsFromQuery($query);
29
30
        columnSingleton()->setColumn($this->index, $data);
31
32
        if(is_callable($query)) {
33
            $result = call_user_func($query);
34
        }else{
35
            $result = DB::select(DB::raw($query));
36
        }
37
38
        if($result) {
39
            $options = [];
40
            foreach($result as $r) {
41
                $options[ $r->key ] = $r->label;
42
            }
43
            $this->options($options);
44
        }
45
46
        return $this;
47
    }
48
49
    private function options($data_options) {
50
        $data = columnSingleton()->getColumn($this->index);
51
        /** @var $data SelectQueryModel */
52
        $data->setOptions($data_options);
53
54
        columnSingleton()->setColumn($this->index, $data);
55
56
        return $this;
57
    }
58
}