Completed
Pull Request — master (#16)
by Timo
06:00
created

Column::isMutated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace hamburgscleanest\DataTables\Models;
4
5
use hamburgscleanest\DataTables\Interfaces\ColumnFormatter;
6
use Illuminate\Database\Eloquent\Model;
7
8
9
/**
10
 * Class Header
11
 * @package hamburgscleanest\hamburgscleanest\DataTables\Models
12
 */
13
class Column {
14
15
    /** @var string */
16
    private $_key;
17
18
    /** @var string */
19
    private $_name;
20
21
    /** @var bool */
22
    private $_mutated = false;
23
24
    /** @var string */
25
    private $_table;
26
27
    /** @var Relation */
28
    private $_relation;
29
30
    /** @var ColumnFormatter */
31
    private $_formatter;
32
33
    /**
34
     * Column constructor.
35
     * @param string $name
36
     * @param ColumnFormatter|null $columnFormatter
37
     * @param Model|null $sourceModel
38
     */
39 50
    public function __construct(string $name, ? ColumnFormatter $columnFormatter = null, ? Model $sourceModel = null)
40
    {
41 50
        $this->_setName($name);
42 50
        $this->_formatter = $columnFormatter;
43 50
        if ($sourceModel !== null)
44
        {
45 46
            $this->_table = $sourceModel->getTable();
46 46
            $this->_mutated = \in_array($name, $sourceModel->getMutatedAttributes(), true);
47
        }
48 50
    }
49
50
    /**
51
     * @param string $name
52
     */
53 50
    private function _setName(string $name) : void
54
    {
55 50
        $posDivider = \mb_strpos($name, '.');
56 50
        if ($posDivider === false)
57
        {
58 47
            $this->_name = $this->_key = $name;
59
60 47
            return;
61
        }
62
63 6
        $this->_name = \str_replace(')', '', \mb_substr($name, $posDivider + 1));
64
65 6
        $this->_relation = new Relation($name);
66 6
        $aggregate = $this->_relation->aggregate;
67 6
        $this->_key = ($aggregate !== 'first' ? ($aggregate . '_') : '') . $this->_relation->name . '_' . $this->_name;
68 6
    }
69
70
    /**
71
     * @return string
72
     */
73 5
    public function getName() : string
74
    {
75 5
        return $this->_name;
76
    }
77
78
    /**
79
     * @return bool
80
     */
81
    public function isMutated() : bool
82
    {
83
        return $this->_mutated;
84
    }
85
86
    /**
87
     * @return null|Relation
88
     */
89 37
    public function getRelation() : ? Relation
90
    {
91 37
        return $this->_relation;
92
    }
93
94
    /**
95
     * @param ColumnFormatter $columnFormatter
96
     * @return Column
97
     */
98 2
    public function setFormatter(ColumnFormatter $columnFormatter) : Column
99
    {
100 2
        $this->_formatter = $columnFormatter;
101
102 2
        return $this;
103
    }
104
105
    /**
106
     * Get the formatted column value.
107
     *
108
     * @param Model $rowModel
109
     * @return string|null
110
     */
111 35
    public function getFormattedValue(Model $rowModel) : ? string
112
    {
113 35
        $value = $this->getValue($rowModel);
114
115 35
        return $this->_formatter !== null ? $this->_formatter->format($value) : $value;
116
    }
117
118
    /**
119
     * Get the value of this column for the given row.
120
     *
121
     * @param Model $rowModel
122
     * @return string|null
123
     */
124 35
    public function getValue(Model $rowModel) : ? string
125
    {
126 35
        if ($this->_relation !== null)
127
        {
128 4
            return $this->_getValueFromRelation($rowModel);
129
        }
130
131 34
        return $this->_getValue($rowModel);
132
    }
133
134
    /**
135
     * Get the value from the column's relation
136
     *
137
     * @param Model $rowModel
138
     * @return string|null
139
     */
140 4
    private function _getValueFromRelation(Model $rowModel) : ? string
141
    {
142 4
        if ($this->_mutated)
143
        {
144
            return $rowModel->{$this->_relation->name}->{$this->_name};
145
        }
146
147 4
        return $rowModel->{$this->getKey()};
148
    }
149
150
    /**
151
     * @return string
152
     */
153 35
    public function getKey() : string
154
    {
155 35
        return $this->_key;
156
    }
157
158
    /**
159
     * @param Model $rowModel
160
     * @return string|null
161
     */
162 34
    private function _getValue(Model $rowModel) : ? string
163
    {
164 34
        return (string) $rowModel->{$this->_name};
165
    }
166
167
    /**
168
     * @return string
169
     */
170 34
    public function getAttributeName() : string
171
    {
172 34
        if ($this->_relation !== null)
173
        {
174 4
            return $this->_relation->attributeName;
175
        }
176
177 33
        if ($this->_mutated)
178
        {
179 1
            return $this->_name;
180
        }
181
182 32
        return $this->_table . '.' . $this->_name;
183
    }
184
}
185