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

ColumnSingleton::setColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: User
5
 * Date: 1/26/2019
6
 * Time: 6:03 PM
7
 */
8
9
namespace crocodicstudio\crudbooster\controllers\scaffolding\singletons;
10
11
12
use crocodicstudio\crudbooster\models\ColumnModel;
13
14
class ColumnSingleton
15
{
16
17
    private $columns;
18
    private $joins;
19
20
    public function newColumns() {
21
        $this->columns = [];
22
    }
23
24
    public function addJoin($data)
25
    {
26
        $this->joins[] = $data;
27
    }
28
29
    public function getJoin()
30
    {
31
        return $this->joins;
32
    }
33
34
    /**
35
     * @return mixed
36
     */
37
    public function getColumns()
38
    {
39
        return $this->columns;
40
    }
41
42
    public function valueAssignment($data_row = null) {
43
        foreach ($this->getColumns() as $index=>$column) {
44
            /** @var ColumnModel $column */
45
            if($data_row) {
46
                $value = $data_row->{ $column->getField() };
47
            }else{
48
                $value = request($column->getName());
49
            }
50
51
            if (! $column->getName()) {
52
                continue;
53
            }
54
55
            if(!$value && $column->getDefaultValue()) {
56
                $value = $column->getDefaultValue();
57
            }
58
59
            $value = getTypeHook($column->getType())->assignment($value, $column);
60
61
            $column->setValue($value);
62
63
            $this->setColumn($index, $column);
64
        }
65
    }
66
67
    public function getIndexColumns()
68
    {
69
        $data = $this->columns;
70
        foreach($data as $i=>$item) {
71
            /** @var ColumnModel $item */
72
            if($item->getShowIndex() === false) {
73
                unset($data[$i]);
74
            }
75
        }
76
        return $data;
77
    }
78
79
    public function getAddEditColumns()
80
    {
81
        $data = $this->columns;
82
        foreach($data as $i=>$item) {
83
            /** @var ColumnModel $item */
84
            if($item->getShowIndex() === false) {
85
                unset($data[$i]);
86
            }
87
88
            if($item->getShowDetail() === false) {
89
                unset($data[$i]);
90
            }
91
92
        }
93
        return $data;
94
    }
95
96
    public function getEditColumns()
97
    {
98
        $data = $this->columns;
99
        foreach($data as $i=>$item) {
100
            /** @var ColumnModel $item */
101
            if($item->getShowEdit() === false) {
102
                unset($data[$i]);
103
            }
104
        }
105
        return $data;
106
    }
107
108
    public function getAddColumns()
109
    {
110
        $data = $this->columns;
111
        foreach($data as $i=>$item) {
112
            /** @var ColumnModel $item */
113
            if($item->getShowAdd() === false) {
114
                unset($data[$i]);
115
            }
116
        }
117
        return $data;
118
    }
119
120
    public function getDetailColumns()
121
    {
122
        $data = $this->columns;
123
        foreach($data as $i=>$item) {
124
            /** @var ColumnModel $item */
125
            if($item->getShowDetail() === false) {
126
                unset($data[$i]);
127
            }
128
        }
129
        return $data;
130
    }
131
132
    public function getAssignmentData()
133
    {
134
        $data = [];
135
        foreach($this->columns as $column) {
136
            /** @var ColumnModel $column */
137
            if(is_array($column->getValue())) {
138
                foreach($column->getValue() as $key=>$val) {
139
                    $data[$key] = $val;
140
                }
141
            }else{
142
                $data[$column->getField()] = $column->getValue();
143
            }
144
        }
145
        return $data;
146
    }
147
148
    public function removeColumn($label_or_name)
149
    {
150
        $data = $this->getColumns();
151
        foreach($data as $i=>$d)
152
        {
153
            /** @var ColumnModel $d */
154
            if($d->getLabel() == $label_or_name || $d->getName() == $label_or_name) {
155
                unset($data[$i]);
156
            }
157
        }
158
        $this->columns = $data;
159
    }
160
161
    public function getColumnNameOnly()
162
    {
163
        $result = [];
164
        foreach($this->columns as $column) {
165
            /** @var ColumnModel $column */
166
            $result[] = $column->getName();
167
        }
168
        return $result;
169
    }
170
171
    /**
172
     * @param int $index
173
     * @param ColumnModel $value
174
     */
175
    public function setColumn($index, ColumnModel $value)
176
    {
177
        $this->columns[$index] = $value;
178
    }
179
180
    /**
181
     * @param int $index
182
     * @return ColumnModel
183
     */
184
    public function getColumn($index) {
185
        return $this->columns[$index];
186
    }
187
188
    public function setColumnArray($index, $key, $values)
189
    {
190
        $this->columns[$index][$key][] = $values;
191
    }
192
}