Passed
Push — master ( cae081...017bc7 )
by Ferry
06:02
created

Radio::optionsFromTable()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 12
nop 4
dl 0
loc 19
rs 8.8333
c 0
b 0
f 0
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\Cbmodel\Core\Model;
12
use crocodicstudio\crudbooster\controllers\scaffolding\traits\DefaultOption;
13
use crocodicstudio\crudbooster\controllers\scaffolding\traits\Join;
14
use crocodicstudio\crudbooster\types\radio\RadioModel;
15
16
class Radio
17
{
18
    use DefaultOption, Join;
19
20
    public function options($data_options) {
21
        $data = columnSingleton()->getColumn($this->index);
22
        /** @var $data RadioModel */
23
        $data->setOptions($data_options);
24
25
        columnSingleton()->setColumn($this->index, $data);
26
27
        return $this;
28
    }
29
30
    /**
31
     * @param $table string|Model
32
     * @param $key_field string
33
     * @param $display_field string
34
     * @param $SQLCondition string|callable
35
     */
36
    public function optionsFromTable($table, $key_field, $display_field, $SQLCondition = null) {
37
38
        if(strpos($table,"App\Models")!==false) {
39
            $table = new $table();
40
            $table = $table::$tableName;
41
        }
42
43
        $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...
44
        if($SQLCondition && is_callable($SQLCondition)) {
45
            $data = call_user_func($SQLCondition, $data);
46
        }elseif ($SQLCondition && is_string($SQLCondition)) {
47
            $data->whereRaw($SQLCondition);
48
        }
49
        $data = $data->get();
50
        $options = [];
51
        foreach ($data as $d) {
52
            $options[ $d->$key_field ] = $d->$display_field;
53
        }
54
        $this->options($options);
55
    }
56
}