Skip to content

Commit 4ec67cc

Browse files
committed
Remove Collection::toDictionary
1 parent a4488db commit 4ec67cc

File tree

4 files changed

+45
-50
lines changed

4 files changed

+45
-50
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/vendor
2+
/.idea
23
composer.phar
34
composer.lock
4-
.DS_Store
5+
.DS_Store

CHANGELOG.markdown

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
### 1.0.1
2+
* Removed `Collection::toDictionary`. Use `Collection::groupBy`.

composer.json

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
{
22
"name": "kalnoy/nestedset",
33
"description": "Nested Set Model for Laravel 4",
4-
"keywords": ["laravel", "nested set", "database", "hierarchy"],
4+
"keywords": ["laravel", "nested sets", "database", "hierarchy"],
55
"licence": "MIT",
6-
6+
77
"authors": [
88
{
9-
"name": "Aleksander Kalnoy",
9+
"name": "Alexander Kalnoy",
1010
"email": "[email protected]"
1111
}
1212
],
13+
1314
"require": {
1415
"php": ">=5.3.0",
15-
"illuminate/support": ">=4.0,<4.2",
16-
"illuminate/database": ">=4.0,<4.2"
16+
"illuminate/support": "4.1.*",
17+
"illuminate/database": "4.1.*"
1718
},
1819

1920
"require-dev": {
@@ -25,5 +26,6 @@
2526
"Kalnoy\\Nestedset": "src/"
2627
}
2728
},
29+
2830
"minimum-stability": "dev"
2931
}

src/Kalnoy/Nestedset/Collection.php

+34-44
Original file line numberDiff line numberDiff line change
@@ -5,57 +5,62 @@
55
class Collection extends BaseCollection {
66

77
/**
8-
* Convert list of nodes to dictionary with specified key.
8+
* Build tree from node list. Each item will have set children relation.
99
*
10-
* If no key is specified then "parent_id" is used.
10+
* To succesfully build tree "id", "_lft" and "parent_id" keys must present.
11+
*
12+
* If {@link rootNodeId} is provided, the tree will contain only descendants
13+
* of the node with such primary key value.
1114
*
12-
* @param string $key
15+
* @param integer $rootNodeId
1316
*
14-
* @return array
17+
* @return Collection
1518
*/
16-
public function toDictionary($key = null)
19+
public function toTree($rootNodeId = null)
1720
{
18-
if (empty($this->items)) {
19-
return array();
20-
}
21+
$result = new static();
22+
23+
if (empty($this->items)) return $result;
24+
25+
$key = $this->first()->getParentIdName();
26+
$dictionary = $this->groupBy($key);
2127

22-
if ($key === null) {
23-
$key = $this->first()->getParentIdName();
28+
$rootNodeId = $this->getRootNodeId($rootNodeId);
29+
30+
if (!isset($dictionary[$rootNodeId]))
31+
{
32+
return $result;
2433
}
2534

26-
$result = array();
35+
$result->items = $dictionary[$rootNodeId];
36+
37+
foreach ($this->items as $item)
38+
{
39+
$key = $item->getKey();
40+
41+
$children = new BaseCollection(isset($dictionary[$key]) ? $dictionary[$key] : array());
2742

28-
foreach ($this->items as $item) {
29-
$result[$item->$key][] = $item;
43+
$item->setRelation('children', $children);
3044
}
3145

3246
return $result;
3347
}
3448

3549
/**
36-
* Build tree from node list.
37-
*
38-
* To succesfully build tree "id", "_lft" and "parent_id" keys must present.
39-
*
40-
* If {@link rootNodeId} is provided, the tree will contain only descendants
41-
* of the node with such primary key value.
42-
*
43-
* @param integer $rootNodeId
50+
* @param null|int $rootNodeId
4451
*
45-
* @return Collection
52+
* @return int
4653
*/
47-
public function toTree($rootNodeId = null)
54+
public function getRootNodeId($rootNodeId = null)
4855
{
49-
$dictionary = $this->toDictionary();
50-
$result = new static();
51-
5256
// If root node is not specified we take parent id of node with
5357
// least lft value as root node id.
54-
if ($rootNodeId === null)
58+
if ($rootNodeId === null)
5559
{
5660
$leastValue = null;
5761

58-
foreach ($this->items as $item) {
62+
foreach ($this->items as $item)
63+
{
5964
if ($leastValue === null || $item->getLft() < $leastValue)
6065
{
6166
$leastValue = $item->getLft();
@@ -64,21 +69,6 @@ public function toTree($rootNodeId = null)
6469
}
6570
}
6671

67-
$result->items = isset($dictionary[$rootNodeId]) ? $dictionary[$rootNodeId] : array();
68-
69-
if (empty($result->items))
70-
{
71-
return $result;
72-
}
73-
74-
foreach ($this->items as $item)
75-
{
76-
$key = $item->getKey();
77-
78-
$children = new BaseCollection(isset($dictionary[$key]) ? $dictionary[$key] : array());
79-
$item->setRelation('children', $children);
80-
}
81-
82-
return $result;
72+
return $rootNodeId;
8373
}
8474
}

0 commit comments

Comments
 (0)