Test Failed
Push — master ( f74f09...dc3255 )
by Petr
02:47
created

PDO::listTaxonomyBindValues()   A

Complexity

Conditions 6
Paths 24

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 16
rs 9.2222
cc 6
nc 24
nop 2
1
<?php
2
3
namespace kalanis\nested_tree\Sources\PDO;
4
5
use kalanis\nested_tree\Sources\SourceInterface;
6
use kalanis\nested_tree\Support;
7
use PDO as base;
8
9
abstract class PDO implements SourceInterface
10
{
11
    use Support\ColumnsTrait;
0 ignored issues
show
introduced by
The trait kalanis\nested_tree\Support\ColumnsTrait requires some properties which are not provided by kalanis\nested_tree\Sources\PDO\PDO: $parentIdColumnName, $positionColumnName, $levelColumnName, $rightColumnName, $leftColumnName, $idColumnName
Loading history...
12
13
    public function __construct(
14
        protected readonly base $pdo,
15
        protected readonly Support\Node $nodeBase,
16
        protected readonly Support\TableSettings $settings,
17
    ) {
18
    }
19
20
    /**
21
     * @param array<array<mixed>> $rows
22
     * @return array<int, Support\Node>
23
     */
24
    protected function fromDbRows(array $rows) : array
25
    {
26
        $result = [];
27
        foreach ($rows as &$row) {
28
            $data = $this->fillDataFromRow($row);
29
            $result[$data->id] = $data;
30
        }
31
32
        return $result;
33
    }
34
35
    /**
36
     * @param array<mixed> $row
37
     * @return Support\Node
38
     */
39
    protected function fillDataFromRow(array $row) : Support\Node
40
    {
41
        $data = clone $this->nodeBase;
42
        foreach ($row as $k => $v) {
43
            if ($this->settings->idColumnName === $k) {
44
                $data->id = max(0, intval($v));
45
            } elseif ($this->settings->parentIdColumnName === $k) {
46
                $data->parentId = is_null($v) && $this->settings->rootIsNull ? null : max(0, intval($v));
47
            } elseif ($this->settings->levelColumnName === $k) {
48
                $data->level = max(0, intval($v));
49
            } elseif ($this->settings->leftColumnName === $k) {
50
                $data->left = max(0, intval($v));
51
            } elseif ($this->settings->rightColumnName === $k) {
52
                $data->right = max(0, intval($v));
53
            } elseif ($this->settings->positionColumnName === $k) {
54
                $data->position = max(0, intval($v));
55
            } else {
56
                $data->{$k} = strval($v);
57
            }
58
        }
59
60
        return $data;
61
    }
62
}
63