Passed
Push — master ( f50cc5...0e0d61 )
by Petr
13:38
created

ColumnsTrait::getNameBasedOnExtraSettings()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
namespace kalanis\nested_tree\Support;
4
5
/**
6
 * Trait to work with names of properties within Node class which shall not be added / updated via this package.
7
 */
8
trait ColumnsTrait
9
{
10 7
    protected function translateColumn(TableSettings $settings, string $name) : ?string
11
    {
12 7
        return match ($name) {
13 1
            'id' => $settings->idColumnName,
14 3
            'parentId' => $settings->parentIdColumnName,
15 2
            'level' => $settings->levelColumnName,
16 2
            'left' => $settings->leftColumnName,
17 2
            'right' => $settings->rightColumnName,
18 2
            'position' => $settings->positionColumnName,
19 7
            default => $this->getNameBasedOnExtraSettings($settings, $name),
20 7
        };
21
    }
22
23
    /**
24
     * @param TableSettings $settings
25
     * @param string $name
26
     * @return string|null
27
     *
28
     * Okay, time to be ready for Reflection.
29
     * When there is settings property with name the same as the entry's then get the value it contains and return it
30
     */
31 6
    private function getNameBasedOnExtraSettings(TableSettings $settings, string $name) : ?string
32
    {
33 6
        if (property_exists($settings, $name)) {
34 3
            if (is_null($settings->{$name})) {
35 2
                return null;
36
            }
37
38 1
            return strval($settings->{$name});
39
        }
40
41 4
        return $name;
42
    }
43
44 6
    protected function isColumnNameFromBasic(string $name) : bool
45
    {
46 6
        return in_array($name, [
47 6
            'id',
48 6
            'childrenIds',
49 6
            'childrenNodes',
50 6
        ]);
51
    }
52
53 4
    protected function isColumnNameFromTree(string $name) : bool
54
    {
55 4
        return in_array($name, [
56 4
            'parentId',
57 4
            'left',
58 4
            'right',
59 4
            'level',
60 4
            'position',
61 4
        ]);
62
    }
63
}
64