Completed
Push — master ( 4bef56...de577b )
by Timo
15s
created

Column::getRelation()   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 0
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 49
33
    /**
34 49
     * Column constructor.
35 49
     * @param string $name
36 49
     * @param ColumnFormatter|null $columnFormatter
37
     * @param Model|null $sourceModel
38
     */
39
    public function __construct(string $name, ? ColumnFormatter $columnFormatter = null, ? Model $sourceModel = null)
40
    {
41 49
        $this->_setName($name);
42
        $this->_formatter = $columnFormatter;
43 49
        if ($sourceModel !== null)
44 49
        {
45
            $this->_table = $sourceModel->getTable();
46 47
            $this->_mutated = \in_array($name, $sourceModel->getMutatedAttributes(), true);
47
        }
48 47
    }
49
50
    /**
51 5
     * @param string $name
52
     */
53 5
    private function _setName(string $name) : void
54 5
    {
55 5
        $posDivider = \mb_strpos($name, '.');
56 5
        if ($posDivider === false)
57
        {
58
            $this->_name = $this->_key = $name;
59
60
            return;
61 34
        }
62
63 34
        $this->_name = \str_replace(')', '', \mb_substr($name, $posDivider + 1));
64
65
        $this->_relation = new Relation($name);
66
        $aggregate = $this->_relation->aggregate;
67
        $this->_key = ($aggregate !== 'first' ? ($aggregate . '_') : '') . $this->_relation->name;
68
    }
69 5
70
    /**
71 5
     * @return string
72
     */
73
    public function getName() : string
74
    {
75
        return $this->_name;
76
    }
77 4
78
    /**
79 4
     * @return bool
80
     */
81
    public function isMutated() : bool
82
    {
83
        return $this->_mutated;
84
    }
85 3
86
    /**
87 3
     * @return null|Relation
88
     */
89
    public function getRelation() : ? Relation
90
    {
91
        return $this->_relation;
92
    }
93
94 2
    /**
95
     * @param ColumnFormatter $columnFormatter
96 2
     * @return Column
97
     */
98 2
    public function setFormatter(ColumnFormatter $columnFormatter) : Column
99
    {
100
        $this->_formatter = $columnFormatter;
101
102
        return $this;
103
    }
104
105
    /**
106
     * Get the formatted column value.
107 34
     *
108
     * @param Model $rowModel
109 34
     * @return string|null
110
     */
111 34
    public function getFormattedValue(Model $rowModel) : ? string
112
    {
113
        $value = $this->getValue($rowModel);
114
115
        return $this->_formatter !== null ? $this->_formatter->format($value) : $value;
116
    }
117
118
    /**
119
     * Get the value of this column for the given row.
120 34
     *
121
     * @param Model $rowModel
122 34
     * @return string|null
123
     */
124 3
    public function getValue(Model $rowModel) : ? string
125
    {
126
        return $rowModel->{$this->getKey()};
127 34
    }
128
129
    /**
130
     * @return string
131
     */
132
    public function getKey() : string
133
    {
134
        return $this->_key;
135
    }
136 3
137
    /**
138 3
     * @return string
139 3
     */
140
    public function getIdentifier() : string
141 1
    {
142
        return $this->getAttributeName() . ' AS ' . $this->_key;
143
    }
144 2
145
    /**
146
     * @return string
147
     */
148
    public function getAttributeName() : string
149
    {
150
        if ($this->_relation !== null)
151 34
        {
152
            return $this->_relation->attributeName;
153 34
        }
154
155
        if ($this->_mutated)
156
        {
157
            return $this->_name;
158
        }
159
160
        return $this->_table . '.' . $this->_name;
161
    }
162
}
163