Passed
Push — master ( 4c6fab...ae3608 )
by Petr
03:10
created

ColumnsTrait::isColumnNameFromSpecial()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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 6
    protected function isColumnNameFromSpecial(string $name) : bool
54
    {
55 6
        return 'row' === $name;
56
    }
57
58 4
    protected function isColumnNameFromTree(string $name) : bool
59
    {
60 4
        return in_array($name, [
61 4
            'parentId',
62 4
            'left',
63 4
            'right',
64 4
            'level',
65 4
            'position',
66 4
        ]);
67
    }
68
69 6
    protected function allowColumn(TableSettings $settings, string $column) : bool
70
    {
71 6
        return !in_array($column, $settings->skipAlways);
72
    }
73
74 6
    protected function allowColumnWithEmptyValue(TableSettings $settings, string $column, mixed $value) : bool
75
    {
76 6
        return in_array($column, $settings->skipIfEmpty) ? !is_null($value) : true;
77
    }
78
}
79