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; |
|
|
|
|
12
|
|
|
|
13
|
65 |
|
public function __construct( |
14
|
|
|
protected readonly base $pdo, |
15
|
|
|
protected readonly Support\Node $nodeBase, |
16
|
|
|
protected readonly Support\TableSettings $settings, |
17
|
|
|
) { |
18
|
65 |
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param array<array<mixed>> $rows |
22
|
|
|
* @return array<int, Support\Node> |
23
|
|
|
*/ |
24
|
62 |
|
protected function fromDbRows(array $rows) : array |
25
|
|
|
{ |
26
|
62 |
|
$result = []; |
27
|
62 |
|
foreach ($rows as &$row) { |
28
|
62 |
|
$data = $this->fillDataFromRow($row); |
29
|
62 |
|
$result[$data->id] = $data; |
30
|
|
|
} |
31
|
|
|
|
32
|
62 |
|
return $result; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param array<mixed> $row |
37
|
|
|
* @return Support\Node |
38
|
|
|
*/ |
39
|
62 |
|
protected function fillDataFromRow(array $row) : Support\Node |
40
|
|
|
{ |
41
|
62 |
|
$data = clone $this->nodeBase; |
42
|
62 |
|
foreach ($row as $k => $v) { |
43
|
62 |
|
if ($this->settings->idColumnName === $k) { |
44
|
62 |
|
$data->id = max(0, intval($v)); |
45
|
62 |
|
} elseif ($this->settings->parentIdColumnName === $k) { |
46
|
62 |
|
$data->parentId = is_null($v) && $this->settings->rootIsNull ? null : max(0, intval($v)); |
47
|
62 |
|
} elseif ($this->settings->levelColumnName === $k) { |
48
|
62 |
|
$data->level = max(0, intval($v)); |
49
|
62 |
|
} elseif ($this->settings->leftColumnName === $k) { |
50
|
62 |
|
$data->left = max(0, intval($v)); |
51
|
62 |
|
} elseif ($this->settings->rightColumnName === $k) { |
52
|
62 |
|
$data->right = max(0, intval($v)); |
53
|
62 |
|
} elseif ($this->settings->positionColumnName === $k) { |
54
|
62 |
|
$data->position = max(0, intval($v)); |
55
|
|
|
} else { |
56
|
62 |
|
$data->{$k} = strval($v); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
62 |
|
return $data; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|