Completed
Push — master ( bbcbbc...8c42c0 )
by Timo
07:53
created

ColumnRelation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace hamburgscleanest\DataTables\Models\Column;
4
5
/**
6
 * Class Relation
7
 * @package hamburgscleanest\hamburgscleanest\DataTables\Models\Column
8
 */
9
class ColumnRelation {
10
11
    /** @var string */
12
    public $name;
13
14
    /** @var string */
15
    public $aggregate = 'first';
16
17
    /** @var string */
18
    public $attributeName;
19
20
    /**
21
     * Relation constructor.
22
     * @param string $columnName
23
     * @internal param string $name
24
     */
25 6
    public function __construct(string $columnName)
26
    {
27 6
        $this->attributeName = $columnName;
28 6
        $this->name = $this->_extractAggregate($columnName);
29 6
    }
30
31
    /**
32
     * @param string $name
33
     * @return string
34
     */
35 6
    private function _extractAggregate(string $name) : string
36
    {
37 6
        $replaced = 0;
38 6
        $extractedName = \preg_replace('/\((.*?)\)/', '#$1', $name, 1, $replaced);
39 6
        if ($replaced === 0)
40
        {
41 3
            return \str_replace('.', '_', \mb_strtolower($extractedName));
42
        }
43
44 3
        $parts = \explode('#', $extractedName);
45 3
        $this->aggregate = \mb_strtolower($parts[0]);
46
47 3
        return \str_replace('.', '_', \mb_strtolower($parts[1]));
48
    }
49
}