Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
18.52% |
5 / 27 |
|
42.86% |
3 / 7 |
CRAP | |
0.00% |
0 / 1 |
TreeWrapper | |
18.52% |
5 / 27 |
|
42.86% |
3 / 7 |
193.28 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSortedTreeByName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
filterTree | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
56 | |||
applyLimitAndOffset | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getDefaultChildren | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getTreeArray | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
sortTreeNodes | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | |
3 | namespace oat\tao\model\Tree; |
4 | |
5 | use oat\generis\model\kernel\persistence\smoothsql\search\filter\FilterOperator; |
6 | |
7 | class TreeWrapper |
8 | { |
9 | /** @var array */ |
10 | protected $treeArray; |
11 | |
12 | /** |
13 | * @param array $treeArray |
14 | */ |
15 | public function __construct(array $treeArray) |
16 | { |
17 | $this->treeArray = $treeArray; |
18 | } |
19 | |
20 | /** |
21 | * @deprecated tree nodes will be sorted by name by default |
22 | * @return TreeWrapper |
23 | */ |
24 | public function getSortedTreeByName() |
25 | { |
26 | return $this; |
27 | } |
28 | |
29 | /** |
30 | * @param []Filter |
31 | * @return TreeWrapper |
32 | */ |
33 | public function filterTree(array $filters) |
34 | { |
35 | $filteredArray = $this->treeArray; |
36 | |
37 | $childrenMatch = []; |
38 | |
39 | foreach ($filters as $filter) { |
40 | if (empty($filteredArray['children'])) { |
41 | continue; |
42 | } |
43 | |
44 | foreach ($filteredArray['children'] as $node) { |
45 | $endDate = (int)(string)$node['attributes'][$filter->getKey()]; |
46 | |
47 | if ( |
48 | $filter->getOperator() === FilterOperator::GREATER_THAN_EQUAL |
49 | && ($filter->getValue() < $endDate || $endDate === 0) |
50 | ) { |
51 | $childrenMatch[] = $node; |
52 | continue; |
53 | } |
54 | } |
55 | } |
56 | |
57 | $filteredArray['children'] = $childrenMatch; |
58 | $filteredArray['count'] = count($childrenMatch); |
59 | |
60 | return new self($filteredArray); |
61 | } |
62 | |
63 | /** |
64 | * @param $limit |
65 | * @param $offset |
66 | * @return TreeWrapper |
67 | */ |
68 | public function applyLimitAndOffset($limit, $offset) |
69 | { |
70 | $array = $this->treeArray; |
71 | |
72 | $array['children'] = array_slice($array['children'], $offset, $limit); |
73 | |
74 | return new self($array); |
75 | } |
76 | |
77 | /** |
78 | * @return TreeWrapper |
79 | */ |
80 | public function getDefaultChildren() |
81 | { |
82 | $treeArray = $this->treeArray; |
83 | |
84 | $treeArray['children'] = isset($treeArray['children']) ? $treeArray['children'] : []; |
85 | |
86 | return new self($treeArray['children']); |
87 | } |
88 | |
89 | /** |
90 | * @return array |
91 | */ |
92 | public function getTreeArray() |
93 | { |
94 | return $this->treeArray; |
95 | } |
96 | |
97 | /** |
98 | * @param $a |
99 | * @param $b |
100 | * @return int |
101 | */ |
102 | protected function sortTreeNodes($a, $b) |
103 | { |
104 | if (isset($a['data']) && isset($b['data'])) { |
105 | if ($a['type'] != $b['type']) { |
106 | return ($a['type'] == 'class') ? -1 : 1; |
107 | } else { |
108 | return strcasecmp($a['data'], $b['data']); |
109 | } |
110 | } |
111 | } |
112 | } |