Completed
Pull Request — master (#8)
by Timo
03:50 queued 54s
created

Column::getFormattedValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
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
     */
29 45
    public function __construct(string $name, ? ColumnFormatter $columnFormatter = null)
30
    {
31 45
        $this->setName($name);
32 45
        $this->_formatter = $columnFormatter;
33 45
    }
34
35
    /**
36
     * @return string
37
     */
38 35
    public function getName(): string
39
    {
40 35
        return $this->_name;
41
    }
42
43
    /**
44
     * @param string $name
45
     */
46 45
    public function setName(string $name)
47
    {
48 45
        $posDivider = \mb_strpos($name, '.');
49 45
        if ($posDivider === false)
50
        {
51 43
            $this->_name = $name;
52
53 43
            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 35
    public function getRelation(): ? Relation
64
    {
65 35
        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 value from the column's relation
81
     *
82
     * @param Model $rowModel
83
     * @return string
84
     */
85 3
    private function _getValueFromRelation(Model $rowModel) : string
86
    {
87 3
        $relation = $rowModel->getRelation($this->_relation->name);
88 3
        if ($relation instanceof Model)
89
        {
90 1
            return $relation->{$this->_name};
91
        }
92
        
93 2
        return $this->_relation->getValue($this->_name, $relation);
94
    }
95
    
96
    /**
97
     * @param Model $rowModel
98
     * @return string
99
     */
100 31
    private function _getValue(Model $rowModel) : string 
101
    {
102 31
        if(!property_exists($rowModel, $this->_name)) 
103
        {
104 31
            return '';
105
        }
106
        
107
        return (string) $rowModel->{$this->_name};
108
    }
109
    
110
    /**
111
     * Get the value of this column for the given row.
112
     *
113
     * @param Model $rowModel
114
     * @return string
115
     */
116 31
    public function getValue(Model $rowModel) : string
117
    {
118 31
        if($this->_relation !== null)
119
        {
120 3
            return $this->_getValueFromRelation($rowModel);
121
        }
122
        
123 31
        return $this->_getValue($rowModel);
124
    }
125
    
126
    /**
127
     * Get the formatted column value.
128
     *
129
     * @param Model $rowModel
130
     * @return string
131
     */
132 31
    public function getFormattedValue(Model $rowModel): string
133
    {
134 31
        $value = $this->getValue($rowModel);
135
        
136 31
        return $this->_formatter !== null ? $this->_formatter->format($value) : $value;
137
    }
138
}
139