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

Checkbox::options()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
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\checkbox\CheckboxModel;
14
15
class Checkbox
16
{
17
    use DefaultOption, Join;
18
19
    /**
20
     * @param array $options Array containing key and value
21
     * @return $this
22
     * @example options(['foo'=>'bar'])
23
     */
24
    public function options($options) {
25
        /** @var CheckboxModel $data */
26
        $data = columnSingleton()->getColumn($this->index);
27
        $data->setCheckboxOptions($options);
28
29
        columnSingleton()->setColumn($this->index, $data);
30
31
        return $this;
32
    }
33
34
    /**
35
     * @param $table string
36
     * @param $key_field string
37
     * @param $display_field string
38
     * @param $SQLCondition string|callable
39
     */
40
    public function optionsFromTable($table, $key_field, $display_field, $SQLCondition = null) {
41
        $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...
42
        if($SQLCondition && is_callable($SQLCondition)) {
43
            $data = call_user_func($SQLCondition, $data);
44
        }elseif ($SQLCondition && is_string($SQLCondition)) {
45
            $data->whereRaw($SQLCondition);
46
        }
47
        $data = $data->get();
48
        $options = [];
49
        foreach ($data as $d) {
50
            $options[ $d->$key_field ] = $d->$display_field;
51
        }
52
        $this->options($options);
53
    }
54
}