-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListTreeStructureConverter.php
110 lines (99 loc) · 2.79 KB
/
ListTreeStructureConverter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
* Use this class to convert data from Adjacency List to FileTree structure.
*
* @author Fernando Hidemi Uchiyama
*/
class ListTreeStructureConverter
{
/**
* @var array[]
*/
private $data = array();
/**
* @var array[]
*/
private $hierarchy = array();
/**
* Sets the data to be arranged as a tree.
*
* The $data parameter is an array of array.
*
* Each element of the main array represents a single record. Each element of the array inside represents an
* attribute. Records can vary on the number of attributes. The index of the main array is the ID of the record.
*
* For example:
*
* array(
* 1 => array('name' => 'Fulano', 'age' => 10),
* 2 => array('name' => 'Ciclano', 'age' => 11, 'sex' => 'M'),
* )
*
* In the example above we have two records. The first record has two attributes and the second has three
* attributes. The 1 and 2 are the IDs of the respective record.
*
* @param array Array of records
*/
public function setData(array $data)
{
$this->data = $data;
}
/**
* @return array[]
*/
public function getData()
{
return $this->data;
}
/**
* Sets the hierarchy data. Only the IDs of the records are required.
*
* The $hierarchy parameter is an array of array.
*
* Each element of the main array is an array of IDs representing the children for the ID specified by the
* index of the main array.
*
* For example:
*
* array(
* 1 => array(2, 3),
* 2 => array(),
* 3 => array(),
* )
*
* In the example above we are saying that record ID 1 has two children: 2 and 3. Records 2 and 3 have no children.
*
* @param array[] Array of array of children ids
* @throws InvalidArgumentException
*/
public function setHierarchy($hierarchy)
{
if (!is_array($hierarchy)) {
throw new InvalidArgumentException('Hierarchy must be an array.');
}
$this->hierarchy = $hierarchy;
}
/**
* @return array[]
*/
public function getHierarchy()
{
return $this->hierarchy;
}
/**
* Converts to File Tree Structure starting from id provided by $itemId.
*
* @param int Id to start from
* @return array Returns the converted data as an array of multiple array structure.
*/
public function convert($itemId)
{
$return = array();
$return['data'] = $this->data[$itemId];
$return['children'] = array();
foreach ($this->hierarchy[$itemId] as $child) {
$return['children'] = array_merge($return['children'], array($this->convert($child)));
}
return $return;
}
}