Completed
Pull Request — master (#8)
by Timo
08:24
created

Column::getFormattedValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 1
cts 1
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
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 $_name;
17
18
    /** @var Relation */
19
    private $_relation;
20
21
    /** @var ColumnFormatter */
22
    private $_formatter;
23
24
    /**
25
     * Column constructor.
26
     * @param string $name
27
     * @param ColumnFormatter|null $columnFormatter
28 45
     */
29
    public function __construct(string $name, ? ColumnFormatter $columnFormatter = null)
30 45
    {
31 45
        $this->setName($name);
32 45
        $this->_formatter = $columnFormatter;
33
    }
34
35
    /**
36
     * @return string
37 35
     */
38
    public function getName() : string
39 35
    {
40
        return $this->_name;
41
    }
42
43
    /**
44
     * @param string $name
45 45
     */
46
    public function setName(string $name)
47 45
    {
48 45
        $posDivider = \mb_strpos($name, '.');
49
        if ($posDivider === false)
50 43
        {
51
            $this->_name = $name;
52 43
53
            return;
54
        }
55 5
56 5
        $this->_relation = new Relation($name);
57 5
        $this->_name = \str_replace(')', '', \mb_substr($name, $posDivider + 1));
58
    }
59
60
    /**
61
     * @return null|Relation
62 35
     */
63
    public function getRelation() : ? Relation
64 35
    {
65
        return $this->_relation;
66
    }
67
68
    /**
69
     * @param ColumnFormatter $columnFormatter
70
     * @return Column
71 2
     */
72
    public function setFormatter(ColumnFormatter $columnFormatter) : Column
73 2
    {
74
        $this->_formatter = $columnFormatter;
75 2
76
        return $this;
77
    }
78
79
    /**
80
     * Get the formatted column value.
81
     *
82
     * @param Model $rowModel
83
     * @return string
84 31
     */
85
    public function getFormattedValue(Model $rowModel) : string
86 31
    {
87
        $value = $this->getValue($rowModel);
88
89
        return $this->_formatter !== null ? $this->_formatter->format($value) : $value;
90
    }
91
92
    /**
93
     * Get the value of this column for the given row.
94
     *
95
     * @param Model $rowModel
96
     * @return string
97
     */
98
    public function getValue(Model $rowModel) : string
99
    {
100
        if ($this->_relation !== null)
101
        {
102
            return $this->_getValueFromRelation($rowModel);
103
        }
104
105
        return $this->_getValue($rowModel);
106
    }
107
108
    /**
109
     * Get the value from the column's relation
110
     *
111
     * @param Model $rowModel
112
     * @return string
113
     */
114
    private function _getValueFromRelation(Model $rowModel) : string
115
    {
116
        $relation = $rowModel->getRelation($this->_relation->name);
117
        if ($relation instanceof Model)
118
        {
119
            return $relation->{$this->_name};
120
        }
121
122
        return $this->_relation->getValue($this->_name, $relation);
123
    }
124
125
    /**
126
     * @param Model $rowModel
127
     * @return string
128
     */
129
    private function _getValue(Model $rowModel) : string
130
    {
131
        return (string) $rowModel->{$this->_name} ?? '';
132
    }
133
}
134