Passed
Push — master ( db1581...a5af1f )
by Ferry
04:30
created

Radio::optionsFromTable()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 13
rs 9.2222
c 0
b 0
f 0
cc 6
nc 6
nop 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\types\radio\RadioModel;
14
15
class Radio
16
{
17
    use DefaultOption, Join;
18
19
    public function options($data_options) {
20
        $data = columnSingleton()->getColumn($this->index);
21
        /** @var $data RadioModel */
22
        $data->setOptions($data_options);
23
24
        columnSingleton()->setColumn($this->index, $data);
25
26
        return $this;
27
    }
28
29
    /**
30
     * @param $table string
31
     * @param $key_field string
32
     * @param $display_field string
33
     * @param $SQLCondition string|callable
34
     */
35
    public function optionsFromTable($table, $key_field, $display_field, $SQLCondition = null) {
36
        $data = DB::table($table);
0 ignored issues
show
Bug introduced by
The type crocodicstudio\crudbooster\types\DB was not found. Did you mean DB? If so, make sure to prefix the type with \.
Loading history...
37
        if($SQLCondition && is_callable($SQLCondition)) {
38
            $data = call_user_func($SQLCondition, $data);
39
        }elseif ($SQLCondition && is_string($SQLCondition)) {
40
            $data->whereRaw($SQLCondition);
41
        }
42
        $data = $data->get();
43
        $options = [];
44
        foreach ($data as $d) {
45
            $options[ $d->$key_field ] = $d->$display_field;
46
        }
47
        $this->options($options);
48
    }
49
}