Completed
Push — master ( aaa409...98f154 )
by Timo
04:23
created

Column::getAggregate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace hamburgscleanest\DataTables\Models;
4
5
use hamburgscleanest\DataTables\Interfaces\ColumnFormatter;
6
7
8
/**
9
 * Class Header
10
 * @package hamburgscleanest\hamburgscleanest\DataTables\Models
11
 */
12
class Column {
13
14
    /** @var string */
15
    private $_name;
16
17
    /** @var Relation */
18
    private $_relation;
19
20
    /** @var ColumnFormatter */
21
    private $_formatter;
22
23
    /**
24
     * Column constructor.
25
     * @param string $name
26
     * @param ColumnFormatter|null $columnFormatter
27
     */
28 38
    public function __construct(string $name, ? ColumnFormatter $columnFormatter = null)
29
    {
30 38
        $this->setName($name);
31 38
        $this->_formatter = $columnFormatter;
32 38
    }
33
34
    /**
35
     * @return string
36
     */
37 31
    public function getName(): string
38
    {
39 31
        return $this->_name;
40
    }
41
42
    /**
43
     * @param string $name
44
     */
45 38
    public function setName(string $name)
46
    {
47 38
        $posDivider = \mb_strpos($name, '.');
48 38
        if ($posDivider === false)
49
        {
50 36
            $this->_name = $name;
51
52 36
            return;
53
        }
54
55 4
        $this->_relation = new Relation($name);
56 4
        $this->_name = \str_replace(')', '', \mb_substr($name, $posDivider + 1));
57 4
    }
58
59
    /**
60
     * @return null|Relation
61
     */
62 31
    public function getRelation(): ?Relation
63
    {
64 31
        return $this->_relation;
65
    }
66
67
    /**
68
     * @param ColumnFormatter $columnFormatter
69
     * @return Column
70
     */
71 2
    public function setFormatter(ColumnFormatter $columnFormatter): Column
72
    {
73 2
        $this->_formatter = $columnFormatter;
74
75 2
        return $this;
76
    }
77
78
    /**
79
     * Formats the column data.
80
     *
81
     * @param string $data
82
     * @return string
83
     */
84 27
    public function format(string $data): string
85
    {
86 27
        return $this->_formatter !== null ? $this->_formatter->format($data) : $data;
87
    }
88
}