Skip to content

Commit 404fbf1

Browse files
committed
Update Collection
1 parent 4ec67cc commit 404fbf1

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

CHANGELOG.markdown

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

src/Kalnoy/Nestedset/Collection.php

+23-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@
44

55
class Collection extends BaseCollection {
66

7+
/**
8+
* Convert list of nodes to dictionary with specified key.
9+
*
10+
* If no key is specified then "parent_id" is used.
11+
*
12+
* @param string $key
13+
*
14+
* @return array
15+
* @deprecated since 1.1
16+
*/
17+
public function toDictionary($key = null)
18+
{
19+
if ($key === null) $key = $this->first()->getParentIdName();
20+
21+
return $this->groupBy($key)->all();
22+
}
23+
724
/**
825
* Build tree from node list. Each item will have set children relation.
926
*
@@ -27,20 +44,22 @@ public function toTree($rootNodeId = null)
2744

2845
$rootNodeId = $this->getRootNodeId($rootNodeId);
2946

30-
if (!isset($dictionary[$rootNodeId]))
47+
if (!$dictionary->has($rootNodeId))
3148
{
3249
return $result;
3350
}
3451

35-
$result->items = $dictionary[$rootNodeId];
52+
$result->items = $dictionary->get($rootNodeId);
3653

3754
foreach ($this->items as $item)
3855
{
3956
$key = $item->getKey();
4057

41-
$children = new BaseCollection(isset($dictionary[$key]) ? $dictionary[$key] : array());
58+
$children = $dictionary->has($key)
59+
? $dictionary->get($key)
60+
: array();
4261

43-
$item->setRelation('children', $children);
62+
$item->setRelation('children', new BaseCollection($children));
4463
}
4564

4665
return $result;

0 commit comments

Comments
 (0)