Column::getValueFromObject()   B
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
ccs 0
cts 14
cp 0
rs 8.8571
cc 5
eloc 10
nc 6
nop 2
crap 30
1
<?php
2
3
namespace Sco\Admin\Display\Columns;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Collection;
7
use Sco\Admin\Contracts\Display\ColumnInterface;
8
9
abstract class Column implements ColumnInterface
10
{
11
    protected $type;
12
13
    protected $name;
14
15
    protected $label;
16
17
    protected $width = 0;
18
19
    protected $minWidth = 80;
20
21
    protected $sortable = false;
22
23
    protected $fixed = false;
24
25
    /**
26
     * @var Model
27
     */
28
    protected $model;
29
30
    protected $defaultValue = '';
31
32
    public function __construct($name, $label)
33
    {
34
        $this->setName($name)->setLabel($label);
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    public function getType()
41
    {
42
        return $this->type;
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    public function getName()
49
    {
50
        return $this->name;
51
    }
52
53
    /**
54
     * @param string $name
55
     *
56
     * @return Column
57
     */
58
    public function setName($name)
59
    {
60
        $this->name = $name;
61
62
        return $this;
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getLabel()
69
    {
70
        return $this->label;
71
    }
72
73
    /**
74
     * @param string $label
75
     *
76
     * @return Column
77
     */
78
    public function setLabel(string $label)
79
    {
80
        $this->label = $label;
81
82
        return $this;
83
    }
84
85
    /**
86
     * @return int
87
     */
88
    public function getWidth()
89
    {
90
        return $this->width;
91
    }
92
93
    /**
94
     * @param int $width
95
     *
96
     * @return Column
97
     */
98
    public function setWidth(int $width)
99
    {
100
        $this->width = $width;
101
102
        return $this;
103
    }
104
105
    /**
106
     * @return int
107
     */
108
    public function getMinWidth()
109
    {
110
        return $this->minWidth;
111
    }
112
113
    /**
114
     * @param int $minWidth
115
     *
116
     * @return Column
117
     */
118
    public function setMinWidth(int $minWidth)
119
    {
120
        $this->minWidth = $minWidth;
121
122
        return $this;
123
    }
124
125
    public function getSortable()
126
    {
127
        return $this->sortable;
128
    }
129
130
    /**
131
     * @return $this
132
     */
133
    public function sortable()
134
    {
135
        $this->sortable = true;
136
137
        return $this;
138
    }
139
140
    public function isFixed()
141
    {
142
        return $this->fixed;
143
    }
144
145
    public function enableFixed()
146
    {
147
        $this->fixed = true;
148
149
        return $this;
150
    }
151
152
    public function getModel()
153
    {
154
        return $this->model;
155
    }
156
157
    public function setModel(Model $model)
158
    {
159
        $this->model = $model;
160
161
        return $this;
162
    }
163
164
    /**
165
     * The column options
166
     *
167
     * @return array
168
     */
169
    public function toArray()
170
    {
171
        return [
172
            'name'     => $this->getName(),
173
            'label'    => $this->getLabel(),
174
            'width'    => $this->getWidth(),
175
            'fixed'    => $this->isFixed(),
176
            'minWidth' => $this->getMinWidth(),
177
            'sortable' => $this->getSortable(),
178
            'type'     => $this->getType(),
179
        ];
180
    }
181
182
    /**
183
     * Set the column default value
184
     *
185
     * @param mixed $value
186
     *
187
     * @return $this
188
     */
189
    public function setDefaultValue($value)
190
    {
191
        $this->defaultValue = $value;
192
193
        return $this;
194
    }
195
196
    public function getDefaultValue()
197
    {
198
        return $this->defaultValue;
199
    }
200
201
    /**
202
     * Get the column value
203
     *
204
     * @return string|static
205
     */
206
    public function getValue()
207
    {
208
        $value = $this->getModelValue();
209
        if (is_null($value)) {
210
            $value = $this->getDefaultValue();
211
        }
212
213
        return $value;
214
    }
215
216
    protected function getModelValue()
217
    {
218
        return $this->getValueFromObject($this->getModel(), $this->getName());
219
    }
220
221
    protected function getValueFromObject($instance, $name)
222
    {
223
        $parts = explode('.', $name);
224
        $part = array_shift($parts);
225
226
        if ($instance instanceof Collection) {
227
            $instance = $instance->pluck($part);
228
        } elseif (! is_null($instance)) {
229
            $instance = $instance->getAttribute($part);
230
        }
231
232
        if (! empty($parts) && ! is_null($instance)) {
233
            return $this->getValueFromObject($instance, implode('.', $parts));
234
        }
235
236
        return $instance;
237
    }
238
239
    public function jsonSerialize()
240
    {
241
        return $this->toArray();
242
    }
243
244
    public function toJson($options = 0)
245
    {
246
        return json_encode($this->jsonSerialize(), $options);
247
    }
248
249
    public function __toString()
250
    {
251
        return $this->toJson();
252
    }
253
}
254