Completed
Push — master ( 895c5c...49cc4e )
by Timo
11:39
created

Column   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 3
dl 0
loc 143
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0

11 Methods

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