Completed
Push — master ( c48d7b...aaa409 )
by Timo
04:06
created

Column::setName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 2
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 string */
18
    private $_relation;
19
20
    /** @var string */
21
    private $_aggregate = 'first';
22
23
    /** @var ColumnFormatter */
24
    private $_formatter;
25
26
    /**
27
     * Column constructor.
28
     * @param string $name
29
     * @param ColumnFormatter|null $columnFormatter
30
     */
31 38
    public function __construct(string $name, ? ColumnFormatter $columnFormatter = null)
32
    {
33 38
        $this->setName($this->_extractAggregate($name));
34 38
        $this->_formatter = $columnFormatter;
35 38
    }
36
37
    /**
38
     * @param string $name
39
     * @return string
40
     */
41 38
    private function _extractAggregate(string $name): string
42
    {
43 38
        $replaced = 0;
44 38
        $name = preg_replace("/\((.*?)\)/", '#$1', $name, 1, $replaced);
1 ignored issue
show
Coding Style introduced by
Consider using a different name than the parameter $name. This often makes code more readable.
Loading history...
Coding Style Comprehensibility introduced by
The string literal /\((.*?)\)/ does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
45 38
        if ($replaced === 1)
46
        {
47 2
            $parts = \explode('#', $name);
48 2
            $this->_aggregate = \mb_strtolower($parts[0]);
49 2
            $name = $parts[1];
1 ignored issue
show
Coding Style introduced by
Consider using a different name than the parameter $name. This often makes code more readable.
Loading history...
50
        }
51
52 38
        return $name;
53
    }
54
55
    /**
56
     * @return string
57
     */
58 3
    public function getAggregate(): string
59
    {
60 3
        return $this->_aggregate;
61
    }
62
63
    /**
64
     * @return string
65
     */
66 31
    public function getName(): string
67
    {
68 31
        return $this->_name;
69
    }
70
71
    /**
72
     * @param string $name
73
     */
74 38
    public function setName(string $name)
75
    {
76 38
        $posDivider = \mb_strpos($name, '.');
77 38
        if ($posDivider === false)
78
        {
79 36
            $this->_name = $name;
80
81 36
            return;
82
        }
83
84 4
        $this->_name = \mb_substr($name, $posDivider + 1);
85 4
        $this->_relation = \mb_substr($name, 0, $posDivider);
86 4
    }
87
88
    /**
89
     * @return string
90
     */
91 4
    public function getRelation(): string
92
    {
93 4
        return $this->_relation;
94
    }
95
96
    /**
97
     * @return bool
98
     */
99 31
    public function isRelation(): bool
100
    {
101 31
        return !empty($this->_relation);
102
    }
103
104
    /**
105
     * @param ColumnFormatter $columnFormatter
106
     * @return Column
107
     */
108 2
    public function setFormatter(ColumnFormatter $columnFormatter): Column
109
    {
110 2
        $this->_formatter = $columnFormatter;
111
112 2
        return $this;
113
    }
114
115
    /**
116
     * Formats the column data.
117
     *
118
     * @param string $data
119
     * @return string
120
     */
121 27
    public function format(string $data): string
122
    {
123 27
        return $this->_formatter !== null ? $this->_formatter->format($data) : $data;
124
    }
125
}