Completed
Pull Request — master (#16)
by Timo
06:00
created

Relation::getValue()   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 2
crap 2
1
<?php
2
3
namespace hamburgscleanest\DataTables\Models;
4
5
/**
6
 * Class Relation
7
 * @package hamburgscleanest\hamburgscleanest\DataTables\Models
8
 */
9
class Relation {
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
            $parts = \explode('#', $extractedName);
42 3
            $this->aggregate = \mb_strtolower($parts[0]);
43
44 3
            $extractedName = $parts[1];
45
        }
46
47 6
        return \mb_substr($extractedName, 0, \mb_strpos($extractedName, '.'));
48
    }
49
}