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

Select::optionsFromTable()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 12
nop 4
dl 0
loc 26
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\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\SelectModel;
15
use Illuminate\Support\Facades\DB;
16
17
class Select
18
{
19
    use DefaultOption, Join;
20
21
    /**
22
     * @param string $field_name
23
     * @return $this
24
     */
25
    public function foreignKey($field_name)
26
    {
27
        $data = columnSingleton()->getColumn($this->index);
28
        /** @var SelectModel $data */
29
        $data->setForeignKey($field_name);
30
        columnSingleton()->setColumn($this->index, $data);
31
32
        return $this;
33
    }
34
35
    /**
36
     * @param $table string|Model
0 ignored issues
show
Bug introduced by
The type crocodicstudio\crudbooster\types\Model was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
37
     * @param $key_field string
38
     * @param $display_field string
39
     * @param $SQLCondition string|callable
40
     * @return $this
41
     */
42
    public function optionsFromTable($table, $key_field, $display_field, $SQLCondition = null) {
43
44
        if(strpos($table,"App\Models")!==false) {
45
            $table = new $table();
46
            $table = $table::$tableName;
47
        }
48
49
        $data = DB::table($table);
50
        if($SQLCondition && is_callable($SQLCondition)) {
51
            $data = call_user_func($SQLCondition, $data);
52
        }elseif ($SQLCondition && is_string($SQLCondition)) {
53
            $data->whereRaw($SQLCondition);
54
        }
55
        $data = $data->get();
56
        $options = [];
57
        foreach ($data as $d) {
58
            $options[ $d->$key_field ] = $d->$display_field;
59
        }
60
        $data = columnSingleton()->getColumn($this->index);
61
        /** @var $data SelectModel */
62
        $data->setOptionsFromTable(["table"=>$table,"key_field"=>$key_field,"display_field"=>$display_field,"sql_condition"=>$SQLCondition]);
63
        columnSingleton()->setColumn($this->index, $data);
64
65
        $this->options($options);
66
67
        return $this;
68
    }
69
70
    public function optionsFromQuery(callable $query) {
71
        $data = columnSingleton()->getColumn($this->index);
72
        /** @var $data SelectModel */
73
        $data->setOptionsFromQuery($query);
74
75
        columnSingleton()->setColumn($this->index, $data);
76
77
        $result = call_user_func($query);
78
        if($result) {
79
            $options = [];
80
            foreach($result as $r) {
81
                $options[ $r->key ] = $r->label;
82
            }
83
            $this->options($options);
84
        }
85
86
        return $this;
87
    }
88
89
    public function options($data_options) {
90
        $data = columnSingleton()->getColumn($this->index);
91
        /** @var $data SelectModel */
92
        $data->setOptions($data_options);
93
94
        columnSingleton()->setColumn($this->index, $data);
95
96
        return $this;
97
    }
98
}