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
|
227 |
|
protected function setOptions(Options $options): void |
20
|
|
|
{ |
21
|
227 |
|
$this->options = $options; |
22
|
227 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
212 |
|
public function getOptions(): Options |
28
|
|
|
{ |
29
|
212 |
|
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
|
94 |
|
protected function _buildNodeInfoObject(array $data) |
64
|
|
|
{ |
65
|
94 |
|
$options = $this->getOptions(); |
66
|
|
|
|
67
|
94 |
|
$id = $data[$options->getIdColumnName()]; |
68
|
94 |
|
$parentId = $data[$options->getParentIdColumnName()]; |
69
|
94 |
|
$level = (int) $data[$options->getLevelColumnName()]; |
70
|
94 |
|
$left = (int) $data[$options->getLeftColumnName()]; |
71
|
94 |
|
$right = (int) $data[$options->getRightColumnName()]; |
72
|
|
|
|
73
|
94 |
|
if (isset($data[$options->getScopeColumnName()])) { |
74
|
39 |
|
$scope = $data[$options->getScopeColumnName()]; |
75
|
|
|
} else { |
76
|
55 |
|
$scope = null; |
77
|
|
|
} |
78
|
|
|
|
79
|
94 |
|
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
|
64 |
|
public function getDbSelectBuilder(): callable |
94
|
|
|
{ |
95
|
64 |
|
if (null === $this->dbSelectBuilder) { |
96
|
|
|
$this->dbSelectBuilder = function () { |
97
|
52 |
|
return $this->getBlankDbSelect(); |
98
|
|
|
}; |
99
|
|
|
} |
100
|
|
|
|
101
|
64 |
|
return $this->dbSelectBuilder; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Return base db select without any join, etc. |
106
|
|
|
* |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
|
|
abstract public function getBlankDbSelect(); |
110
|
|
|
} |
111
|
|
|
|