Skip to content

Commit 524cef4

Browse files
committed
Merge remote-tracking branch 'origin/1.1'
2 parents 149dfe8 + 7b5e95d commit 524cef4

File tree

4 files changed

+56
-45
lines changed

4 files changed

+56
-45
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
/.idea
33
composer.phar
44
composer.lock
5-
.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+
* `Collection::toDictionary` is now obsolete. Use `Collection::groupBy`.

composer.json

+7-7
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.18",
17+
"illuminate/database": "~4.1.18"
1718
},
1819

1920
"require-dev": {
@@ -24,6 +25,5 @@
2425
"psr-0": {
2526
"Kalnoy\\Nestedset": "src/"
2627
}
27-
},
28-
"minimum-stability": "dev"
28+
}
2929
}

src/Kalnoy/Nestedset/Collection.php

+46-37
Original file line numberDiff line numberDiff line change
@@ -9,76 +9,85 @@ class Collection extends BaseCollection {
99
*
1010
* If no key is specified then "parent_id" is used.
1111
*
12-
* @param string $key
12+
* @param string $key
1313
*
1414
* @return array
15+
* @deprecated since 1.1
1516
*/
1617
public function toDictionary($key = null)
1718
{
18-
if (empty($this->items)) {
19-
return array();
20-
}
21-
22-
if ($key === null) {
23-
$key = $this->first()->getParentIdName();
24-
}
25-
26-
$result = array();
27-
28-
foreach ($this->items as $item) {
29-
$result[$item->$key][] = $item;
30-
}
19+
if ($key === null) $key = $this->first()->getParentIdName();
3120

32-
return $result;
21+
return $this->groupBy($key)->all();
3322
}
3423

3524
/**
36-
* Build tree from node list.
25+
* Build tree from node list. Each item will have set children relation.
3726
*
3827
* To succesfully build tree "id", "_lft" and "parent_id" keys must present.
39-
*
28+
*
4029
* If {@link rootNodeId} is provided, the tree will contain only descendants
4130
* of the node with such primary key value.
42-
*
31+
*
4332
* @param integer $rootNodeId
4433
*
4534
* @return Collection
4635
*/
4736
public function toTree($rootNodeId = null)
4837
{
49-
$dictionary = $this->toDictionary();
5038
$result = new static();
5139

52-
// If root node is not specified we take parent id of node with
53-
// least lft value as root node id.
54-
if ($rootNodeId === null)
55-
{
56-
$leastValue = null;
40+
if (empty($this->items)) return $result;
5741

58-
foreach ($this->items as $item) {
59-
if ($leastValue === null || $item->getLft() < $leastValue)
60-
{
61-
$leastValue = $item->getLft();
62-
$rootNodeId = $item->getParentId();
63-
}
64-
}
65-
}
42+
$key = $this->first()->getParentIdName();
43+
$dictionary = $this->groupBy($key);
6644

67-
$result->items = isset($dictionary[$rootNodeId]) ? $dictionary[$rootNodeId] : array();
45+
$rootNodeId = $this->getRootNodeId($rootNodeId);
6846

69-
if (empty($result->items))
47+
if (!$dictionary->has($rootNodeId))
7048
{
7149
return $result;
7250
}
7351

74-
foreach ($this->items as $item)
52+
$result->items = $dictionary->get($rootNodeId);
53+
54+
foreach ($this->items as $item)
7555
{
7656
$key = $item->getKey();
7757

78-
$children = new BaseCollection(isset($dictionary[$key]) ? $dictionary[$key] : array());
79-
$item->setRelation('children', $children);
58+
$children = $dictionary->has($key)
59+
? $dictionary->get($key)
60+
: array();
61+
62+
$item->setRelation('children', new BaseCollection($children));
8063
}
8164

8265
return $result;
8366
}
67+
68+
/**
69+
* @param null|int $rootNodeId
70+
*
71+
* @return int
72+
*/
73+
public function getRootNodeId($rootNodeId = null)
74+
{
75+
// If root node is not specified we take parent id of node with
76+
// least lft value as root node id.
77+
if ($rootNodeId === null)
78+
{
79+
$leastValue = null;
80+
81+
foreach ($this->items as $item)
82+
{
83+
if ($leastValue === null || $item->getLft() < $leastValue)
84+
{
85+
$leastValue = $item->getLft();
86+
$rootNodeId = $item->getParentId();
87+
}
88+
}
89+
}
90+
91+
return $rootNodeId;
92+
}
8493
}

0 commit comments

Comments
 (0)