Completed
Push — master ( 86815f...7e02e6 )
by wen
02:47
created

Column::getModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 2
nop 0
1
<?php
2
3
4
namespace Sco\Admin\View\Columns;
5
6
use Illuminate\Database\Eloquent\Model;
7
use JsonSerializable;
8
use Illuminate\Contracts\Support\Arrayable;
9
use Illuminate\Contracts\Support\Jsonable;
10
use Sco\Admin\Contracts\ColumnInterface;
11
12
abstract class Column implements ColumnInterface, Arrayable, Jsonable, JsonSerializable
13
{
14
    protected $name;
15
16
    protected $label;
17
18
    protected $width = 0;
19
20
    protected $minWidth = 80;
21
22
    protected $sortable = false;
23
24
    protected $fixed = false;
25
26
    protected $model;
27
28
    public function __construct($name, $label)
29
    {
30
        $this->name  = $name;
31
        $this->label = $label;
32
    }
33
34
    public function getName()
35
    {
36
        return $this->name;
37
    }
38
39
    public function setWidth($width)
40
    {
41
        $this->width = $width;
42
43
        return $this;
44
    }
45
46
    public function setMinWidth($width)
47
    {
48
        $this->minWidth = $width;
49
50
        return $this;
51
    }
52
53
    public function isSortable()
54
    {
55
        $this->sortable = true;
56
57
        return $this;
58
    }
59
60
    public function isCustomSortable()
61
    {
62
        $this->sortable = 'custom';
0 ignored issues
show
Documentation Bug introduced by
The property $sortable was declared of type boolean, but 'custom' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
63
        // TODO
64
        // register sort route
65
66
        return $this;
67
    }
68
69
    public function isFixed()
70
    {
71
        $this->fixed = true;
72
73
        return $this;
74
    }
75
76
    public function getModel()
77
    {
78
        return $this->model;
79
    }
80
81
    public function setModel(Model $model)
82
    {
83
        $this->model = $model;
84
        return $this;
85
    }
86
87
    public function toArray()
88
    {
89
        return [
90
            'name'     => $this->getName(),
91
            'label'    => $this->label,
92
            'width'    => $this->width,
93
            'fixed'    => $this->fixed,
94
            'minWidth' => $this->minWidth,
95
            'sortable' => $this->sortable,
96
        ];
97
    }
98
99
    public function getModelValue()
100
    {
101
        return $this->getModel()->{$this->getName()};
102
    }
103
104
    public function jsonSerialize()
105
    {
106
        return $this->toArray();
107
    }
108
109
    public function toJson($options = 0)
110
    {
111
        return json_encode($this->jsonSerialize(), $options);
112
    }
113
114
    public function __toString()
115
    {
116
        return $this->toJson();
117
    }
118
}
119