|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace StefanoTree\NestedSet\Adapter; |
|
6
|
|
|
|
|
7
|
|
|
use StefanoTree\NestedSet\NodeInfo; |
|
8
|
|
|
use StefanoTree\NestedSet\Options; |
|
9
|
|
|
|
|
10
|
|
|
abstract class AdapterAbstract implements AdapterInterface |
|
11
|
|
|
{ |
|
12
|
|
|
private $options; |
|
13
|
|
|
|
|
14
|
|
|
private $dbSelectBuilder; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param Options $options |
|
18
|
|
|
*/ |
|
19
|
221 |
|
protected function setOptions(Options $options): void |
|
20
|
|
|
{ |
|
21
|
221 |
|
$this->options = $options; |
|
22
|
221 |
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @return Options |
|
26
|
|
|
*/ |
|
27
|
206 |
|
protected function getOptions(): Options |
|
28
|
|
|
{ |
|
29
|
206 |
|
return $this->options; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Data cannot contain keys like idColumnName, levelColumnName, ... |
|
34
|
|
|
* |
|
35
|
|
|
* @param array $data |
|
36
|
|
|
* |
|
37
|
|
|
* @return array |
|
38
|
|
|
*/ |
|
39
|
11 |
|
protected function cleanData(array $data): array |
|
40
|
|
|
{ |
|
41
|
11 |
|
$options = $this->getOptions(); |
|
42
|
|
|
|
|
43
|
|
|
$disallowedDataKeys = array( |
|
44
|
11 |
|
$options->getIdColumnName(), |
|
45
|
11 |
|
$options->getLeftColumnName(), |
|
46
|
11 |
|
$options->getRightColumnName(), |
|
47
|
11 |
|
$options->getLevelColumnName(), |
|
48
|
11 |
|
$options->getParentIdColumnName(), |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
11 |
|
if (null !== $options->getScopeColumnName()) { |
|
52
|
4 |
|
$disallowedDataKeys[] = $options->getScopeColumnName(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
11 |
|
return array_diff_key($data, array_flip($disallowedDataKeys)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param array $data |
|
60
|
|
|
* |
|
61
|
|
|
* @return NodeInfo |
|
62
|
|
|
*/ |
|
63
|
91 |
|
protected function _buildNodeInfoObject(array $data) |
|
64
|
|
|
{ |
|
65
|
91 |
|
$options = $this->getOptions(); |
|
66
|
|
|
|
|
67
|
91 |
|
$id = $data[$options->getIdColumnName()]; |
|
68
|
91 |
|
$parentId = $data[$options->getParentIdColumnName()]; |
|
69
|
91 |
|
$level = (int) $data[$options->getLevelColumnName()]; |
|
70
|
91 |
|
$left = (int) $data[$options->getLeftColumnName()]; |
|
71
|
91 |
|
$right = (int) $data[$options->getRightColumnName()]; |
|
72
|
|
|
|
|
73
|
91 |
|
if (isset($data[$options->getScopeColumnName()])) { |
|
74
|
39 |
|
$scope = $data[$options->getScopeColumnName()]; |
|
75
|
|
|
} else { |
|
76
|
52 |
|
$scope = null; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
91 |
|
return new NodeInfo($id, $parentId, $level, $left, $right, $scope); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
*/ |
|
85
|
18 |
|
public function setDbSelectBuilder(callable $selectBuilder): void |
|
86
|
|
|
{ |
|
87
|
18 |
|
$this->dbSelectBuilder = $selectBuilder; |
|
88
|
18 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return callable |
|
92
|
|
|
*/ |
|
93
|
62 |
|
public function getDbSelectBuilder(): callable |
|
94
|
|
|
{ |
|
95
|
62 |
|
if (null === $this->dbSelectBuilder) { |
|
96
|
50 |
|
$this->dbSelectBuilder = function () { |
|
97
|
50 |
|
return $this->getBlankDbSelect(); |
|
98
|
|
|
}; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
62 |
|
return $this->dbSelectBuilder; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|