Column::setFormatter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace hamburgscleanest\DataTables\Models\Column;
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\Column
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 ColumnRelation */
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 62
    public function __construct(string $name, ? ColumnFormatter $columnFormatter = null, ? Model $sourceModel = null)
40
    {
41 62
        $this->_setName($name);
42 62
        $this->_formatter = $columnFormatter;
43 62
        if ($sourceModel !== null)
44
        {
45 59
            $this->_table = $sourceModel->getTable();
46 59
            $this->_mutated = \in_array($name, $sourceModel->getMutatedAttributes(), true);
47
        }
48 62
    }
49
50
    /**
51
     * @param string $name
52
     */
53 62
    private function _setName(string $name) : void
54
    {
55 62
        $posDivider = \mb_strpos($name, '.');
56 62
        if ($posDivider === false)
57
        {
58 59
            $this->_name = $this->_key = $name;
59
60 59
            return;
61
        }
62
63 6
        $this->_name = \str_replace(')', '', \mb_substr($name, $posDivider + 1));
64
65 6
        $this->_relation = new ColumnRelation($name);
66 6
        $aggregate = $this->_relation->aggregate;
67 6
        $this->_key = ($aggregate !== 'first' ? ($aggregate . '_') : '') . $this->_relation->name;
68 6
    }
69
70
    /**
71
     * @return string
72
     */
73 14
    public function getName() : string
74
    {
75 14
        return $this->_name;
76
    }
77
78
    /**
79
     * @return bool
80
     */
81 47
    public function isMutated() : bool
82
    {
83 47
        return $this->_mutated;
84
    }
85
86
    /**
87
     * @return null|ColumnRelation
88
     */
89 3
    public function getRelation() : ? ColumnRelation
90
    {
91 3
        return $this->_relation;
92
    }
93
94
    /**
95
     * @param ColumnFormatter $columnFormatter
96
     * @return Column
97
     */
98 10
    public function setFormatter(ColumnFormatter $columnFormatter) : Column
99
    {
100 10
        $this->_formatter = $columnFormatter;
101
102 10
        return $this;
103
    }
104
105
    /**
106
     * Get the formatted column value.
107
     *
108
     * @param Model $rowModel
109
     * @return string|null
110
     */
111 44
    public function getFormattedValue(Model $rowModel) : ? string
112
    {
113 44
        $value = $this->getValue($rowModel);
114
115 44
        return $this->_formatter !== null ? $this->_formatter->format($rowModel, $value) : $value;
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $column of hamburgscleanest\DataTab...lumnFormatter::format() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

115
        return $this->_formatter !== null ? $this->_formatter->format($rowModel, /** @scrutinizer ignore-type */ $value) : $value;
Loading history...
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 44
    public function getValue(Model $rowModel) : ? string
125
    {
126 44
        return $rowModel->{$this->getKey()};
127
    }
128
129
    /**
130
     * @return string
131
     */
132 44
    public function getKey() : string
133
    {
134 44
        return $this->_key;
135
    }
136
137
    /**
138
     * @return string
139
     */
140 45
    public function getIdentifier() : string
141
    {
142 45
        return $this->getAttributeName() . ' AS ' . $this->_key;
143
    }
144
145
    /**
146
     * @return string
147
     */
148 46
    public function getAttributeName() : string
149
    {
150 46
        if ($this->_relation !== null)
151
        {
152 4
            return $this->_relation->attributeName;
153
        }
154
155 45
        if ($this->_mutated)
156
        {
157 1
            return $this->_name;
158
        }
159
160 44
        return $this->_table . '.' . $this->_name;
161
    }
162
}
163