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

Relation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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