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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Tree node collection
*
* @author Magento Core Team <core@magentocommerce.com>
*/
namespace Magento\Framework\Data\Tree\Node;
use Magento\Framework\Data\Tree;
use Magento\Framework\Data\Tree\Node;
/**
* @api
* @since 100.0.2
*/
class Collection implements \ArrayAccess, \IteratorAggregate, \Countable
{
/**
* @var array
*/
private $_nodes;
/**
* @var Node
*/
private $_container;
/**
* @param Node $container
*/
public function __construct($container)
{
$this->_nodes = [];
$this->_container = $container;
}
/**
* Get the nodes
*
* @return array
*/
public function getNodes()
{
return $this->_nodes;
}
/**
* Implementation of \IteratorAggregate::getIterator()
*
* @return \ArrayIterator
*/
public function getIterator()
{
return new \ArrayIterator($this->_nodes);
}
/**
* Implementation of \ArrayAccess:offsetSet()
*
* @param string $key
* @param mixed $value
* @return void
*/
public function offsetSet($key, $value)
{
$this->_nodes[$key] = $value;
}
/**
* Implementation of \ArrayAccess:offsetGet()
* @param string $key
* @return mixed
*/
public function offsetGet($key)
{
return $this->_nodes[$key];
}
/**
* Implementation of \ArrayAccess:offsetUnset()
* @param string $key
* @return void
*/
public function offsetUnset($key)
{
unset($this->_nodes[$key]);
}
/**
* Implementation of \ArrayAccess:offsetExists()
* @param string $key
* @return bool
*/
public function offsetExists($key)
{
return isset($this->_nodes[$key]);
}
/**
* Adds a node to this node
* @param Node $node
* @return Node
*/
public function add(Node $node)
{
$node->setParent($this->_container);
// Set the Tree for the node
if ($this->_container->getTree() instanceof Tree) {
$node->setTree($this->_container->getTree());
}
$this->_nodes[$node->getId()] = $node;
return $node;
}
/**
* Delete
*
* @param Node $node
* @return $this
*/
public function delete($node)
{
if (isset($this->_nodes[$node->getId()])) {
unset($this->_nodes[$node->getId()]);
}
return $this;
}
/**
* Return count
*
* @return int
*/
public function count()
{
return count($this->_nodes);
}
/**
* Return the last node
*
* @return mixed
*/
public function lastNode()
{
if (!empty($this->_nodes)) {
$result = end($this->_nodes);
reset($this->_nodes);
} else {
$result = null;
}
return $result;
}
/**
* Search by Id
*
* @param string $nodeId
* @return null
*/
public function searchById($nodeId)
{
if (isset($this->_nodes[$nodeId])) {
return $this->_nodes[$nodeId];
}
return null;
}
}