Completed
Push — master ( ffa818...f511c0 )
by Timo
07:51
created

Column::_getValueFromRelation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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